-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path30[课设].归并排序.cpp
57 lines (50 loc) · 1 KB
/
30[课设].归并排序.cpp
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
#include <stdio.h>
void Merge(int A[], int B[], int Lpos, int Rpos, int RightEnd)
{
int i, LeftEnd, NumElements, TmpPos;
LeftEnd = Rpos - 1;
TmpPos = Lpos;
NumElements = RightEnd - Lpos + 1;
while (Lpos <= LeftEnd && Rpos <= RightEnd)
{
if (A[Lpos] <= A[Rpos])
B[TmpPos++] = A[Lpos++];
else
B[TmpPos++] = A[Rpos++];
}
while (Lpos <= LeftEnd)
B[TmpPos++] = A[Lpos++];
while (Rpos <= RightEnd)
B[TmpPos++] = A[Rpos++];
for (i = 0; i < NumElements; i++, RightEnd--)
{
A[RightEnd] = B[RightEnd];
}
}
void MergeSort(int A[], int B[], int begin, int end)
{
if (begin >= end)
return;
int mid = (end + begin) / 2;
MergeSort(A, B, begin, mid);
MergeSort(A, B, mid + 1, end);
Merge(A, B, begin, mid + 1, end);
}
int main()
{
int A[100];
int B[100];
int n;
printf("Please input merge n\n");
scanf_s("%d", &n);
printf("Please input nums\n");
for (int i = 0; i < n; i++)
{
scanf_s("%d", &A[i]);
}
MergeSort(A, B, 0, n - 1);
for (int i = 0; i < n; i++)
{
printf("%d ", A[i]);
}
}