-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2751.cpp
More file actions
63 lines (47 loc) · 1.1 KB
/
2751.cpp
File metadata and controls
63 lines (47 loc) · 1.1 KB
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
#include <iostream>
#include <algorithm>
using namespace std;
const int MAX = 1000000;
int N;
int arr[MAX];
int tempArr[MAX];
void merge(int low, int mid, int high)
{
int i = low, j = mid + 1, k = low;
while (i <= mid && j <= high)
{
if (arr[i] < arr[j])
tempArr[k] = arr[i++];
else
tempArr[k] = arr[j++];
k++;
}
if (i > mid)
for (int idx = j; idx <= high; idx++)
tempArr[k++] = arr[idx];
else
for (int idx = i; idx <= mid; idx++)
tempArr[k++] = arr[idx];
for (int idx = low; idx <= high; idx++)
arr[idx] = tempArr[idx];
}
void mergeSort(int low, int high)
{
if (high > low)
{
int mid = (low + high) / 2;
mergeSort(low, mid);
mergeSort(mid + 1, high);
merge(low, mid, high);
}
}
int main(void)
{
scanf("%d", &N);
for (int i = 0; i < N; i++)
scanf("%d", &arr[i]);
mergeSort(0, N - 1);
for (int i = 0; i < N; i++)
printf("%d\n", arr[i]);
return 0;
}