-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathmerge_sort.h
84 lines (79 loc) · 2.58 KB
/
merge_sort.h
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*******************************************************************************
* DANIEL'S ALGORITHM IMPLEMENTAIONS
*
* /\ | _ _ ._ o _|_ |_ ._ _ _
* /--\ | (_| (_) | | |_ | | | | | _>
* _|
*
* MERGE SORT
*
* Features:
* This is divide and conquer algorithm. This works as follows.
* (1) Divide the input which we have to sort into two parts in the middle. Call it the left part
* and right part.
* Example: Say the input is -10 32 45 -78 91 1 0 -16 then the left part will be
* -10 32 45 -78 and the right part will be 91 1 0 6.
* (2) Sort Each of them separately. Note that here sort does not mean to sort it using some other
* method. We already wrote function to sort it. Use the same.
* (3) Then merge the two sorted parts.
*
* ------------
* Worst case performance O(n log n)
* Best case performance
* O(n log n) typical,
* O(n) natural variant
* Average case performance O(n log n)
* Worst case space complexity O(n) auxiliary
* ------------
*
* Merge sort can easily be optmized for parallized computing.
*
* http://en.wikipedia.org/wiki/Merge_sort
*
******************************************************************************/
#ifndef ALGO_MERGE_SORT_H__
#define ALGO_MERGE_SORT_H__
namespace alg {
/**
* Merge functions merges the two sorted parts. Sorted parts will be from [left, mid] and [mid+1, right].
*/
template<typename T>
static void merge_(T *array, int left, int mid, int right) {
/*We need a Temporary array to store the new sorted part*/
T tempArray[right-left+1];
int pos=0,lpos = left,rpos = mid + 1;
while(lpos <= mid && rpos <= right) {
if(array[lpos] < array[rpos]) {
tempArray[pos++] = array[lpos++];
}
else {
tempArray[pos++] = array[rpos++];
}
}
while(lpos <= mid) tempArray[pos++] = array[lpos++];
while(rpos <= right)tempArray[pos++] = array[rpos++];
int iter;
/* Copy back the sorted array to the original array */
for(iter = 0;iter < pos; iter++) {
array[iter+left] = tempArray[iter];
}
return;
}
/**
* sort an array from left->right
*/
template<typename T>
static void merge_sort(T *array, int left, int right) {
int mid = (left+right)/2;
/* We have to sort only when left<right because when left=right it is anyhow sorted*/
if(left<right) {
/* Sort the left part */
merge_sort(array,left,mid);
/* Sort the right part */
merge_sort(array,mid+1,right);
/* Merge the two sorted parts */
merge_(array,left,mid,right);
}
}
}
#endif //