fork download
  1.  
  2. #include <cstdlib>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. const int sz = 20;
  8. int arr[sz];
  9.  
  10. void printArray(int l=0, int r=sz-1) {
  11. for (int i=l; i<=r; i++) {
  12. cout<<arr[i]<<" ";
  13. }
  14. cout<<endl;
  15. }
  16.  
  17. int random(int a, int b) {
  18. return a + rand() % (b - a + 1);
  19. }
  20.  
  21. void quickSort(int *arr, int l, int r) {
  22. if (l >= r) return;
  23.  
  24. int i = l, j = r;
  25. int X = arr[random(l, r)];
  26.  
  27. while (i < j) {
  28. while (arr[i] < X) i++;
  29. while (arr[j] > X) j--;
  30. if (i <= j) swap(arr[i++], arr[j--]);
  31. }
  32.  
  33. quickSort(arr, i, r);
  34. quickSort(arr, l, j);
  35. }
  36.  
  37.  
  38. bool check() {
  39. for (int i=0; i<sz-1; i++){
  40. if (arr[i] > arr[i + 1]) return false;
  41. }
  42. return true;
  43. }
  44.  
  45. int main(){
  46. for (int i=0; i<sz; i++) {
  47. arr[i] = random(1, 100);
  48. }
  49.  
  50. cout<<"Original array: \n";
  51. printArray();
  52.  
  53. quickSort(arr, 0, sz - 1);
  54.  
  55. cout<<"Sorted array: \n";
  56. printArray();
  57.  
  58. cout<<(check() ? "OK": "FAIL");
  59.  
  60. return 0;
  61. }
  62.  
Success #stdin #stdout 0.01s 5504KB
stdin
Standard input is empty
stdout
Original array: 
84 87 78 16 94 36 87 93 50 22 63 28 91 60 64 27 41 27 73 37 
Sorted array: 
16 22 27 27 28 36 37 41 50 60 63 64 73 78 84 87 87 91 93 94 
OK