fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. int comparisons=0,swaps=0;
  5. int*CombSort(int*input,int input_size){
  6. int gap=input_size;
  7. double shrink=1.3;
  8. bool swapped;
  9. while (!( gap==1 and swapped==false )){//repeate until gap==1 and swapped==false
  10. gap=int(gap/shrink);
  11. if(gap<1)
  12. {
  13. gap=1;
  14. }//end if
  15. int i=0;
  16. swapped=false;
  17. while(!( i+gap>=input_size )){
  18. comparisons++;
  19. if(input[i]>input[i+gap]){
  20. swap(input[i],input[i+gap]);
  21. swaps++;
  22. swapped=true;
  23. }//end if
  24. i=i+1;
  25. }//end while
  26. }//end while
  27. return input;
  28. }//end combSort
  29. int main()
  30. {
  31. int A[10]={10,2,3,4,5,6,7,8,9,1};
  32. for(int i=0;i<10;i++)
  33. cout<<A[i]<<" ";
  34. cout<<endl;
  35. CombSort(A,10);
  36. cout<<"comparsisons="<<comparisons<<" swaps="<<swaps<<endl;
  37. for(int i=0;i<10;i++)
  38. cout<<A[i]<<" ";
  39. cout<<endl;
  40. return 0;
  41. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
10 2 3 4 5 6 7 8 9 1 
comparsisons=50 swaps=13
1 2 3 4 5 6 7 8 9 10