Q:

C Program For QUICK Sort In Ascending Order

belongs to collection: Sorting C Programs

0

Quicksort is a sorting algorithm based on the divide and conquer approach where

 

  1. An array is divided into subarrays by selecting a pivot element (element selected from the array).
    While dividing the array, the pivot element should be positioned in such a way that elements less than pivot are kept on the left side and elements greater than pivot are on the right side of the pivot.
  2. The left and right subarrays are also divided using the same approach. This process continues until each subarray contains a single element.
  3. At this point, elements are already sorted. Finally, elements are combined to form a sorted array.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include

void quicksort(int [10],int,int);

int main()
{
  int x[20],size,i;

  printf("Enter size of the array: ");
  scanf("%d",&size);

  printf("Enter %d elements: ",size);
  for(i=0;i    scanf("%d",&x[i]);

  quicksort(x,0,size-1);

  printf("Sorted elements: ");
  for(i=0;i    printf(" %d",x[i]);

  return 0;
}

void quicksort(int x[10],int first,int last)
{
    int pivot,j,temp,i;

     if(first         pivot=first;
         i=first;
         j=last;

         while(i             while(x[i]<=x[pivot]&&i                 i++;
             while(x[j]>x[pivot])
                 j--;
             if(i                 temp=x[i];
                  x[i]=x[j];
                  x[j]=temp;
             }
         }

         temp=x[pivot];
         x[pivot]=x[j];
         x[j]=temp;
         quicksort(x,first,j-1);
         quicksort(x,j+1,last);

    }
}

 

Output:

Enter size of array :6

Enter 6 elements: 123

36925

-56

0

45

456

Sorted elements : -56 0 45 123 456 36925

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Program For MERGE Sort In Ascending Order... >>
<< C Program For Insertion Sort In Ascending Order...