forked from spandey1296/Learn-Share-Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 5
/
mergeSort.cpp
46 lines (44 loc) · 857 Bytes
/
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
#include <iostream>
using namespace std;
void merge(int arr[], int low, int mid, int high)
{
int i = low, j = mid + 1, k = low, z[20];
while (i <= mid && j <= high)
{
if (arr[i] <= arr[j])
z[k++] = arr[i++];
else
z[k++] = arr[j++];
}
while (i <= mid)
z[k++] = arr[i++];
while (j <= high)
z[k++] = arr[j++];
for (i = low; i <= high; i++)
arr[i] = z[i];
}
void mergeSort(int *arr, int low, int high)
{
if (low < high)
{
int mid = (low + high) / 2;
mergeSort(arr, low, mid);
mergeSort(arr, mid + 1, high);
merge(arr, low, mid, high);
}
}
int main()
{
int n;
cin >> n;
int *arr = new int[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
mergeSort(arr, 0, n - 1);
cout << "Sorted array is" << endl;
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}