Advertisement
p0lich

task1(OA)

Feb 14th, 2017
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <ctime>
  5.  
  6. using namespace std;
  7.  
  8. void countSort(int *a, int dif, int min, int n)
  9. {
  10. int *CountSort = new int[dif];
  11.  
  12. for (int i = 0; i < dif; i++)
  13. CountSort[i] = 0;
  14.  
  15. for (int i = 0; i < n; i++)
  16. CountSort[a[i] - min]++;
  17.  
  18. //for (int i = 0; i < dif; i++)
  19. // for (int j = 0; j < CountSort[i]; j++)
  20. // cout << i + min << " ";
  21. cout << endl << endl;
  22. }
  23.  
  24. void dischargeSort(int* a, int n, int max)
  25. {
  26. int* DischargeSort = new int [n];
  27. int k = 0;
  28. int s = 0;
  29. int num = 1;
  30. while (max > 0)
  31. {
  32. max = max/10;
  33. k++;
  34. }
  35.  
  36. for (int i = 0; i < k; i++)
  37. {
  38. for (int j = 0; j < 10; j++)
  39. {
  40. for (int z = 0; z < n; z++)
  41. {
  42. if ((a[z]/num)%10 == j)
  43. {
  44. DischargeSort[s] = a[z];
  45. s++;
  46. }
  47. }
  48. }
  49. num *= 10;
  50. s = 0;
  51. }
  52.  
  53. //for (int i = 0; i < n; i ++)
  54. // cout << DischargeSort[i] << " ";
  55. //cout << endl << endl;
  56. }
  57.  
  58. int main()
  59. {
  60. setlocale(LC_ALL,"RUS");
  61.  
  62. int n, max, min;
  63. cin >> n;
  64.  
  65. min = 1000;
  66. max = -1;
  67.  
  68. int *a = new int[n];
  69. srand ((unsigned int)time(NULL)); // начальная точка генерации
  70. cout << "Исходный массив" << endl;
  71. for (int i = 0; i < n; i++)
  72. {
  73. a[i] = rand()%100;
  74. cout << a[i] << " ";
  75. if (a[i] > max)
  76. max = a[i];
  77. if (a[i] < min)
  78. min = a[i];
  79. }
  80. int dif = max - min + 1;
  81. cout << endl << endl;
  82.  
  83. cout << "Сортировка Подсчётом" << endl;
  84. countSort(a,dif,min,n);
  85.  
  86. cout << "Поразрядная сортировка(LSD)" << endl;
  87. dischargeSort(a,n,max);
  88.  
  89. cout << "Оценка сложности" << endl << endl;
  90.  
  91. n = 10;
  92. unsigned int start, end;
  93. unsigned int* count = new unsigned int [5];
  94. unsigned int* discharge = new unsigned int [5];
  95. int s = 0;
  96. for (int i = 0; i < 5; i++)
  97. {
  98. count[i] = 0;
  99. discharge[i] = 0;
  100. }
  101.  
  102. for (n; n < 1000000; n *= 10)
  103. cout << n << endl;
  104.  
  105. for (n; n < 1000000; n *= 10)
  106. {
  107. start = clock();
  108. countSort(a,dif,min,n);
  109. end = clock();
  110. count[s] = end - start;
  111. s++;
  112. }
  113.  
  114. for (n; n < 1000000; n += 10)
  115. {
  116. start = clock();
  117. dischargeSort(a,n,max);
  118. end = clock();
  119. discharge[s] = end - start;
  120. s++;
  121. }
  122.  
  123. cout << "Сортировка Подсчётом" << endl;
  124. for (int i = 0; i < 5; i++)
  125. cout << count[i] << " ";
  126. cout << endl << endl;
  127.  
  128. cout << "Поразрядная сортировка(LSD)" << endl;
  129. for (int i = 0; i < 5; i++)
  130. cout << discharge[i] << " ";
  131. cout << endl << endl;
  132.  
  133.  
  134. system("pause");
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement