fork download
  1. //Quick Sort
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4.  
  5. //element swapping
  6. void swap(int &a, int &b){
  7. int temp=a; a=b; b=temp;
  8. }
  9.  
  10. //Partition
  11. int partition(int arr[], int low, int high){
  12. int pivot = arr[high]; //last er ta pivot
  13. int i = low-1; //choto upadan er tracker
  14.  
  15. for (int j=low; j<high; j++){
  16. //pivot er choto
  17. if (arr[j]<pivot){
  18. i++;
  19. swap(arr[i], arr[j]);
  20. }
  21. }
  22.  
  23. swap(arr[i+1], arr[high]);
  24. return i+1; //pivot er index
  25. }
  26.  
  27. void quickSort(int arr[], int low, int high){
  28. if(low < high){
  29. int pIdx = partition(arr, low, high);
  30.  
  31. quickSort(arr, low, pIdx-1);
  32. quickSort(arr, pIdx+1, high);
  33. }
  34. }
  35.  
  36.  
  37. int main(){
  38. int arr[]={10, 80, 30, 90, 40, 50, 70};
  39. int n=7;
  40.  
  41. quickSort(arr, 0, n-1);
  42.  
  43. cout << "Quick sorted array: ";
  44.  
  45. for (int i = 0; i<n; i++){
  46. cout<<arr[i]<<" ";
  47. }
  48.  
  49. cout << endl;
  50. return 0;
  51. }
  52.  
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
Quick sorted array: 10 30 40 50 70 80 90