fork(1) download
  1. #include <iostream>
  2.  
  3. template<typename T, typename Cmp1, typename Cmp2>
  4. void bsort_if(T a[], unsigned n, Cmp1 cmp1, Cmp2 cmp2){
  5. bool next;
  6. unsigned i, j;
  7. do {
  8. next = false;
  9. for(i = j = 0; i < n; i = j){
  10. while((i < n) && !cmp1(a[i]))
  11. ++i;
  12.  
  13. j = i + 1;
  14. while((j < n) && !cmp1(a[j]))
  15. ++j;
  16.  
  17. if((j < n) && cmp2(a[j], a[i])){
  18. std::swap(a[j], a[i]);
  19. next = true;
  20. }
  21. }
  22. } while(next);
  23. }
  24.  
  25. int main(void){
  26. int a[] = { -1, 3, -7, 6, 5, -1, 7, 4, 1, -4, 8, 9, -5, 2 };
  27. unsigned n = sizeof(a)/sizeof(a[0]);
  28.  
  29. bsort_if(a, n,
  30. [] (int n) { return (n > 0); },
  31. [] (int a, int b) { return (a < b); }
  32. );
  33.  
  34. for(unsigned i = 0; i < n; ++i)
  35. std::cout << a[i] << ' ';
  36. return 0;
  37. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
-1 1 -7 2 3 -1 4 5 6 -4 7 8 -5 9