-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMerge sort.cpp
More file actions
61 lines (53 loc) · 1.37 KB
/
Copy pathMerge sort.cpp
File metadata and controls
61 lines (53 loc) · 1.37 KB
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
/* Merge-sort is based on the divide-and-conquer paradigm. It involves the following three steps:
Divide the array into two (or more) subarrays
Sort each subarray (Conquer)
Merge them into one (in a smart way!) */
#include <iostream>
#include <cstdio>
using namespace std;
void mergeSort(int* arr, int beg, int end);
void merge(int*, int, int, int);
void printArr(int* arr, int size);
void printArray(int* arr, int beg, int end);
void mergeSort(int* arr, int beg, int end) {
if (beg < end) {
int mid = (beg+end)/2;
mergeSort(arr, beg, mid);
mergeSort(arr, mid+1, end);
merge(arr, beg, end, mid);
}
return;
}
void merge(int* arr, int beg, int end, int mid) {
int temp[end+1], i, j, k;
i = beg; j = mid+1; k = beg;
while (i <= mid && j <= end)
if (arr[i] < arr[j])
temp[k++] = arr[i++];
else
temp[k++] = arr[j++];
while (i <= mid)
temp[k++] = arr[i++];
while (j <= end)
temp[k++] = arr[j++];
for (i = beg; i <= end; i++)
arr[i] = temp[i];
return;
}
void printArr(int* arr, int size) {
for (int i = 0; i < size; i++)
cout<<arr[i]<<" ";
return;
}
int main() {
int arr[] = {1, 5, 20, 6, 4, 5, 56};
int n = sizeof(arr)/sizeof(int);
cout<<"array: ";
printArr(arr, n);
cout<<endl;
cout<<"Merge sort: ";
mergeSort(arr, 0, n-1);
printArr(arr, n);
cout<<endl<<endl;
return 0;
}