-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.cpp
More file actions
28 lines (28 loc) · 966 Bytes
/
Copy pathMergeSort.cpp
File metadata and controls
28 lines (28 loc) · 966 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
#include<stdlib.h>
void merge(int* a, int* t, int leftIndex, int rightIndex, int rightEnd) {
int begin = leftIndex;
int leftEnd = rightIndex - 1;
int tIndex = leftIndex;
while (leftIndex <= leftEnd && rightIndex <= rightEnd) {
if (a[leftIndex] <= a[rightIndex]) t[tIndex++] = a[leftIndex++];
else t[tIndex++] = a[rightIndex++];
}
while (leftIndex <= leftEnd) t[tIndex++] = a[leftIndex++];
while (rightIndex <= rightEnd) t[tIndex++] = a[rightIndex++];
for (int i = begin; i <= rightEnd; ++i) a[i] = t[i];
return;
}
void msort(int* arr, int* tmp, int left, int right) {
if (left >= right) return;
int mid = (left + right) / 2;
msort(arr, tmp, left, mid);
msort(arr, tmp, mid + 1, right);
merge(arr, tmp, left, mid + 1, right);
}
void mergeSort(int* arr, int size) {
int* tmp;
tmp = (int*)malloc(sizeof(int) * size);
if (!tmp)return;
msort(arr, tmp, 0, size - 1);
free(tmp);
}