fork download
  1. //quicksort with lomuto partition
  2.  
  3. void qs(int a[], int l, int r){
  4. if(l < r){
  5. int i = l - 1, x = a[r];
  6. for(int j = l ; j < r; j++){
  7. if(a[j] < x){
  8. ++i;
  9. swap(a[i], a[j]);
  10. }
  11. }
  12. ++i;
  13. swap(a[i], a[r]);
  14. qs(a, l, i - 1);
  15. qs(a, i + 1, r);
  16. }
  17. }
  18.  
  19. //quicksort with hoare partition, pivot is the last element of segment
  20. void qs2(int a[], int l, int r){
  21. int i = l, j = r, x = a[r];
  22. while(i <= j){
  23. while(a[i] < x) ++i;
  24. while(a[j] > x) --j;
  25. if(i <= j){
  26. swap(a[i], a[j]);
  27. ++i; --j;
  28. }
  29. }
  30. if(i < r) qs2(a, i, r);
  31. if(l < j) qs2(a, l, j);
  32. }
  33.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘void qs(int*, int, int)’:
prog.cpp:9:5: error: ‘swap’ was not declared in this scope
     swap(a[i], a[j]);
     ^~~~
prog.cpp:13:3: error: ‘swap’ was not declared in this scope
   swap(a[i], a[r]);
   ^~~~
prog.cpp: In function ‘void qs2(int*, int, int)’:
prog.cpp:26:4: error: ‘swap’ was not declared in this scope
    swap(a[i], a[j]);
    ^~~~
stdout
Standard output is empty