fork download
  1. #include<iostream>
  2. using namespace std;
  3.  
  4.  
  5. int partition( int arr[], int s, int e) {
  6.  
  7. int pivot = arr[s];
  8.  
  9. int cnt = 0;
  10. for(int i = s+1; i<=e; i++) {
  11. if(arr[i] <=pivot) {
  12. cnt++;
  13. }
  14. }
  15.  
  16. //place pivot at right position
  17. int pivotIndex = s + cnt;
  18. swap(arr[pivotIndex], arr[s]);
  19.  
  20. //left and right wala part smbhal lete h
  21. int i = s, j = e;
  22.  
  23. while(i < pivotIndex && j > pivotIndex) {
  24.  
  25. while(arr[i] <= pivot)
  26. {
  27. i++;
  28. }
  29.  
  30. while(arr[j] > pivot) {
  31. j--;
  32. }
  33.  
  34. if(i < pivotIndex && j > pivotIndex) {
  35. swap(arr[i++], arr[j--]);
  36. }
  37.  
  38. }
  39.  
  40. return pivotIndex;
  41.  
  42. }
  43.  
  44. void quickSort(int arr[], int s, int e) {
  45.  
  46. //base case
  47. if(s >= e)
  48. return ;
  49.  
  50. //partitioon karenfe
  51. int p = partition(arr, s, e);
  52.  
  53. //left part sort karo
  54. quickSort(arr, s, p-1);
  55.  
  56. //right wala part sort karo
  57. quickSort(arr, p+1, e);
  58.  
  59. }
  60.  
  61. int main() {
  62.  
  63. int arr[10] = {2,4,1,6,9 ,9,9,9,9,9};
  64. int n = 10;
  65.  
  66. quickSort(arr, 0, n-1);
  67.  
  68. for(int i=0; i<n; i++)
  69. {
  70. cout << arr[i] << " ";
  71. } cout << endl;
  72.  
  73.  
  74. return 0;
  75. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
1 2 4 6 9 9 9 9 9 9