-
Notifications
You must be signed in to change notification settings - Fork 32
/
mergesort.cpp
84 lines (64 loc) · 1.19 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
74
75
76
77
78
79
80
81
82
83
84
/*
simple implementation of merge sort
Author:sspsk
Email:[email protected]
*/
#include<iostream>
using namespace std;
/*
inputs:
a = given array
start = position of the first element
med = position of middle element
last = position of last element
function=joins the two subarrays
*/
void merge(int a[],int start,int med,int last){
int b[last-start+1];
int i=start;
int j=med+1;
int k=0;
while(i<=med && j<=last){
if(a[i]<a[j]){b[k]=a[i];i++;}
else{b[k] = a[j];j++;}
k++;
}
if(i>med){
while(j<=last){b[k]=a[j];j++;k++;}
}
else{
while(i<=med){b[k]=a[i];i++;k++;}
}
for(i=0;i<k;i++){
a[start+i]=b[i];
}
}
/*
inputs:
a = given array
start = position of first element
last = position of last element
function:
creates two subarrays and makes recursice call of mergesort
*/
void mergesort(int a[],int start,int last){
int med = (last+start)/2;
if(start==last)return;
mergesort(a,start,med);
mergesort(a,med+1,last);
merge(a,start,med,last);
};
/*
exmaple on a array
*/
int main(){
int a[]={0,9,2,7,4,5,3,6,8,1};
for(int i=0;i<10;i++){
cout<< a[i];
}
cout <<endl;
mergesort(a,0,9);
for(int i=0;i<10;i++){
cout<< a[i];
}
}