Advertisement
AJTAMjid2000

Quick sort

Jun 29th, 2021
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.34 KB | None | 0 0
  1. #include<stdio.h>
  2. void qsort(int v[] , int  start  , int end)
  3. {
  4.     int i,last;
  5.     if(start>=end)
  6.     {
  7.         return ;                                                 //exit
  8.  
  9.     }
  10.     Swap( v ,  start , (start+end)/2);
  11.     last= start;                                                        //last==start==0;
  12.  
  13.     for(i=start+1;i<=end;i++)
  14.     {
  15.         if(v[i]<v[start])                                          //elements less than pivot coming left ,grater than pivot right the pivot
  16.         {
  17.             Swap(v, ++start, i);
  18.  
  19.         }
  20.  
  21.     }
  22.     printf("\n");
  23.     Swap(v, start , last);
  24.     qsort(v,start, last-1);          //recursive call for the segment left of the partition index
  25.     qsort(v,last+1, end);          //recursive call for the segment right of the partition index
  26.  
  27. }
  28. void Swap(int v[],int i,int j)
  29. {
  30.     int temp;
  31.     temp=v[i];
  32.     v[i]=v[j];
  33.     v[j]=temp;
  34. }
  35. int main()
  36. {
  37.     int n,i;
  38.     printf("Display your Array size\n");
  39.     scanf("%d",&n);
  40.     int v[n];
  41.     printf("display your array elements  \n\n");
  42.     for(i=0;i<n;i++)
  43.     {
  44.         scanf("%d",&v[i]);
  45.     }
  46.     i=0;
  47.     qsort(v, i, n-1);
  48.     printf("Observe your sorting array  . it's working very well !!!, isn't it??\n\n");
  49.     for(i=0;i<n;i++)
  50.     {
  51.         printf("%d  ",v[i]);
  52.     }
  53.     printf("\n\n");
  54.  
  55.     return 0;
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement