zero_shubham1

quick_sort

Dec 27th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.85 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. void quicksort(int arr[], int low, int high){
  4.     if(low>=high)
  5.         return;
  6.     int i;
  7.     printf("\n");
  8.     for(i=0;i<10;i++)
  9.         printf("%d\t",arr[i]);
  10.     int part = partition(arr,low,high);
  11.     quicksort(arr,low,part-1);
  12.     quicksort(arr,part+1,high);
  13. }
  14.  
  15. int partition(int arr[], int l, int h){
  16.     int pi = h;
  17.     int i=pi, j, tmp;
  18.  
  19.     for(j=i-1;j>=l;j--){
  20.         if(arr[j]>arr[pi]){
  21.             --i;
  22.             tmp = arr[j];
  23.             arr[j] = arr[i];
  24.             arr[i] = tmp;
  25.             }
  26.         }
  27.         tmp = arr[i];
  28.         arr[i] = arr[pi];
  29.         arr[pi] = tmp;
  30.  
  31.         return i;
  32.     }
  33.  
  34.  
  35. int main(){
  36.  
  37.     int arr[10] ={70,90,40,30,15,25,20,10,67,5};
  38.     quicksort(arr,0,9);
  39.     int i;
  40.     printf("\n");
  41.     for(i=0;i<10;i++)
  42.         printf("%d\t",arr[i]);
  43.     return 1;
  44. }
Add Comment
Please, Sign In to add comment