fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. typedef bool Cmp(int a,int b);
  5. bool up(int a,int b) { return a<b; }
  6. bool dn(int a,int b) { return a>b; }
  7. void bubleSort(int tb[],size_t size,Cmp *cmp)
  8. {
  9. for(size_t last=0,stop=size;stop>0;stop=last,last=0)
  10. {
  11. for(size_t i=1;i<stop;++i)
  12. {
  13. if(cmp(tb[i-1],tb[i]))
  14. {
  15. int tmp=tb[i-1];
  16. tb[i-1]=tb[i];
  17. tb[i]=tmp;
  18. last=i;
  19. }
  20. }
  21. }
  22. }
  23.  
  24. void show(int tb[],size_t size)
  25. {
  26. cout<<"{";
  27. for(size_t i=0;i<size;++i) cout<<(", "+!i)<<tb[i];
  28. cout<<" }\n";
  29. }
  30.  
  31. int main()
  32. {
  33. int tb[]={8,5,9,2,3,1,4,7,6};
  34. show(tb,sizeof(tb)/sizeof(*tb));
  35. bubleSort(tb,sizeof(tb)/sizeof(*tb),up);
  36. show(tb,sizeof(tb)/sizeof(*tb));
  37. bubleSort(tb,sizeof(tb)/sizeof(*tb),dn);
  38. show(tb,sizeof(tb)/sizeof(*tb));
  39. return 0;
  40. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
{ 8, 5, 9, 2, 3, 1, 4, 7, 6 }
{ 9, 8, 7, 6, 5, 4, 3, 2, 1 }
{ 1, 2, 3, 4, 5, 6, 7, 8, 9 }