-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge.c
197 lines (167 loc) · 3.86 KB
/
merge.c
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include<stdio.h>
#include<stdlib.h>
void print(int* arr,int len)
{
int i;
for(i = 0;i < len;i++)
{
printf("%d ",arr[i]);
}
printf("\n");
}
void merge(int arr[],int left,int middle,int right)
{
int n1 = middle - left + 1; //middle - left + 1是左边界
int n2 = right - middle; //right - middle 是右边界
//创建临时数组来存储左右子数组
int* L = (int*)malloc(n1 * sizeof(int));
int* R = (int*)malloc(n2 * sizeof(int));
//将数据复制到临时数组
for(int i = 0;i < n1;i++)
{
L[i] = arr[left + i]; //
}
for(int j = 0;j < n2;j++)
{
R[j] = arr[middle + 1 + j];
}
//合并临时数组为一个有序数组
int i = 0,j = 0,k = left;
while(i < n1 && j < n2)
{
if(L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
//arr[k++] = L[i++];
//arr[k++] = R[j++];
}
//处理剩余的元素
while(i < n1)
{
arr[k++] = L[i++];
}
while(j < n2)
{
arr[k++] = R[j++];
}
//释放临时数组的内存
free(L);
free(R);
}
void mergeSort(int arr[],int left,int right)
{
if(left < right)
{
int middle = left + (right - left) / 2;
//递归地排序左右子数组
mergeSort(arr,left,middle);
mergeSort(arr,middle + 1,right);
//合并两个排序好的子数组
merge(arr,left,middle,right);
}
}
int min(int a,int b)
{
return a < b ? a : b;
}
void merge_sort(int* arr,int len)
{
int* a = arr;
int* b = (int*)malloc(sizeof(int) * len);
int seg,start;
for(seg = 1;seg < len;seg += seg)
{
for(start = 0;start < len;start += seg + seg)
{
int low = start,mid = min(start + seg,len),high = min(start + seg + seg,len);
int k = start;
int start1 = low,end1 = mid;
int start2 = mid,end2 = high;
while(start1 < end1 && start2 < end2)
b[k++] = a[start1] < a[start2] ? a[start1++] : a[start2++];
while(start1 < end1)
b[k++] = a[start1++];
while(start2 < end2)
b[k++] = a[start2++];
}
int* temp = a;
a = b; //a指向已经排好列的数组
b = temp; //b指向arr b = temp = arr
}
if(a != arr)
{
int i;
for(i = 0;i < len;i++)
{
b[i] = a[i]; //修改arr里面的值
}
b = a; //改变指向
}
free(b);
}
void MSort(int* A,int* TmpArray,int left,int right);
//另一种递归实现merge方法
void Merge_sort(int* A,int N)
{
int* TmpArray = (int*)malloc(sizeof(int) * N);
if(TmpArray != NULL)
{
MSort(A,TmpArray,0,N - 1);
free(TmpArray); //申请空间不要忘记释放空间
}
else
{
printf("Out of space!\n");
exit(0);
}
}
void Merge(int* A,int* TmpArray,int Lpos,int Rpos,int RightEnd);
void MSort(int* A,int* TmpArray,int left,int right)
{
int Center = (left + right) / 2; //center = left + (right - left) / 2;
if(left < right)
{
MSort(A,TmpArray,left,Center);
MSort(A,TmpArray,Center + 1,right);
Merge(A,TmpArray,left,Center + 1,right);
}
}
void Merge(int* A,int* TmpArray,int Lpos,int Rpos,int RightEnd)
{
int LeftEnd,TmpPos,NumElements;
LeftEnd = Rpos - 1;
TmpPos = Lpos;
NumElements = RightEnd - Lpos + 1; //rightend是原数组的最后一个下标,所以要+1
//main loop
while(Lpos <= LeftEnd && Rpos <= RightEnd)
TmpArray[TmpPos++] = A[Lpos] < A[Rpos] ? A[Lpos++] : A[Rpos++];
while(Lpos <= LeftEnd)
TmpArray[TmpPos++] = A[Lpos++];
while(Rpos <= RightEnd)
TmpArray[TmpPos++] = A[Rpos++];
//copy tmparray back
for(int i = 0;i < NumElements;i++,RightEnd--)
A[RightEnd] = TmpArray[RightEnd];
//A[RightEnd--] = TmpArray[RightEnd--];
}
int main()
{
int arr[] = {12,11,13,5,6,7};
int n = sizeof(arr) /sizeof(arr[0]);
printf("Unsorted array: \n");
print(arr,n);
//mergeSort(arr,0,n - 1);
//merge_sort(arr,n);
Merge_sort(arr,n);
printf("After sorted: \n");
print(arr,n);
return EXIT_SUCCESS;
}