Вы находитесь на странице: 1из 12

2

: . . TI-155 .
: . . ..

2016
:
:

1. . (HeapSort)
2. (InsertionSort)
3. (MergeSort)
4. (QuickSort)
5. . (CountingSort)
6. . (MSD RadixSort)
7. . (LSD RadixSort)

, . (TimSort, ShellSort,
BucketSort .).

1. .
2. ( )
: 67, 82, 36, 34, 55, 89, 4, 18, 44, 32.
3. ( ..
), .
n , 3
, .
4. .
5. ( ).

1.
:
CountingSort ( )
MergeSort ( )
InsertSort ( )

COUNTING_SORT(A,B,k)
1 for i 0 to k
2 do C[i] 0
3 for j 1 to length[A]
4 do C[A[j]] C[A[j]]+1
5 C[i] , i
6 for i 1 to k
7 do C[i] C[i]+C[i-1]
8 C[i]- , i
9 for j length[A] downto 1
10 do B[C[A[j]]] A[j]
11 C[A[j]] C[A[j]]-1

MERGE_SORT(A, p, r)
1 if p<r
2 then q ( p r ) / 2
3 MERGE_SORT(A, p, q);
4 MERGE_SORT(A,q+1,r);
5 MERGE(A,p,q,r);
MERGE,
, A[ p..r ] :
MERGE (A,p,q,r)
1 for i 1+q downto p+1
2 mas[i-1] A[i-1]
3 i i-1
4 for j q to r-1
5 mas[r+q-j] A[j+1]
6 j j+1
7 for k p to r
8 if mas[j]<mas[i]
9 then A[k] mas[j--]
10 else A[k] mas[i++]
MERGE , :
MERGE (A,p,q,r)
1 qq q++ count 1
2 while (count<p)
3 b[count] a[count++]
4 while ((p<=qq)&&(q<=r))
5 if (a[p]<a[q])
6 b[count++]=a[p++]
7 else
8 b[count++]=a[q++]
9 while (p<=qq)
10 b[count++]=a[p++]
11 while (q<=r)
12 b[count++]=a[q++]
13 B A

1. for j<-2 to length[A]


2. do key<-A[j]
3. A[J]
A
4. i<- j-1
5. while i>0 and A[i]>key
6. do A[i + 1] <-- A[i]
7. i := i - 1
8. A[i+ 1] := key
2.

67 82 36 34 55 89 4 18 44 32

1. (Counting sort)

0 . A C .
A:
67 82 36 34 55 89 4 18 44 32
C:

0 0 0 0 0 0 0 0 0 0 0 0 0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8 8 8 8 8 8 8 8 8 8 8 8 8
8 9 9 9 9 9 9 9 10-
>9
B:
4 18 32 34 36 44 55 89

18 .
A:
67 82 36 34 55 89 4 18 44 32
C:
0 0 0 1- 1 1 1 1 1 1 1 1 1 1 1 1 1 2- 2 2
>0 >1
2 2 2 2 2 2 2 2 2 2 2 3- 3 4- 4 5- 5 5 5 5
>2 >3 >4
5 5 5 6- 6 6 6 6 6 6 6 6 6 6 7- 7 7 7 7 7
>5 >6
7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 8 8 8 8 8
8 9- 9 9 9 9 9 9 10-
>8 >9
B:
4 18 32 34 36 44 55 67 82 89

A:
4 18 32 34 36 44 55 67 82 89
.

2.
:|67|82|36|34|55|89|4|18|44|32|
0 . :
|67|82|36|34|55| |89|4|18|44|32|
1 .
:
|67|82| |36|34|55|
2 . :
|67| |82|
3 . 1. :
|67|82|
4 . , 1:
|36| |34|55|
5 . = 1. . ,
4:
|34| |55|
6 . . :
|34|55|
7 . 6 4.
|34|36|55|
8 . , 7 , 3:
|34|36|55|67|82|
9 . , 0:
|89|4| |18|44|32|
10 . , 9:
|89| |4|
11. . .
|4|89|
12 . 9:
|18| |44|32|
13 . . .
|44| |32|
14. . :
|32|44|
15. , 14 , 12:
|18|32|44|
16 . , 16 11:
|4|18|32|44|89|
19 . , 15 8:
|4|18|32|34|36|44|55|67|82|89|
.

