-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergeSort.cpp
73 lines (59 loc) · 1.25 KB
/
mergeSort.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Divide and Conquer
#include <bits/stdc++.h>
using namespace std;
void merge(int a[],int beg,int mid,int end){
int i=beg,j=mid+1;
int temp[end-beg+1];
int index=0;
while(i<=mid && j <= end){
if(a[i] < a[j]){
temp[index] = a[i];
i++;
}else{
temp[index] = a[j];
j++;
}
index++;
}
if(i>mid){
while(j<=end){
temp[index] = a[j];
j++;
index++;
}
}else{
while(i<=mid){
temp[index] = a[i];
i++;
index++;
}
}
for(int k=0;k<index && beg<=end;k++){
a[beg] = temp[k];
beg++;
}
}
// divide
void mergeSort(int a[],int beg,int end){ // T(n)
int mid;
if(beg<end){
mid = beg + (end-beg)/2;
mergeSort(a,beg,mid); // T(n/2)
mergeSort(a,mid+1,end); // T(n/2)
merge(a,beg,mid,end); // O(n)
}
}
// Time Complexity :- nlog(n)
void display(int arr[],int n){
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}
int main(){
int input[] = {3,0,3,2,1,0};
int n=6;
mergeSort(input,0,n-1);
display(input,n);
return 0;
}