-
Notifications
You must be signed in to change notification settings - Fork 1
/
MergeSort.cpp
88 lines (73 loc) · 1.81 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
85
86
87
88
// Techie Delight https://www.techiedelight.com/merge-sort/
#include <iostream>
#include <vector>
#include <cstdlib>
#include <algorithm>
void print(const std::vector<int>& nums)
{
for (auto n : nums) {
std::cout << n << " ";
}
}
std::vector<int> generateRandomNumbers(const int length, const int min, const int max)
{
std::vector<int> nums;
for (int i = 0; i < length; ++i) {
int x = min + (rand() % max - min + 1);
nums.push_back(x);
}
return nums;
}
void merge(std::vector<int>& nums, std::vector<int>& aux, int low, int mid, int high)
{
int i = low;
int j = mid + 1;
int k = low;
while (i <= mid && j <= high) {
if (nums[i] <= nums[j]) {
aux[k++] = nums[i++];
} else {
aux[k++] = nums[j++];
}
}
while (i <= mid) {
aux[k++] = nums[i++];
}
while (j <= high) {
aux[k++] = nums[j++];
}
for (int i = low; i <= high; ++i) {
nums[i] = aux[i];
}
}
void mergesort(std::vector<int>& nums, std::vector<int>& aux, int low, int high)
{
if (low == high) {
return;
}
int mid = low + (high - low) / 2;
mergesort(nums, aux, low, mid);
mergesort(nums, aux, mid + 1, high);
merge(nums, aux, low, mid, high);
}
void mergesort(std::vector<int>& nums)
{
std::vector<int> aux(nums.size());
mergesort(nums, aux, 0, nums.size() - 1);
}
void test(std::vector<int>& nums)
{
std::cout << "Numbers before sorting: ";
print(nums);
mergesort(nums);
std::cout << "\nNumbers after sorting: ";
print(nums);
std::cout << "\nIs sorted: " << std::boolalpha << std::is_sorted(nums.begin(), nums.end());
std::cout << "\n";
}
int main()
{
std::vector<int> nums = generateRandomNumbers(20, -1000, 1000);
test(nums);
return 0;
}