-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.cpp
More file actions
48 lines (39 loc) · 1.13 KB
/
MergeSort.cpp
File metadata and controls
48 lines (39 loc) · 1.13 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
// stl 없이 합병 정렬 구현
// 시간 복잡도 best - avg - worst >>> nlogn - nlogn - nlogn
#include <iostream>
using namespace std;
int N;
int temp[1000001], arr[1000001];
void Merge(int start, int mid, int end){
int l = start, m = mid+1, nl = start;
while (l <= mid && m <= end){ // 분할된거 병합
if(arr[l] <= arr[m]){ temp[nl++] = arr[l++]; }
else{ temp[nl++] = arr[m++]; }
}
if(l > mid){ // 남아있는 값들 복사
for(int i=m; i<=end; i++){ temp[nl++] = arr[i]; }
}
else{
for(int i=l; i<=mid; i++){ temp[nl++] = arr[i]; }
}
for(int i=start; i<=end; i++){ // 임시 배열 리스트 arr로 복사
arr[i] = temp[i];
}
}
void Divide(int start, int end){
int mid = 0;
if(start < end){
mid = (start + end)/2;
Divide(start, mid);
Divide(mid+1, end);
Merge(start, mid, end);
}
}
int main(void){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> N;
for(int i=0; i<N; i++){ cin >> arr[i]; }
Divide(0, N-1);
for(int i=0; i<N; i++){ cout << arr[i] << "\n"; }
return 0;
}