-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMergeX.h
148 lines (128 loc) · 4.45 KB
/
MergeX.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#ifndef CH2_MERGEX_H
#define CH2_MERGEX_H
#include <vector>
#include <iostream>
using std::vector;
using std::swap;
using std::cout;
using std::endl;
/**
* The {@code MergeX} class provides static methods for sorting an
* array using an optimized version of mergesort.
* <p>
* For additional documentation, see <a href="https://algs4.cs.princeton.edu/22mergesort">Section 2.2</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
class MergeX {
public:
// This class should not be instantiated.
MergeX() = delete;
/**
* Rearranges the array in ascending order, using the natural order.
* @param a the array to be sorted
*/
template<typename T>
static void sort(vector<T> &a) {
vector<T> aux(a);
sort(aux, a, 0, a.size() - 1);
}
/*******************************************************************
* Version that takes Comparator as argument.
*******************************************************************/
/**
* Rearranges the array in ascending order, using the provided order.
*
* @param a the array to be sorted
* @param comparator the comparator that defines the total order
*/
template<typename T, typename Comp>
static void sort(vector<T> &a, Comp comp) {
vector<T> aux(a);
sort(aux, a, 0, a.size() - 1, comp);
}
// print array to standard output
template<typename T>
static void show(vector<T> &a) {
for (int i = 0; i < a.size(); i++) {
cout << a[i] << endl;
}
}
private:
template<typename T>
static void merge(vector<T> &src, vector<T> &dst, int lo, int mid, int hi) {
int i = lo, j = mid + 1;
for (int k = lo; k <= hi; k++) {
if (i > mid) dst[k] = src[j++];
else if (j > hi) dst[k] = src[i++];
else if (src[j] < src[i]) dst[k] = src[j++]; // to ensure stability
else dst[k] = src[i++];
}
}
template<typename T>
static void sort(vector<T> &src, vector<T> &dst, int lo, int hi) {
// if (hi <= lo) return;
if (hi <= lo + CUTOFF) {
insertionSort(dst, lo, hi);
return;
}
int mid = lo + (hi - lo) / 2;
sort(dst, src, lo, mid);
sort(dst, src, mid + 1, hi);
// first num of right>=last num of left
if (!(src[mid + 1] < src[mid])) {
for (int i = lo; i <= hi; i++) dst[i] = src[i];
return;
}
merge(src, dst, lo, mid, hi);
}
// sort from a[lo] to a[hi] using insertion sort
template<typename T>
static void insertionSort(vector<T> &a, int lo, int hi) {
for (int i = lo; i <= hi; i++)
for (int j = i; j > lo && a[j] < a[j - 1]; j--)
swap(a[j], a[j - 1]);
}
/*******************************************************************
* Version that takes Comparator as argument.
*******************************************************************/
template<typename T, typename Comp>
static void merge(vector<T> &src, vector<T> &dst, int lo, int mid, int hi, Comp comp) {
int i = lo, j = mid + 1;
for (int k = lo; k <= hi; k++) {
if (i > mid) dst[k] = src[j++];
else if (j > hi) dst[k] = src[i++];
else if (comp(src[j], src[i])) dst[k] = src[j++];
else dst[k] = src[i++];
}
}
template<typename T, typename Comp>
static void sort(vector<T> &src, vector<T> &dst, int lo, int hi, Comp comp) {
// if (hi <= lo) return;
if (hi <= lo + CUTOFF) {
insertionSort(dst, lo, hi, comp);
return;
}
int mid = lo + (hi - lo) / 2;
sort(dst, src, lo, mid, comp);
sort(dst, src, mid + 1, hi, comp);
// using System.arraycopy() is a bit faster than the above loop
if (!comp(src[mid + 1], src[mid])) {
for (int i = lo; i <= hi; i++) dst[i] = src[i];
return;
}
merge(src, dst, lo, mid, hi, comp);
}
// sort from a[lo] to a[hi] using insertion sort
template<typename T, typename Comp>
static void insertionSort(vector<T> &a, int lo, int hi, Comp comp) {
for (int i = lo; i <= hi; i++)
for (int j = i; j > lo && comp(a[j], a[j - 1]); j--)
swap(a[j], a[j - 1]);
}
private:
static const int CUTOFF = 7;
};
#endif //CH2_MERGEX_H