forked from poetteaes/git
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeSort.c
118 lines (92 loc) · 1.78 KB
/
MergeSort.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
# include <stdio.h>
# include <stdlib.h>
/*
void Merge(int A[], int M, int B[], int N, int out[])
{
int i=0;
int j=0;
int k=0;
while(i<M && j<N)
{
if(A[i]>B[j])
out[k++] = B[j++];
else
out[k++] = A[i++];
}
while(i<M)
out[k++] = A[i++];
while(j<N)
out[k++] = B[j++];
}
*/
/*分治算法 分段处理再合并 用递归的方法*/
void Merge(int Arr[], int tmpArr[], int left,int mid, int right)
{
int i, j,k;
i = left;
j = mid + 1;
k = 0;
while(i<=mid && j<=right)
{
if(Arr[i]>Arr[j])
tmpArr[k++] = Arr[j++];
else
tmpArr[k++] = Arr[i++];
}
while(i<=mid)
tmpArr[k++] = Arr[i++];
while(j<=right)
tmpArr[k++] = Arr[j++];
for(i =0; i<right-left+1; i++ )
Arr[i+left] = tmpArr[i];
}
void MergeDriver(int Arr[], int tmpArr[], int left, int right)
{
int mid;
if(left >= right)
return;
mid = (left + right)/2;
MergeDriver(Arr, tmpArr, left, mid);
MergeDriver(Arr, tmpArr, mid+1, right);
Merge(Arr, tmpArr, left, mid, right);
}
int MergeSort(int Arr[], int N)
{
int *tmpArr = NULL;
tmpArr = (int*)malloc(sizeof(int)*N);
if(tmpArr == NULL)
{
printf("Error! malloc error.\n");
return -1;
}
MergeDriver(Arr, tmpArr, 0, N-1);
free(tmpArr);
return 0;
}
int main(void)
{
int N;
int Arr[100];
int i;
/*input*/
printf("Please input an positive integer N which is between 1 and 100.\n");
scanf("%d", &N);
if(N <= 0 || N >100)
{
printf("Invalid input N[%d]\n", N);
return -1;
}
printf("Please input %d integer numbers.\n", N);
for(i=0; i<N; i++)
{
scanf("%d", &Arr[i]);
}
/*sort and ouput*/
MergeSort(Arr, N);
for(i=0; i<N; i++)
{
printf("%d ",Arr[i]);
}
printf("\n");
return 0;
}