Advertisement
Guest User

Untitled

a guest
May 27th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.95 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. void quicksort(int [10],int,int);
  4.  
  5. int main(){
  6.   int x[20],size,i;
  7.  
  8.   printf("Enter size of the array: ");
  9.   scanf("%d",&size);
  10.  
  11.   printf("Enter %d elements: ",size);
  12.   for(i=0;i<size;i++)
  13.     scanf("%d",&x[i]);
  14.  
  15.   quicksort(x,0,size-1);
  16.  
  17.   printf("Sorted elements: ");
  18.   for(i=0;i<size;i++)
  19.     printf(" %d",x[i]);
  20.  
  21.   return 0;
  22. }
  23.  
  24. void quicksort(int x[10],int first,int last){
  25.     int pivot,j,temp,i;
  26.  
  27.      if(first<last){
  28.          pivot=first;
  29.          i=first;
  30.          j=last;
  31.  
  32.          while(i<j){
  33.              while(x[i]<=x[pivot]&&i<last)
  34.                  i++;
  35.              while(x[j]>x[pivot])
  36.                  j--;
  37.              if(i<j){
  38.                  temp=x[i];
  39.                   x[i]=x[j];
  40.                   x[j]=temp;
  41.              }
  42.          }
  43.  
  44.          temp=x[pivot];
  45.          x[pivot]=x[j];
  46.          x[j]=temp;
  47.          quicksort(x,first,j-1);
  48.          quicksort(x,j+1,last);
  49.  
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement