-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergesort.cc
84 lines (78 loc) · 1.4 KB
/
mergesort.cc
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
#include <iostream>
#include <vector>
using namespace std;
void MergeSort(vector<int>& a) {
int size = a.size();
//base case
if (size == 1)
return;
//normal case
int mid = (size - 1) / 2;
int n1 = (size + 1) / 2;
int n2 = size - n1;
//create two half arrays
vector<int> a1(n1);
vector<int> a2(n2);
//copy values into the two arrays
int i = 0;
for (i = 0; i <= mid; i++)
{
a1[i] = a[i];
}
for (i = mid + 1; i < size; i++)
{
a2[i - mid - 1] = a[i];
}
//call merge sort on half arrays
MergeSort(a1);
MergeSort(a2);
//Merge the sorted halves
int j, k;
i = 0;
j = 0;
k = 0;
while (k < size)
{
if (j >= n2)
{
a[k] = a1[i];
i++;
}
else if (i >= n1)
{
a[k] = a2[j];
j++;
}
else if (a1[i] < a2[j])
{
a[k] = a1[i];
i++;
}
else
{
a[k] = a2[j];
j++;
}
k++;
}
return;
}
void main()
{
vector<int> nums(5);
nums[0] = 5;
nums[1] = 3;
nums[2] = 4;
nums[3] = 1;
nums[4] = 2;
for (int i = 0; i < 5; i++)
{
cout << nums[i] << " ";
}
cout << endl;
MergeSort(nums);
for (int i = 0; i < 5; i++)
{
cout << nums[i] << " ";
}
}