fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <chrono>
  4. using namespace std;
  5.  
  6. struct Timer {
  7. std::chrono::high_resolution_clock::time_point start;
  8. Timer() : start(std::chrono::high_resolution_clock::now()) {}
  9. ~Timer() {
  10. auto stop = std::chrono::high_resolution_clock::now();
  11. auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start);
  12. std::cout << "Time: " << duration.count() << " ms\n";
  13. }
  14. };
  15. template<typename T>
  16.  
  17. void Firstalg(T x[], int &a, int &b, int n){
  18. a=0;
  19. b=0;
  20. for(int i=0;i<n;i++){
  21. for(int j=0; j<n-i-1; j++){
  22. a+=1;
  23. if(x[j]>x[j+1]){
  24. swap(x[j],x[j+1]);
  25. b+=1;
  26. }
  27. }
  28. }
  29. }
  30.  
  31. template<typename T>
  32. void Secondalg(T &x, int &a, int &b, int n){
  33. a=0;
  34. b=0;
  35. for(int i=1; i<n; i++){
  36. int key = x[i];
  37. int j=i-1;
  38. while(j>=0){
  39. a+=1;
  40. if(x[j]>key){
  41. x[j+1]=x[j];
  42. b+=1;
  43. j--;
  44. }else{break;}
  45. }
  46. x[j+1]=key;
  47. }
  48. }
  49.  
  50. int main() {
  51. int sort, comp;
  52. int n = 10000;
  53. int x[n];
  54. for (int i=0; i<n; i++){
  55. x[i]=rand() % 10001;
  56. };
  57.  
  58. for (int i=0; i<100; i++){
  59. cout<<x[i]<<" ";
  60. };
  61. cout<<endl;
  62. {
  63. Timer t;
  64. Firstalg(x,sort,comp,n);
  65. }
  66.  
  67. }
  68. /* Secondalg(x,sort,comp,n);
  69. for (int i=0; i<n; i++){
  70. cout<<x[i]<<" ";
  71. };
  72. cout<<endl;
  73. cout<<"Кількість порівнянь "<<sort<<endl<<"Кількість перестановок "<<comp;
  74.  
  75. Firstalg(x,sort,comp,n);
  76. for (int i=0; i<n; i++){
  77. cout<<x[i]<<" ";
  78. };
  79. cout<<endl;
  80. cout<<"Кількість порівнянь "<<sort<<endl<<"Кількість перестановок "<<comp;*/
Success #stdin #stdout 0.11s 5308KB
stdin
Standard input is empty
stdout
8973 6202 4625 5469 2038 5916 3405 5533 7004 2469 9853 4992 361 9819 3294 7195 4036 9404 8767 5404 1711 3214 3100 3751 2139 5437 4993 1759 9572 6270 3789 9623 2472 9493 6170 5589 5408 9576 2200 2411 3123 2052 8482 3484 2949 2855 1758 6985 3337 524 3468 5048 4818 6568 8800 6957 3083 4871 8716 3733 1140 2504 3356 3612 3076 604 279 8484 1258 2479 1974 5461 5610 455 8945 8560 4389 1781 6623 7727 3385 1169 2774 8203 7737 2652 6238 820 7523 4953 4553 8664 8536 8988 3354 1611 671 3634 1174 1929 
Time: 107 ms