-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMerge Sort.cpp
64 lines (52 loc) · 1.12 KB
/
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
53
54
55
56
57
58
59
60
61
62
63
#include<iostream>
#include<conio.h>
#include<windows.h>
using namespace std;
void mergesort(int[],int,int);
void merge(int[],int,int,int);
void main()
{
int a[10],p,r,i,n;
system("cls");
cout<<"Enter the number of elements";
cin>>n;
p=0;
r=n-1;
cout<<"Enter the array";
for(i=0;i<n;i++)
{
cin>>a[i];
}
mergesort(a,p,r);
cout<<"The sorted array is:";
for(i=0;i<n;i++)
{
cout<<"\n"<<a[i];
}
//getch();
}
void mergesort(int a[], int low, int high) {
if(low == high)
return;
int length = high-low+1;
int pivot = (low+high) / 2;
mergesort(a, low, pivot);
mergesort(a, pivot+1, high);
int *working = new int[length];
for(int i = 0; i < length; i++)
working[i] = a[low+i];
int m1 = 0;
int m2 = pivot-low+1;
for(int i = 0; i < length; i++) {
if(m2 <= high-low)
if(m1 <= pivot-low)
if(working[m1] > working[m2])
a[i+low] = working[m2++];
else
a[i+low] = working[m1++];
else
a[i+low] = working[m2++];
else
a[i+low] = working[m1++];
}
}