fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void display_arr(int *an_array){
  5. int i;
  6. for (i = 0; i < 9; i++){
  7. cout << an_array[i] << ", ";
  8. }
  9. cout << endl;
  10. }
  11.  
  12. int partition(int array[], int l, int h){
  13. int i, p, firsthigh;
  14. p = h;
  15. firsthigh = l;
  16. display_arr(array);
  17. for (i = l; i < h; i++){
  18. if (array[i] < array[p]){
  19. cout << i << ", " << h << ", " << firsthigh << endl;
  20. swap(array[i], array[firsthigh]);
  21. firsthigh++;
  22. display_arr(array);
  23. }
  24. }
  25. swap(array[p], array[firsthigh]);
  26. return firsthigh;
  27. }
  28.  
  29. void quicksort(int array[], int l, int h){
  30. int p;
  31. if ((h-l) > 0) {
  32. p = partition(array, l, h);
  33. quicksort(array, l, p-1);
  34. quicksort(array, p+1, h);
  35. }
  36. }
  37.  
  38.  
  39. int main() {
  40. int myarray[9];
  41. int i;
  42. for (i = 0; i < 9; i++){
  43. cin >> myarray[i];
  44. }
  45. quicksort(myarray, 0, 8);
  46. display_arr(myarray);
  47. return 0;
  48. }
Success #stdin #stdout 0s 3344KB
stdin
9
3
8
7
1
2
6
5
4
stdout
9, 3, 8, 7, 1, 2, 6, 5, 4, 
1, 8, 0
3, 9, 8, 7, 1, 2, 6, 5, 4, 
4, 8, 1
3, 1, 8, 7, 9, 2, 6, 5, 4, 
5, 8, 2
3, 1, 2, 7, 9, 8, 6, 5, 4, 
3, 1, 2, 4, 9, 8, 6, 5, 7, 
1, 2, 0
1, 3, 2, 4, 9, 8, 6, 5, 7, 
1, 2, 3, 4, 9, 8, 6, 5, 7, 
6, 8, 4
1, 2, 3, 4, 6, 8, 9, 5, 7, 
7, 8, 5
1, 2, 3, 4, 6, 5, 9, 8, 7, 
1, 2, 3, 4, 6, 5, 7, 8, 9, 
1, 2, 3, 4, 5, 6, 7, 8, 9, 
7, 8, 7
1, 2, 3, 4, 5, 6, 7, 8, 9, 
1, 2, 3, 4, 5, 6, 7, 8, 9,