fork download
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. void swap(int&a, int&b)
  7. {
  8. int t = a; a = b; b = t;
  9. }
  10.  
  11. void BubbleSort(int * a, int n)
  12. {
  13. for(int b = 0, e = n-1; b != e; ++b)
  14. for(int k = e, j = k--; j != b; j = k--)
  15. if (a[j] < a[k]) swap(a[j],a[k]);
  16. }
  17.  
  18.  
  19. void oddBubbleSort(int * a, int n)
  20. {
  21. for(int b = 0, e = ((n-1)/2)*2; b != e; b+=2)
  22. for(int k = e-2, j = e; j != b; j = k, k -= 2)
  23. if (a[j] < a[k]) swap(a[j],a[k]);
  24. }
  25.  
  26.  
  27. int main(int argc, char * argv[])
  28. {
  29. int a[] = { 9,8,7,6,5,4,3,2,1,0};
  30. oddBubbleSort(a,10);
  31.  
  32. for(int i: a) cout << i << " ";
  33. }
  34.  
  35.  
Success #stdin #stdout 0.01s 5380KB
stdin
Standard input is empty
stdout
1 8 3 6 5 4 7 2 9 0