You are on page 1of 6

23:29 14/01/2024 Test.

cpp

Test.cpp

1 // BAI 1
2 #include <stdio.h>
3 #include<stdlib.h>
4 #include<time.h>
5 #include<math.h>
6
7 #define N 10
8 #define Min -2*N
9 #define Max 2*N
10 #define L_MAX N*N/2
11 #define R_MAX N*N
12
13 void swap(int* a, int* b) {
14 int t = *a;
15 *a = *b;
16 *b = t;
17 }
18
19 int partition(int arr[], int left, int right, int* leftShift, int* rightShift) {
20 int pivot = right;
21 right--;
22 while(true) {
23 while(arr[left] < arr[pivot]) {
24 left++;
25 (*leftShift)++;
26 }
27 while(arr[right] > arr[pivot] && left <= right) {
28 right--;
29 (*rightShift)++;
30 }
31 if (left >= right) {
32 break;
33 }
34 swap(&arr[left], &arr[right]);
35 left++;
36 right--;
37 (*leftShift)++;
38 (*rightShift)++;
39 }
40 swap(&arr[left], &arr[pivot]);
41 return left;
42 }
43
44 void printArray(int arr[], int begin, int end) {
45 for (int i = begin; i < end; i++) {
46 printf("%d ", arr[i]);
47 }
48 printf("\n");
49 }
50
51 void quickSort(int arr[], int left, int right, int* leftShift, int* rightShift) {
52 *leftShift = 0; *rightShift = 0;
53 if (left < right) {

localhost:56264/1f71acb7-88b3-4594-80a1-a13aff229926/ 1/6
23:29 14/01/2024 Test.cpp
54 int i, j;
55 int pi = partition(arr, left, right, leftShift, rightShift);
56 quickSort(arr, left, pi - 1, &i, &j);
57 *leftShift += i;
58 *rightShift += j;
59 quickSort(arr, pi + 1, right, &i, &j);
60 *leftShift += i;
61 *rightShift += j;
62 }
63 }
64
65 int isSorted(int n, int arr[]) {
66 for (int i = 0; i < n - 1; i++) {
67 if (arr[i] > arr[i + 1]) {
68 return 0;
69 }
70 }
71 return 1;
72 }
73
74 void initializeArray(int n, int arr[]) {
75 for (int i = 0; i < n; i++) {
76 arr[i] = Min + rand()%(Max - Min + 1);
77 }
78 }
79
80 void createFrequencyTable(int n, int arr[], int k, int leftShift[], int rightShift[]) {
81 for (int i = 0; i < L_MAX; i++) {
82 leftShift[i] = 0;
83 }
84 for (int i = 0; i < R_MAX; i++) {
85 rightShift[i] = 0;
86 }
87 int temp_l, temp_r;
88 for (int i = 0; i < k; i++) {
89 initializeArray(n, arr);
90 quickSort(arr, 0, n - 1, &temp_l, &temp_r);
91 leftShift[temp_l]++;
92 rightShift[temp_r]++;
93 }
94 }
95
96 void printFrequencyTable(int leftShift[], int rightShift[]) {
97 printf("Left shift:\n");
98 for (int i = 0; i < L_MAX; i++) {
99 if (leftShift[i] != 0) {
100 printf("%d| %d\n", i, leftShift[i]);
101 }
102 }
103 printf("Right shift:\n");
104 for (int i = 0; i < R_MAX; i++) {
105 if (rightShift[i] != 0) {
106 printf("%d| %d\n", i, rightShift[i]);
107 }
108 }
109 }

