• Source
    1. #include <iostream>
    2. #include <algorithm>
    3.  
    4. using namespace std;
    5.  
    6.  
    7. int partition(int array[], int left,int right){
    8. int pivot = array[right];
    9. int storeIndex = left;
    10. for(int i = left; i<right; i++){
    11. if(array[i]<pivot){
    12. swap(array[storeIndex],array[i]);
    13. storeIndex++;
    14. }
    15. }
    16. swap(array[storeIndex],array[right]);
    17. return storeIndex;
    18. }
    19.  
    20.  
    21. void quicksort(int a[],int i,int k){
    22. if(i<k){
    23. int p = partition(a,i,k);
    24. quicksort(a,i,p-1);
    25. quicksort(a,p+1,k);
    26. }
    27. }
    28.  
    29.  
    30. int main() {
    31. // array a to be sorted
    32. int a[] = {1,2,3,4,5,6,7,8,9,5,34,53,8};
    33.  
    34. //finds the size of array a
    35. int n = sizeof(a)/sizeof(a[0]);
    36.  
    37. //calls the quicksort function define above
    38. quicksort(a,0,n-1);
    39.  
    40. //prints the sorted array
    41. for(int i = 0; i<n; i++){
    42. cout<<a[i]<<" ";
    43. }
    44.  
    45. return 0;
    46. }