-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMerge Sort Code
69 lines (63 loc) · 883 Bytes
/
Merge Sort Code
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
#include<bits/stdc++.h>
using namespace std;
void merge(int input[],int start,int end)
{
int mid=(start+end)/2;
int n1=mid-start+1;
int n2=end-mid;
int p1[n1];
int p2[n2];
for(int i=0;i<n1;i++)
{
p1[i]=input[start+i];
}
for(int j=0;j<n2;j++)
{
p2[j]=input[mid+1+j];
}
int i=0;
int j=0;
int k=start;
while(i<n1 && j<n2)
{
if(p1[i]<=p2[j])
{
input[k]=p1[i];
k++;
i++;
}
else
{
input[k]=p2[j];
k++;
j++;
}
}
while(i<n1)
{
input[k]=p1[i];
k++;
i++;
}
while(j<n2)
{
input[k]=p2[j];
k++;
j++;
}
}
void helper(int input[],int start,int end)
{
if(start>=end)
{
return;
}
int mid=(start+end)/2;
helper(input,start,mid);
helper(input,mid+1,end);
merge(input,start,end);
}
void mergeSort(int input[], int size)
{
helper(input,0,size-1);
}