localhost:56264/1f71acb7-88b3-4594-80a1-a13aff229926/ 2/6
23:29 14/01/2024 Test.cpp
110
111 int main() {
112 srand(time(0));
113 int arr[N];
114 int n = N;
115 int k[4] = {10, 100, 1000, 10000};
116 int num_case = 4;
117 int leftShift[L_MAX + 1], rightShift[R_MAX + 1];
118
119 for (int i = 0; i < num_case; i++) {
120 printf("-- K = %d: \n", k[i]);
121 createFrequencyTable(n, arr, k[i], leftShift, rightShift);
122 printFrequencyTable(leftShift, rightShift);
123 }
124 return 0;
125 }
126
127
128 // BAI 2
129 #include<stdio.h>
130 #include<stdlib.h>
131 #include<time.h>
132
133 #define max 20
134
135 void swap(int *a, int *b) {
136 int temp = *a;
137 *a = *b;
138 *b = temp;
139 }
140
141 void bubbleSort(int n, int arr[], int *step)
142 {
143 *step = 0;
144 for (int i = n - 1; i > 0; i--) {
145 for (int j = 0; j < i; j++) {
146 (*step)++;
147 if (arr[j] > arr[j + 1]) {
148 (*step) += 3;
149 swap(&arr[j], &arr[j + 1]);
150 }
151 }
152 }
153 }
154
155 void selectionSort(int n, int arr[], int *step)
156 {
157 *step = 0;
158 int min_index;
159 for (int i = 0; i < n - 1; i++) {
160 min_index = i;
161 for (int j = i + 1; j < n; j++) {
162 (*step)++;
163 if (arr[j] < arr[min_index]) {
164 min_index = j;
165 }

localhost:56264/1f71acb7-88b3-4594-80a1-a13aff229926/ 3/6
23:29 14/01/2024 Test.cpp
166 }
167 (*step) += 3;
168 swap(&arr[i], &arr[min_index]);
169 }
170 }
171
172 void insertionSort(int n, int arr[], int *step) {
173 *step = 0;
174 int pos;
175 for (int i = 1; i < n; i++) {
176 pos = arr[i];
177 int j = i - 1;
178 (*step)++;
179 while(j >= 0 && pos < arr[j]) {
180 (*step) += 2;
181 arr[j + 1] = arr[j];
182 j--;
183 }
184 (*step)++;
185 arr[j + 1] = pos;
186 }
187 }
188
189 int partition(int arr[], int left, int right, int* step) {
190 int pivot = right;
191 right--;
192 while(true) {
193 (*step)++;
194 while(arr[left] < arr[pivot]) {
195 (*step)++;
196 left++;
197 }
198 (*step)++;
199 while(arr[right] > arr[pivot] && left <= right) {
200 (*step)++;
201 right--;
202 }
203 if (left >= right) {
204 break;
205 }
206 (*step) += 3;
207 swap(&arr[left], &arr[right]);
208 left++;
209 right--;
210 }
211 (*step) += 3;
212 swap(&arr[left], &arr[pivot]);
213 return left;
214 }
215
216 void quickSort(int arr[], int left, int right, int *step)
217 {
218 *step = 0;
219 if (left < right) {
220 int pi = partition(arr, left, right, step);
221 int temp;

localhost:56264/1f71acb7-88b3-4594-80a1-a13aff229926/ 4/6
23:29 14/01/2024 Test.cpp
222 quickSort(arr, left, pi - 1, &temp);
223 *step += temp;
224 quickSort(arr, pi + 1, right, &temp);
225 *step += temp;
226 }
227 }
228
229 void printArray(int n, int arr[])
230 {
231 for (int i = 0; i < n; i++) {
232 printf("%d ", arr[i]);
233 }
234 printf("\n");
235 }
236
237 void copyArray(int *n, int arr[], int *n_copy, int arr_copy[])
238 {
239 for (int i = 0; i < *n; i++) {
240 arr_copy[i] = arr[i];
241 }
242 *n_copy = *n;
243 }
244
245 void showTimeComplexity4Sort(int n, int arr[], int *step_bubble_sort, int
*step_selection_sort, int *step_insertion_sort, int *step_quick_sort)
246 {
247 int arr_copy[max];
248
249 copyArray(&n, arr, &n, arr_copy);
250 bubbleSort(n, arr_copy, step_bubble_sort);
251 printf("Bubble Sort:\n");
252 printArray(n, arr_copy);
253 printf("Step count: %d\n", *step_bubble_sort);
254
255 copyArray(&n, arr, &n, arr_copy);
256 selectionSort(n, arr_copy, step_selection_sort);
257 printf("Selection Sort:\n");
258 printArray(n, arr_copy);
259 printf("Step count: %d\n", *step_selection_sort);
260
261 copyArray(&n, arr, &n, arr_copy);
262 insertionSort(n, arr_copy, step_insertion_sort);
263 printf("Insertion Sort:\n");
264 printArray(n, arr_copy);
265 printf("Step count: %d\n", *step_insertion_sort);
266
267 copyArray(&n, arr, &n, arr_copy);
268 quickSort(arr_copy, 0, n - 1, step_quick_sort);
269 printf("Quick Sort:\n");
270 printArray(n, arr_copy);
271 printf("Step count: %d\n", *step_quick_sort);
272 }
273 int main()
274 {
275 int arr1[] = {3, 2, 9, 7, 5};
276 int n1 = 5;

localhost:56264/1f71acb7-88b3-4594-80a1-a13aff229926/ 5/6
23:29 14/01/2024 Test.cpp
277 int arr2[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
278 int n2 = 10;
279 int arr3[] = {4, 3, 2, 6, 5, -3, 5, 7, -3, 1, -3, 5, 7, 9, 7, -5, 3, 2, 1, 2};
280 int n3 = 20;
281 int arr4[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 11, 17, 14, 15, 16, 13, 18, 19, 20};
282 int n4 = 20;
283
284 int step_bubble_sort, step_insertion_sort, step_selection_sort, step_quick_sort;
285
286 printf("MANG 1:\n");
287 showTimeComplexity4Sort(n1, arr1, &step_bubble_sort, &step_selection_sort, &
step_insertion_sort, &step_quick_sort);
288 printf("\nMANG 2:\n");
289 showTimeComplexity4Sort(n2, arr2, &step_bubble_sort, &step_selection_sort, &
step_insertion_sort, &step_quick_sort);
290 printf("\nMANG 3:\n");
291 showTimeComplexity4Sort(n3, arr3, &step_bubble_sort, &step_selection_sort, &
step_insertion_sort, &step_quick_sort);
292 printf("\nMANG 4:\n");
293 showTimeComplexity4Sort(n4, arr4, &step_bubble_sort, &step_selection_sort, &
step_insertion_sort, &step_quick_sort);
294 return 0;
295 }
296
297 /*
298 Mảng 1: Đối với mảng có lượng ít phần tử thì cả 4 thuật toán cho kết quả chênh lệch không
quá lớn
299 Mảng 2: Mảng có số lượng phần tử trung bình và có thứ tự giảm dần, thuật toán selection
sort cho độ phức tạp thấp nhất, quick sort có độ phức
300 tạp nhỏ thứ 2, bubble sort có độ phức tạp lớn nhất
301 Mảng 3: Mảng có số phần tử lớn hơn và được sắp xếp không trật tự, quick sort hiệu quả
nhất kế tiếp là insertion sort, cho thấy với mảng được
302 sắp xếp theo chiều gần như giảm dần thì selection sort nhanh hơn insertion sort, còn sắp
xếp hỗn loạn thì insertion sort cho hiệu quả tốt hơn
303 Mảng 4: Mảnh đã được sắp xếp tăng dần, insertion sort cho hiệu quả tốt nhất, quick sort
và selection sort cho thấy hiệu quả kém trong trường
304 hợp này
305 Ở các trường hợp bubble sort đều không cho hiệu quả tốt đáng kể so với các thuật toán
khác
306 */

localhost:56264/1f71acb7-88b3-4594-80a1-a13aff229926/ 6/6

You might also like