-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMerge.cpp
53 lines (44 loc) · 1 KB
/
Merge.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
//2012004236_정구열
#include <stdio.h>
#define MAX_NUM 100000
void Merge_Sort(int[],int,int);
void Merge(int[],int,int,int);
int main() {
int arr[MAX_NUM], num, i;
scanf("%d",&num);
for(i=0;i<num;i++)
scanf("%d",&arr[i]);
Merge_Sort(arr, 0, num-1);
for(i=0;i<num;i++)
printf("%d\n",arr[i]);
return 0;
}
void Merge_Sort(int arr[], int f, int l)
{
int mid;
if(f<l)
{
mid=(f+l)/2;
Merge_Sort(arr, f, mid);
Merge_Sort(arr, mid+1, l);
Merge(arr, f, mid, l);
}
}
void Merge(int arr[],int f, int mid, int l)
{
int p = f, q = mid+1;
int tmp[l-f+1] , k=0, i;
for(i = f ;i <= l ;i++) {
if(p > mid)
tmp[ k++ ] = arr[ q++] ;
else if ( q > l)
tmp[ k++ ] = arr[ p++ ];
else if( arr[ p ] > arr[ q ])
tmp[ k++ ] = arr[ p++ ];
else
tmp[ k++ ] = arr[ q++];
}
for (int p=0 ; p< k ;p ++) {
arr[ f++ ] = tmp[ p ] ;
}
}