fork download
  1. #include <iostream>
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4.  
  5. int partition(int a[],int low,int high);
  6.  
  7. void quicksort(int a[], int low ,int high){
  8. if(low < high){
  9. int pivot=partition(a,low,high);
  10. quicksort(a,low,pivot-1);
  11. quicksort(a,pivot+1,high);
  12. }
  13. }
  14. int partition(int a[],int low,int high){
  15. int piv=a[high];
  16. int i=low-1;
  17. for(int j=low;j<high;j++){
  18. if(a[j]<=piv){
  19. i++;
  20. swap(a[i],a[j]);
  21. }
  22. }
  23. swap(a[i+1],a[high]);
  24. return i+1;
  25. }
  26.  
  27. int main() {
  28. // your code goes here
  29. int n;
  30. cin>>n;
  31. int a[n];
  32. for(int i=0;i<n;i++){
  33. cin>>a[i];
  34. }
  35. quicksort(a,0,n-1);
  36. for(int i=0;i<n;i++){
  37. cout<<a[i]<<" ";
  38. }
  39. return 0;
  40. }
Success #stdin #stdout 0s 5272KB
stdin
9
4 2 8 9 5 1 88 12 9
stdout
1 2 4 5 8 9 9 12 88