3.
0 .
67 82 36 34 55 89 4 18 44 32
1 .
36 67 82 34 55 89 4 18 44 32
2 .
34 36 67 82 55 89 4 18 44 32
3 .
34 36 55 67 82 89 4 18 44 32
4 .
4 34 36 55 67 82 89 18 44 32
5 .
4 18 34 36 55 67 82 89 44 32
6 .
4 18 34 36 44 55 74 76 81 15
7 .
14 15 28 44 49 56 57 74 76 81

3.
:
T(n) = (n)
T(n) = (n log )
T(n) = T(n) = (2 )

n 1000 5000 10000 20000 30000

Merge sort 0,005 0,015 0,03 0,026 0,035
Counting sort 0,0008 0,0012 0,002 0,0025 0,005
InsertSort 0,02 0,020 0,027 0,1 0,209

4. :


6. // lab_apa.cpp: .
7. //
8.
9. #include "stdafx.h"
10.
11.
12. #include <chrono>
13. #include <ostream>
14. #include <iostream>
15. #include <algorithm>
16. using namespace std;
17.
18. void merge(int a[], const int low, const int mid, const int high)
19. {
20. int * b = new int[high + 1 - low];
21. int h, i, j, k;
22. h = low;
23. i = 0;
24. j = mid + 1;
25. while ((h <= mid) && (j <= high))
26. {
27. if (a[h] <= a[j])
28. {
29. b[i] = a[h];
30. h++;
31. }
32. else
33. {
34. b[i] = a[j];
35. j++;
36. }
37. i++;
38. }
39. if (h>mid)
40. {
41. for (k = j; k <= high; k++)
42. {
43. b[i] = a[k];
44. i++;
45. }
46. }
47. else
48. {
49. for (k = h; k <= mid; k++)
50. {
51. b[i] = a[k];
52. i++;
53. }
54. }
55. for (k = 0; k <= high - low; k++)
56. {
57. a[k + low] = b[k];
58. }
59. delete[] b;
60.
61. }
62.
63. void merge_sort(int a[], const int low, const int high)
64. {
65. int mid;
66. if (low < high) {
67. mid = (low + high) / 2;
68. merge_sort(a, low, mid);
69. merge_sort(a, mid + 1, high);
70. merge(a, low, mid, high);
71. }
72. }
73. void insertion_sort(int arr[], int length) {
74. int i, j, tmp;
75. for (i = 1; i < length; i++) {
76. j = i;
77. while (j > 0 && arr[j - 1] > arr[j]) {
78. tmp = arr[j];
79. arr[j] = arr[j - 1];
80. arr[j - 1] = tmp;
81. j--;
82. }
83. }
84. }
85. void CountingSort(int arr[], int sz) {
86. int i, j, k;
87. int idx = 0;
88. int min, max;
89.
90. min = max = arr[0];
91. for (i = 1; i < sz; i++) {
92. min = (arr[i] < min) ? arr[i] : min;
93. max = (arr[i] > max) ? arr[i] : max;
94. }
95.
96. k = max - min + 1;
97. int *B = new int[k];
98. for (i = 0; i < k; i++) B[i] = 0;
99.
100. for (i = 0; i < sz; i++) B[arr[i] - min]++;
101. for (i = min; i <= max; i++)
102. for (j = 0; j < B[i - min]; j++) arr[idx++] = i;
103. delete[] B;
104. }
105. int main()
106. {
107. using namespace std::chrono;
108. int a1[5000], a2[5000], a3[5000];
109. int b1[16000], b2[16000], b3[16000];
110. for (int i = 0; i < 5000; i++) {
111. a3[i] = a2[i] = a1[i] = rand() % 10000 + 1;
112. }
113. for (int i = 0; i < 16000; i++) {
114. b3[i] = b2[i] = b1[i] = rand() % 320000 + 1;
115. }
116. int arraySize = 10;
117.
118. int a[10] = { 67, 82, 36, 34, 55, 89, 4, 18, 44, 32 };
119. int p[10] = { 67, 82, 36, 34, 55, 89, 4, 18, 44, 32 };
120. int sz = 10;
121. high_resolution_clock::time_point t1 = high_resolution_clock::now();
122. for (int i = 0; i < 10000; i++) {
123. int arr[10] = { 67, 82, 36, 34, 55, 89, 4, 18, 44, 32 };
124. insertion_sort(arr, 10);
125. }
126. high_resolution_clock::time_point t2 = high_resolution_clock::now();
127. for (int i = 0; i < 10000; i++) {
128. int a[10] = { 67, 82, 36, 34, 55, 89, 4, 18, 44, 32 };
129. merge_sort(a, 0, (arraySize - 1));
130. }
131. high_resolution_clock::time_point t3 = high_resolution_clock::now();
132. for (int i = 0; i < 10000; i++) {
133. int p[10] = { 67, 82, 36, 34, 55, 89, 4, 18, 44, 32 };
134. CountingSort(p, sz);
135. }
136. high_resolution_clock::time_point t4 = high_resolution_clock::now();
137. insertion_sort(a1, 5000);
138. high_resolution_clock::time_point t5 = high_resolution_clock::now();
139. merge_sort(a2, 0, (5000 - 1));
140. high_resolution_clock::time_point t6 = high_resolution_clock::now();
141. CountingSort(b3, 16000);
142. high_resolution_clock::time_point t7 = high_resolution_clock::now();
143. insertion_sort(b1, 16000);
144. high_resolution_clock::time_point t8 = high_resolution_clock::now();
145. merge_sort(b2, 0, (16000 - 1));
146. high_resolution_clock::time_point t9 = high_resolution_clock::now();
147. CountingSort(b3, 16000);
148. high_resolution_clock::time_point t10 = high_resolution_clock::now();
149.
150. duration<double> time_span = duration_cast<duration<double>>(t2 - t1);
151. duration<double> time_span1 = duration_cast<duration<double>>(t3 - t2);
152. duration<double> time_span2 = duration_cast<duration<double>>(t4 - t3);
153. duration<double> time_span3 = duration_cast<duration<double>>(t5 - t4);
154. duration<double> time_span4 = duration_cast<duration<double>>(t6 - t5);
155. duration<double> time_span5 = duration_cast<duration<double>>(t7 - t6);
156. duration<double> time_span6 = duration_cast<duration<double>>(t8 - t7);
157. duration<double> time_span7 = duration_cast<duration<double>>(t9 - t8);
158. duration<double> time_span8 = duration_cast<duration<double>>(t10 - t9);
159.
160.
161. cout << "Time for Insertion Sort " << time_span.count() / 10000 << " seconds."
<< endl;
162. cout << "Time for Merge Sort " << time_span1.count() / 10000 << " seconds." <<
endl;
163. cout << "Time for Counting Sort " << time_span2.count() / 10000 << " seconds."
<< endl << endl;
164. cout << "Time for Insertion 5000 " << time_span3.count() << " seconds." <<
endl;
165. cout << "Time for Merge Sort 5000 " << time_span4.count() << " seconds." <<
endl;
166. cout << "Time for Counting Sort 5000 " << time_span5.count() / 3.2 << "
seconds." << endl << endl;
167. cout << "Time for Insertion 16000 " << time_span6.count() << " seconds." <<
endl;
168. cout << "Time for Merge Sort 16000 " << time_span7.count() << " seconds." <<
endl;
169. cout << "Time for Counting Sort 16000 " << time_span8.count() << " seconds."
<< endl;
170.
171. system("pause");
172. return 0;
173. }

7.
, 3 :
Merge Sort, Counting Sort, Insertion Sort. ,
,
, ,
, .
, ,
, .
,
.

. :
, ,
. ,
,
, .
, Insertion Sort
. , ,
, , .
.
( ,
).

Оценить