-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergeSort.cpp
49 lines (41 loc) · 900 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
47
48
49
#include <iostream>
#include <vector>
using namespace std;
void merge(int arr[], int mid,int low, int high){
int B[100];
int k = low;
int i = low;
int j = mid+1;
while(i <= mid && j <= high){
if(arr[i] < arr[j]){
B[k++] = arr[i++];
} else {
B[k++] = arr[j++];
}
}
for(; i <= mid; ++i) {
B[k++] = arr[i];
}
for(; j <= high; ++j) {
B[k++] = arr[j];
}
for(i=low;i<=high;i++)
arr[i]=B[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, mid, low , high);
}
}
int main()
{
int arr[] = {9, 2, 3, 6, 1, 0, 5, 8};
mergeSort(arr, 0, 7);
for(int i = 0; i < 8; ++i){
std::cout << arr[i] << endl;
}
return 0;
}