Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #define N 100000
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include <fstream>
  7. using namespace std;
  8. void insertion_sort(int a[], int n);
  9. void selection_sort(int a[], int n);
  10. void IS(int n);
  11. void SS(int n);
  12. double best_IS=0, worst_IS=0, avarage_IS=0,best_SS=0,worst_SS=0,avarage_SS=0;
  13.  
  14. int main()
  15. {
  16. ofstream fout("lista.txt");
  17. srand(time(0));
  18. int n = 100000;
  19. IS(n);
  20. SS(n);
  21. return 0;
  22. }
  23.  
  24. void insertion_sort(int a[], int n)
  25. {
  26. int v;
  27. for (int i = 0; i <= n - 1; ++i)
  28. {
  29. for (int j = i - 1; j >= 0; --j)
  30. {
  31. if (a[j] > a[j + 1])
  32. {
  33. //swap(a[j], a[j + 1]);
  34. v = a[j]; a[j] = a[j + 1]; a[j + 1] = v;
  35. }
  36. }
  37. }
  38. }
  39. void selection_sort(int a[], int n)
  40. {
  41. int min_idx,v;
  42. for (int i = 0; i < n - 1; ++i) {
  43. min_idx = i;
  44. for (int j = i + 1; j < n; ++j) {
  45. if (a[j] > a[min_idx]) {
  46. min_idx = j;
  47. }
  48. }
  49. v = a[i]; a[i] = a[min_idx]; a[min_idx] = v;
  50. // swap(a[i], a[min_idx]);
  51. }
  52. }
  53. void IS(int n) {
  54. int i;
  55. int a[N];
  56. clock_t start, stop;
  57. double best, worst, avarage, ido;
  58. cout << "Insertion sort.\n\nWorking with array lenght of: " << n << endl;
  59. cout << "Worst case generation started..." << endl;
  60. for (i = 0; i < n; ++i)
  61. {
  62. a[i] = n - i - 1;
  63. }
  64. cout << "Worst case generation finished, now sorting..." << endl;
  65. start = clock();
  66. insertion_sort(a, n);
  67. stop = clock();
  68. ido = stop - start;
  69. ido /= CLOCKS_PER_SEC;
  70. worst = ido;
  71. cout << "Finished. Time: " << worst << endl;
  72. cout << "\nBest case generation started..." << endl;
  73. for (i = 0; i < n; ++i)
  74. {
  75. a[i] = i;
  76. }
  77. cout << "Best case generation finished, now sorting..." << endl;
  78. start = clock();
  79. insertion_sort(a, n);
  80. stop = clock();
  81. ido = stop - start;
  82. ido /= CLOCKS_PER_SEC;
  83. best = ido;
  84. cout << "Finished. Time: " << best << endl;
  85. cout << "\nAvarage case generation started..." << endl;
  86. for (i = 0; i < n; ++i) {
  87. a[i] = rand() % n;
  88. }
  89. cout << "Avarage case generation finished, now sorting..." << endl;
  90. start = clock();
  91. insertion_sort(a, n);
  92. stop = clock();
  93. ido = stop - start;
  94. ido /= CLOCKS_PER_SEC;
  95. avarage = ido;
  96. cout << "Finished. Time: " << avarage << endl;
  97.  
  98. cout << "\nWorst: " << worst << endl << "Best: " << best << endl << "Avarage: " << avarage << endl << endl;
  99. best_IS = best;
  100. avarage_IS = avarage;
  101. worst_IS = worst;
  102. }
  103. void SS(int n) {
  104. int i;
  105. int a[N];
  106. clock_t start, stop;
  107. double best, worst, avarage, ido;
  108. cout << "Selection sort.\n\nWorking with array lenght of: " << n << endl;
  109. cout << "Worst case generation started..." << endl;
  110. for (i = 0; i < n; ++i)
  111. {
  112. a[i] = n - i - 1;
  113. }
  114. cout << "Worst case generation finished, now sorting..." << endl;
  115. start = clock();
  116. selection_sort(a, n);
  117. stop = clock();
  118. ido = stop - start;
  119. ido /= CLOCKS_PER_SEC;
  120. worst = ido;
  121. cout << "Finished. Time: " << worst << endl;
  122. cout << "\nBest case generation started..." << endl;
  123. for (i = 0; i < n; ++i)
  124. {
  125. a[i] = i;
  126. }
  127. cout << "Best case generation finished, now sorting..." << endl;
  128. start = clock();
  129. selection_sort(a, n);
  130. stop = clock();
  131. ido = stop - start;
  132. ido /= CLOCKS_PER_SEC;
  133. best = ido;
  134. cout << "Finished. Time: " << best << endl;
  135. cout << "\nAvarage case generation started..." << endl;
  136. for (i = 0; i < n; ++i) {
  137. a[i] = rand() % n;
  138. }
  139. cout << "Avarage case generation finished, now sorting..." << endl;
  140. start = clock();
  141. selection_sort(a, n);
  142. stop = clock();
  143. ido = stop - start;
  144. ido /= CLOCKS_PER_SEC;
  145. avarage = ido;
  146. cout << "Finished. Time: " << avarage << endl;
  147.  
  148. cout << "\nWorst: " << worst << endl << "Best: " << best << endl << "Avarage: " << avarage << endl<<endl;
  149. best_IS = best;
  150. avarage_IS = avarage;
  151. worst_IS = worst;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement