forked from rajnishmaurya73/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_sort.cpp
88 lines (73 loc) · 1.76 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<stdlib.h>
#include<stdio.h>
#include<iostream>
using namespace std;
void merge(int a[], int Firstindex, int m, int Lastindex); //merges the sub-arrays which are created while division
void mergeSort(int a[], int Firstindex, int Lastindex)
{
if (Firstindex < Lastindex)
{
int m = Firstindex + (Lastindex - Firstindex)/2;
mergeSort(a, Firstindex, m);
mergeSort(a, m+1, Lastindex);
merge(a, Firstindex, m, Lastindex);
}
}
void merge(int a[], int Firstindex, int m, int Lastindex)
{
int x;
int y;
int z;
int sub1 = m - Firstindex + 1;
int sub2 = Lastindex - m;
int First[sub1]; //temp array
int Second[sub2];
for (x = 0; x < sub1; x++) // copying data to temp arrays
First[x] = a[Firstindex + x];
for (y = 0; y < sub2; y++)
Second[y] = a[m + 1+ y];
x = 0;
y = 0;
z = Firstindex;
while (x < sub1 && y < sub2)
{
if (First[x] <= Second[y])
{
a[z] = First[x];
x++;
}
else
{
a[z] = Second[y];
y++;
}
z++;
}
while (x < sub1)
{
a[z] = First[x];
x++;
z++;
}
while (y < sub2)
{
a[z] = Second[y];
y++;
z++;
}
}
int main()
{
int size;
cout<<"Enter the size of the Array that is to be sorted: "; cin>>size;
int Hello[size],i;
cout<<"Enter the elements of the array one by one:n";
for(i=0; i<size; i++) cin>>Hello[i];
mergeSort(Hello, 0, size - 1);
cout<<"The Sorted List isn";
for(i=0; i<size; i++)
{
cout<<Hello[i]<<" ";
}
return 0;
}