-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharray_sample_merge_sort.texy
75 lines (63 loc) · 1.3 KB
/
array_sample_merge_sort.texy
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
74
75
void print_wordarray(Word a[], int n){
int i;
printword("--------------------");
for (i=0; i<n; i=i+1){
printword(a[i]);
}
}
int merge(Word a[], int l, int m, int r){
int n1;
int n2;
Word buf[];
int i;
buf = [" "," "," "," "," "," "," "," "," "," "," "," "," "];
n1=l;
n2 = m+1;
i=l;
for (i=l;n1<=m&&n2<=r;i=i+1) {
if(strcmp(a[n1], a[n2])<=0) {
buf@[i] = a[n1];
n1= n1+1;
}
else {
buf@[i] = a[n2];
n2= n2+1;
}
}
while(n1<=m){
buf@[i]= a[n1];
i=i+1;
n1= n1+1;
}
while(n2<=r){
buf@[i]= a[n2];
i=i+1;
n2= n2+1;
}
for (i=l;i<=r;i=i+1){
a@[i]=buf[i];
}
return 0;
}
int merge_sort(Word a[], int l, int r){
int m;
if(l <r){
m = (l+r)/2;
merge_sort(a, l, m);
merge_sort(a, m+1, r);
merge(a, l, m, r);
}
return 0;
}
int main() {
Word names[];
int i;
names = ["Stephen","Thomas","Dennis","Dop","Able","Bob","Celtic","Ethen","Mike"];
printword("Before sorted:");
print_wordarray(names, 9);
printword(" ");
merge_sort(names,0,8);
printword("After merge sort:");
print_wordarray(names, 9);
return 0;
}