shawonrog

QUICKSORT PARTITION

Aug 3rd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.99 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. void quick_sort(int[],int,int);
  4. int partition(int[],int,int);
  5.  
  6. int main()
  7. {
  8.     int a[50],n,i;
  9.     printf("How many elements?");
  10.     scanf("%d",&n);
  11.     printf("\nEnter array elements:");
  12.  
  13.     for(i=0; i<n; i++)
  14.         scanf("%d",&a[i]);
  15.  
  16.     quick_sort(a,0,n-1);
  17.     printf("\nArray after sorting:");
  18.  
  19.     for(i=0; i<n; i++)
  20.         printf("%d ",a[i]);
  21.  
  22.     return 0;
  23. }
  24.  
  25. void quick_sort(int a[],int l,int u)
  26. {
  27.     int j;
  28.     if(l<u)
  29.     {
  30.         j=partition(a,l,u);
  31.         quick_sort(a,l,j-1);
  32.         quick_sort(a,j+1,u);
  33.     }
  34. }
  35.  
  36. int partition(int a[],int l,int u)
  37. {
  38.     int v,i,j,temp;
  39.     v=a[l];
  40.     i=l;
  41.     j=u+1;
  42.  
  43.     do
  44.     {
  45.         do
  46.             i++;
  47.  
  48.         while(a[i]<v&&i<=u);
  49.  
  50.         do
  51.             j--;
  52.         while(v<a[j]);
  53.  
  54.         if(i<j)
  55.         {
  56.             temp=a[i];
  57.             a[i]=a[j];
  58.             a[j]=temp;
  59.         }
  60.     }
  61.     while(i<j);
  62.  
  63.     a[l]=a[j];
  64.     a[j]=v;
  65.  
  66.     return(j);
  67. }
Add Comment
Please, Sign In to add comment