-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUASPractice_MergeSort.cpp
More file actions
73 lines (61 loc) · 1.48 KB
/
UASPractice_MergeSort.cpp
File metadata and controls
73 lines (61 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<stdio.h>
#include<string.h>
struct kalimat{
char string[1001];
};
void merge(struct kalimat kt[], int left, int right){
int mid = left + (right - left) / 2;
struct kalimat sorted[right - left + 1];
int curr = 0;
int leftIndex = left;
int rightIndex = mid + 1;
while(leftIndex <= mid && rightIndex <= right){
if(strcmp(kt[leftIndex].string, kt[rightIndex].string) > 0){
sorted[curr] = kt[rightIndex];
curr++;
rightIndex++;
} else if(strcmp(kt[leftIndex].string, kt[rightIndex].string) < 0){
sorted[curr] = kt[leftIndex];
curr++;
leftIndex++;
} else if(strcmp(kt[leftIndex].string, kt[rightIndex].string) == 0){
sorted[curr] = kt[leftIndex];
curr++;
leftIndex++;
}
}
while(leftIndex <= mid){
sorted[curr] = kt[leftIndex];
curr++;
leftIndex++;
}
while(rightIndex <= right){
sorted[curr] = kt[rightIndex];
curr++;
rightIndex++;
}
for(int i = 0; i < curr; i++){
kt[left + i] = sorted[i];
}
}
void mergeSort(struct kalimat kt[], int left, int right){
if(left < right){
int mid = left + (right - left) / 2;
mergeSort(kt, left, mid);
mergeSort(kt, mid+1, right);
merge(kt, left, right);
}
}
int main(){
struct kalimat kt[1001];
int t;
scanf("%d", &t); getchar();
for(int i = 0; i < t; i++){
scanf("%[^\n]", &kt[i].string); getchar();
}
mergeSort(kt, 0, t-1);
for(int i = 0; i < t; i++){
printf("%s\n", kt[i].string);
}
return 0;
}