forked from sachinsingh3232/Hactoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Merge_sort.cpp
52 lines (44 loc) · 976 Bytes
/
Merge_sort.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
#include<bits/stdc++.h>
using namespace std;
void merging(int input[],int start,int end){
int mid = (start+end)/2;
int i=start, j=mid+1, k=start;
int ans[end+1];
while(i<=mid&&j<=end){
if(input[i]<input[j]){
ans[k]=input[i];
i++;
k++;
}
else{
ans[k]=input[j];
j++;
k++;
}
}
while(i<=mid){
ans[k]=input[i];
i++;
k++;
}
while(j<=end){
ans[k]=input[j];
j++;
k++;
}
for(int s=start;s<=end;s++){
input[s]=ans[s];
}
}
void merge_sort(int input[],int start,int end){
if(start>=end){
return;
}
int mid = ((start+end)/2);
merge_sort(input,start,mid);
merge_sort(input,mid+1,end);
merging(input,start,end);
}
void mergeSort(int input[], int size){
merge_sort(input,0,size-1);
}