-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmergesort.cpp
More file actions
48 lines (46 loc) · 809 Bytes
/
mergesort.cpp
File metadata and controls
48 lines (46 loc) · 809 Bytes
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
#include<iostream>
#include<algorithm>
template<class T>
void mergesort(T* arr, T* temp, int start, int end)
{
int mid = (start + end) / 2;
if (start == end) return;
mergesort(arr, temp, start, mid);
mergesort(arr, temp, mid + 1, end);
for (int i = start; i <= end; ++i)
{
temp[i] = arr[i];
}
int i1 = start, i2 = mid + 1;
for (int i = start; i <= end; i++)
{
if (i1 == mid + 1)
{
arr[i] = temp[i2++];
}
else if (i2 == end + 1)
{
arr[i] = temp[i1++];
}
else if (temp[i1] < temp[i2])
{
arr[i] = temp[i1++];
}
else
{
arr[i] = temp[i2++];
}
}
}
int main(int argc, char const *argv[])
{
//1 2 2 3 3 5 5 7 8
int arr[] = {8, 5, 3, 2, 1, 5, 7, 2, 3};
int temp[9] = {0};
mergesort(arr, temp, 0, 8);
for (auto i : arr)
{
std::cout << i << " ";
}
return 0;
}