Advertisement
ivandrofly

Quick sort

Dec 24th, 2015
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. #include <stdio.h>
  2. #include<stdlib.h>
  3. int partition(int *a,int start,int end)
  4. {
  5.     int i,pivot,pi;    
  6.     pivot=a[end];
  7.     pi=start;
  8.     for(i=start;i<end;i++)
  9.     {
  10.         if(a[i]<pivot)
  11.         {
  12.             int temp=a[i];
  13.             a[i]=a[pi];
  14.             a[pi]=temp;
  15.             pi=pi+1;
  16.            
  17.         }
  18.        
  19.     }
  20.    return pi;
  21.    
  22. }
  23. void quicksort(int *a,int start,int end)
  24. {
  25.     if(start<end)
  26.    {
  27.      int pi=partition(a,start,end);
  28.     quicksort(a,start,pi-1);
  29.     quicksort(a,pi+1,end);
  30.    }
  31. }
  32. int main()
  33. {
  34.     int a[ ]={7,6,5,4,3,2,1,0};
  35.     quicksort(a,0,7);
  36.     for(int i=0;i<8;i++)
  37.     {
  38.         printf("%d\t",a[i]);
  39.     }
  40. }
  41.  
  42. // source: https://www.youtube.com/watch?v=COk73cpQbFQ
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement