Advertisement
daniil_mironoff

Untitled

Feb 22nd, 2021
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <chrono>
  4.  
  5. using namespace std;
  6.  
  7. void display(int *array, int size) {
  8. for(int i = 0; i < size; i++) {
  9. cout << array[i] << " ";
  10. }
  11.  
  12. cout << '\n';
  13. }
  14.  
  15. void countSort(int *array, int size) {
  16.  
  17. int output[size]; // Буффер
  18. int max = array[0]; // Максимальное значение
  19.  
  20. // Находим макс. значение
  21. for (int i = 1; i < size; i++) {
  22. if (array[i] > max) {
  23. max = array[i];
  24. }
  25. }
  26.  
  27. // Массив-счётчик
  28. int count[max + 1];
  29.  
  30. // Записываем нули в массив-счётчик
  31. for (int i = 0; i <= max; ++i) {
  32. count[i] = 0;
  33. }
  34.  
  35. // Записываем значения в счётчик
  36. for (int i = 0; i < size; i++) {
  37. count[array[i]]++; // увеличичение count
  38. }
  39.  
  40. // Суммирование в массиве-счётчике
  41. for (int i = 1; i <= max; i++) {
  42. count[i] += count[i - 1];
  43. }
  44.  
  45. // Увеличение coy
  46. for (int i = size - 1; i >= 0; i--) {
  47. output[count[array[i]] - 1] = array[i];
  48. count[array[i]]--; // уменьшение count
  49. }
  50.  
  51. // Записываем из буфера в результивный массив
  52. for (int i = 0; i < size; i++) {
  53. array[i] = output[i];
  54. }
  55. }
  56.  
  57. int main(int argc, char **argv) {
  58. srand(time(NULL));
  59. setlocale(LC_ALL, "rus");
  60.  
  61. unsigned int size;
  62. unsigned int ans;
  63. fstream Ifile;
  64.  
  65. // Меню
  66. cout << "Выберите размер сортируемого массива:\n"
  67. "1. 16\n2. 100\n3. 500\n4. 1000\n5. 5000\n";
  68. cin >> ans;
  69.  
  70. switch (ans) {
  71. case 1:
  72. size = 16;
  73. Ifile.open("d16.txt");
  74. break;
  75. case 2:
  76. size = 100 + 2;
  77. Ifile.open("d100.txt");
  78. break;
  79. case 3:
  80. size = 500 + 2;
  81. Ifile.open("d500.txt");
  82. break;
  83. case 4:
  84. size = 1000 + 2;
  85. Ifile.open("d1000.txt");
  86. break;
  87. case 5:
  88. size = 5000 + 2;
  89. Ifile.open("d5000.txt");
  90. break;
  91.  
  92. default:
  93. cout << "Ошибка выбора размера массива\n";
  94. return 1;
  95. }
  96.  
  97. // Считывание из файла в массив
  98. int* arr = new int[size];
  99. for (int i = 0; i < size; i++) {
  100. Ifile >> arr[i];
  101. }
  102.  
  103. // Вывод массива ДО сортировки
  104. printf("Массив до сортировки: ");
  105. display(arr, size);
  106.  
  107. // Засекаем время до сортировки
  108. std::chrono::steady_clock::time_point started = std::chrono::high_resolution_clock::now();
  109.  
  110. // Сортировка
  111. countSort(arr, size);
  112.  
  113. // Запоминаем время сортировки
  114. std::chrono::steady_clock::time_point done = std::chrono::high_resolution_clock::now();
  115.  
  116. // Вывод массива ПОСЛЕ сортировки
  117. printf("Массив после сортировки: ");
  118. display(arr, size);
  119.  
  120. // Вывод время сортировки
  121. cout << "Время сортировки: " << std::chrono::duration_cast<std::chrono::microseconds>(done-started).count() << " мксек." << endl;
  122.  
  123. // Запись в новый файл
  124. ofstream Ofile;
  125.  
  126. switch (ans) {
  127. case 1:
  128. Ofile.open("d16-s.txt", ios::trunc);
  129. break;
  130. case 2:
  131. Ofile.open("d100-s.txt", ios::trunc);
  132. break;
  133. case 3:
  134. Ofile.open("d500-s.txt", ios::trunc);
  135. break;
  136. case 4:
  137. Ofile.open("d1000-s.txt", ios::trunc);
  138. break;
  139. case 5:
  140. Ofile.open("d5000-s.txt", ios::trunc);
  141. break;
  142. }
  143.  
  144. Ofile << arr[0];
  145.  
  146. for (int i = 1; i < size; i++) {
  147. Ofile << endl << arr[i];
  148. }
  149.  
  150.  
  151. Ofile.close();
  152. delete[] arr;
  153.  
  154. return 0;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement