Skip to content

Commit 89e89c8

Browse files
committedMar 20, 2014
C++/AlgoAndADT: Non-recursive version of mergeSort
1 parent 83e9ff7 commit 89e89c8

File tree

2 files changed

+304
-165
lines changed

2 files changed

+304
-165
lines changed
 

‎C++/AlgoAndADT/sort.h

+130
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,135 @@ static void mergeSort3(T *begin, T *end) {
157157
_mergeSortInplace3(begin, end, &_temp[0]);
158158
}
159159

160+
template<typename T>
161+
struct MergeCombine4State {
162+
T *begin, *end;
163+
T *begin2, *end2;
164+
T *dest;
165+
MergeCombine4State(T *_begin, T *_end, T *_begin2, T *_end2, T *_dest):
166+
begin(_begin), end(_end), begin2(_begin2), end2(_end2), dest(_dest){}
167+
MergeCombine4State(){}
168+
};
169+
template<typename T>
170+
struct MergeSortInplace4State {
171+
T *begin, *end, *temp;
172+
int state;
173+
MergeSortInplace4State(T *_begin, T *_end, T *_temp): begin(_begin), end(_end), temp(_temp), state(0){}
174+
MergeSortInplace4State(){}
175+
};
176+
template<typename T>
177+
struct MergeSortTo4State {
178+
T *begin, *end, *dest;
179+
int state;
180+
MergeSortTo4State(T *_begin, T *_end, T *_dest): begin(_begin), end(_end), dest(_dest), state(0){}
181+
MergeSortTo4State(){}
182+
};
183+
template<typename T>
184+
struct MergeSort4State {
185+
enum {
186+
T_MergeCombine,
187+
T_MergeSortInplace,
188+
T_MergeSortTo,
189+
} type;
190+
union {
191+
MergeCombine4State<T> combine;
192+
MergeSortInplace4State<T> inplace;
193+
MergeSortTo4State<T> to;
194+
};
195+
MergeSort4State(const MergeCombine4State<T>& _combine): type(T_MergeCombine), combine(_combine){}
196+
MergeSort4State(const MergeSortInplace4State<T>& _inplace): type(T_MergeSortInplace), inplace(_inplace){}
197+
MergeSort4State(const MergeSortTo4State<T>& _to): type(T_MergeSortTo), to(_to){}
198+
MergeSort4State(){}
199+
};
200+
template<typename T>
201+
static void mergeSort4(T *begin, T *end) {
202+
static vector<T> _temp;
203+
_temp.resize(end - begin);
204+
205+
MergeSort4State<T> _stack[32];
206+
MergeSort4State<T> *top = _stack;
207+
208+
*top++ = MergeSort4State<T>(MergeSortInplace4State<T>(begin, end, &_temp[0]));
209+
210+
while (top > _stack) {
211+
MergeSort4State<T>& s = top[-1];
212+
switch (s.type) {
213+
case MergeSort4State<T>::T_MergeCombine: {
214+
MergeCombine4State<T>& combine = s.combine;
215+
T *begin = combine.begin, *end = combine.end;
216+
T *begin2 = combine.begin2, *end2 = combine.end2;
217+
T *dest = combine.dest;
218+
while (begin != end && begin2 != end2) {
219+
if (begin[0] < begin2[0]) *dest++ = *begin++;
220+
else *dest++ = *begin2++;
221+
}
222+
for (; begin != end; ++begin) *dest++ = *begin;
223+
for (; begin2 != end2; ++begin2) *dest++ = *begin2;
224+
--top;
225+
}
226+
break;
227+
case MergeSort4State<T>::T_MergeSortInplace: {
228+
MergeSortInplace4State<T>& inplace = s.inplace;
229+
T *begin = inplace.begin, *end = inplace.end, *temp = inplace.temp;
230+
int count = end - begin;
231+
switch (inplace.state) {
232+
case 0:
233+
if (count <= 1) {
234+
--top;
235+
} else {
236+
inplace.state = 1;
237+
T *mid = begin + count / 2;
238+
*top++ = MergeSort4State<T>(MergeSortTo4State<T>(begin, mid, temp));
239+
}
240+
break;
241+
case 1: {
242+
inplace.state = 2;
243+
T *mid = begin + count / 2, *tmid = temp + count / 2;
244+
*top++ = MergeSort4State<T>(MergeSortTo4State<T>(mid, end, tmid));
245+
}
246+
break;
247+
case 2: {
248+
T *tmid = temp + count / 2, *tend = temp + count;
249+
top[-1] = MergeSort4State<T>(MergeCombine4State<T>(temp, tmid, tmid, tend, begin));
250+
}
251+
break;
252+
default: assert(0); break;
253+
}
254+
}
255+
break;
256+
case MergeSort4State<T>::T_MergeSortTo: {
257+
MergeSortTo4State<T>& to = s.to;
258+
T *begin = to.begin, *end = to.end, *dest = to.dest;
259+
int count = end - begin;
260+
T *mid = begin + count / 2;
261+
switch (to.state) {
262+
case 0:
263+
if (count <= 1) {
264+
if (count == 1) dest[0] = begin[0];
265+
--top;
266+
} else {
267+
to.state = 1;
268+
*top++ = MergeSort4State<T>(MergeSortInplace4State<T>(begin, mid, dest));
269+
}
270+
break;
271+
case 1: {
272+
to.state = 2;
273+
*top++ = MergeSort4State<T>(MergeSortInplace4State<T>(mid, end, dest));
274+
}
275+
break;
276+
case 2: {
277+
top[-1] = MergeSort4State<T>(MergeCombine4State<T>(begin, mid, mid, end, dest));
278+
}
279+
break;
280+
default: assert(0); break;
281+
}
282+
}
283+
break;
284+
default: assert(0); break;
285+
}
286+
}
287+
}
288+
160289
template<typename T>
161290
static void pushHeap(T *begin, T *end) {
162291
for (int cur = end - begin, parent; cur > 1; cur = parent) {
@@ -426,6 +555,7 @@ int main() {
426555
ITEM(mergeSort, 0),
427556
ITEM(mergeSort2, 0),
428557
ITEM(mergeSort3, 0),
558+
ITEM(mergeSort4, 0),
429559
ITEM(quickSort, 0),
430560
ITEM(quickSort2, 0),
431561
ITEM(quickSort3, 0),

‎C++/AlgoAndADT/sortReport.txt

+174-165
Original file line numberDiff line numberDiff line change
@@ -6,183 +6,192 @@
66
@@@@ len = 0.093K,
77
@@@@ len = 0.124K,
88
@@@@ len = 0.251K,
9-
bubbleSort : 0.000130s
10-
bubbleSort2 : 0.000136s
11-
selectionSort : 0.000140s
12-
insertionSort : 0.000023s
13-
shellSort : 0.000022s
9+
bubbleSort : 0.000178s
10+
bubbleSort2 : 0.000197s
11+
selectionSort : 0.000155s
12+
insertionSort : 0.000022s
13+
shellSort : 0.000025s
1414
heapSort : 0.000021s
15-
mergeSort : 0.000011s
16-
mergeSort2 : 0.000012s
17-
mergeSort3 : 0.000009s
18-
quickSort : 0.000013s
15+
mergeSort : 0.000013s
16+
mergeSort2 : 0.000013s
17+
mergeSort3 : 0.000010s
18+
mergeSort4 : 0.000017s
19+
quickSort : 0.000014s
1920
quickSort2 : 0.000014s
2021
quickSort3 : 0.000022s
2122
quickSort4 : 0.000012s
22-
quickSort5 : 0.000014s
23-
quickSort6 : 0.000011s
23+
quickSort5 : 0.000015s
24+
quickSort6 : 0.000013s
2425
sort : 0.000011s
25-
stable_sort : 0.000012s
26-
ANSICqsort : 0.000030s
26+
stable_sort : 0.000014s
27+
ANSICqsort : 0.000034s
2728
stdSetSort : 0.000044s
28-
bstSort : 0.000015s
29+
bstSort : 0.000017s
2930
@@@@ len = 1.001K,
30-
bubbleSort : 0.001875s
31-
bubbleSort2 : 0.001956s
32-
selectionSort : 0.001913s
33-
insertionSort : 0.000289s
34-
shellSort : 0.000105s
35-
heapSort : 0.000086s
36-
mergeSort : 0.000043s
37-
mergeSort2 : 0.000047s
38-
mergeSort3 : 0.000036s
39-
quickSort : 0.000055s
40-
quickSort2 : 0.000057s
41-
quickSort3 : 0.000086s
42-
quickSort4 : 0.000052s
43-
quickSort5 : 0.000058s
44-
quickSort6 : 0.000048s
45-
sort : 0.000045s
46-
stable_sort : 0.000045s
47-
ANSICqsort : 0.000120s
48-
stdSetSort : 0.000164s
49-
bstSort : 0.000063s
31+
bubbleSort : 0.001253s
32+
bubbleSort2 : 0.001418s
33+
selectionSort : 0.001799s
34+
insertionSort : 0.000228s
35+
shellSort : 0.000101s
36+
heapSort : 0.000073s
37+
mergeSort : 0.000041s
38+
mergeSort2 : 0.000043s
39+
mergeSort3 : 0.000034s
40+
mergeSort4 : 0.000053s
41+
quickSort : 0.000047s
42+
quickSort2 : 0.000048s
43+
quickSort3 : 0.000072s
44+
quickSort4 : 0.000045s
45+
quickSort5 : 0.000051s
46+
quickSort6 : 0.000045s
47+
sort : 0.000039s
48+
stable_sort : 0.000047s
49+
ANSICqsort : 0.000122s
50+
stdSetSort : 0.000140s
51+
bstSort : 0.000068s
5052
@@@@ len = 1.997K,
51-
bubbleSort : 0.007438s
52-
bubbleSort2 : 0.007605s
53-
selectionSort : 0.007616s
54-
insertionSort : 0.001148s
55-
shellSort : 0.000211s
56-
heapSort : 0.000189s
57-
mergeSort : 0.000092s
58-
mergeSort2 : 0.000097s
53+
bubbleSort : 0.003545s
54+
bubbleSort2 : 0.006159s
55+
selectionSort : 0.007361s
56+
insertionSort : 0.000997s
57+
shellSort : 0.000202s
58+
heapSort : 0.000171s
59+
mergeSort : 0.000096s
60+
mergeSort2 : 0.000098s
5961
mergeSort3 : 0.000077s
60-
quickSort : 0.000119s
61-
quickSort2 : 0.000123s
62-
quickSort3 : 0.000181s
63-
quickSort4 : 0.000111s
64-
quickSort5 : 0.000121s
65-
quickSort6 : 0.000105s
66-
sort : 0.000098s
67-
stable_sort : 0.000095s
68-
ANSICqsort : 0.000257s
69-
stdSetSort : 0.000349s
70-
bstSort : 0.000138s
62+
mergeSort4 : 0.000121s
63+
quickSort : 0.000110s
64+
quickSort2 : 0.000111s
65+
quickSort3 : 0.000158s
66+
quickSort4 : 0.000097s
67+
quickSort5 : 0.000112s
68+
quickSort6 : 0.000100s
69+
sort : 0.000085s
70+
stable_sort : 0.000097s
71+
ANSICqsort : 0.000263s
72+
stdSetSort : 0.000298s
73+
bstSort : 0.000145s
7174
@@@@ len = 4.001K,
72-
bubbleSort : 0.029827s
73-
bubbleSort2 : 0.030822s
74-
selectionSort : 0.030481s
75-
insertionSort : 0.004593s
76-
shellSort : 0.000623s
77-
heapSort : 0.000408s
78-
mergeSort : 0.000195s
79-
mergeSort2 : 0.000206s
80-
mergeSort3 : 0.000170s
81-
quickSort : 0.000259s
82-
quickSort2 : 0.000267s
83-
quickSort3 : 0.000389s
84-
quickSort4 : 0.000247s
85-
quickSort5 : 0.000272s
86-
quickSort6 : 0.000227s
87-
sort : 0.000212s
88-
stable_sort : 0.000205s
89-
ANSICqsort : 0.000550s
90-
stdSetSort : 0.000754s
91-
bstSort : 0.000310s
75+
bubbleSort : 0.016801s
76+
bubbleSort2 : 0.026581s
77+
selectionSort : 0.030344s
78+
insertionSort : 0.003837s
79+
shellSort : 0.000622s
80+
heapSort : 0.000345s
81+
mergeSort : 0.000201s
82+
mergeSort2 : 0.000211s
83+
mergeSort3 : 0.000172s
84+
mergeSort4 : 0.000232s
85+
quickSort : 0.000225s
86+
quickSort2 : 0.000224s
87+
quickSort3 : 0.000312s
88+
quickSort4 : 0.000199s
89+
quickSort5 : 0.000224s
90+
quickSort6 : 0.000211s
91+
sort : 0.000181s
92+
stable_sort : 0.000196s
93+
ANSICqsort : 0.000529s
94+
stdSetSort : 0.000623s
95+
bstSort : 0.000306s
9296
@@@@ len = 7.998K,
93-
bubbleSort : 0.118685s
94-
bubbleSort2 : 0.123571s
95-
selectionSort : 0.122373s
96-
insertionSort : 0.018363s
97-
shellSort : 0.001087s
98-
heapSort : 0.000890s
99-
mergeSort : 0.000416s
100-
mergeSort2 : 0.000443s
101-
mergeSort3 : 0.000355s
102-
quickSort : 0.000564s
103-
quickSort2 : 0.000572s
104-
quickSort3 : 0.000822s
105-
quickSort4 : 0.000535s
106-
quickSort5 : 0.000587s
107-
quickSort6 : 0.000492s
108-
sort : 0.000457s
109-
stable_sort : 0.000422s
110-
ANSICqsort : 0.001178s
111-
stdSetSort : 0.001639s
112-
bstSort : 0.000697s
97+
bubbleSort : 0.090263s
98+
bubbleSort2 : 0.110506s
99+
selectionSort : 0.124322s
100+
insertionSort : 0.015273s
101+
shellSort : 0.001016s
102+
heapSort : 0.000759s
103+
mergeSort : 0.000418s
104+
mergeSort2 : 0.000421s
105+
mergeSort3 : 0.000341s
106+
mergeSort4 : 0.000504s
107+
quickSort : 0.000490s
108+
quickSort2 : 0.000483s
109+
quickSort3 : 0.000668s
110+
quickSort4 : 0.000446s
111+
quickSort5 : 0.000494s
112+
quickSort6 : 0.000438s
113+
sort : 0.000370s
114+
stable_sort : 0.000430s
115+
ANSICqsort : 0.001159s
116+
stdSetSort : 0.001366s
117+
bstSort : 0.000712s
113118
@@@@ len = 15.999K,
114-
bubbleSort : 0.478276s
115-
bubbleSort2 : 0.492027s
116-
selectionSort : 0.483548s
117-
insertionSort : 0.072853s
118-
shellSort : 0.002474s
119-
heapSort : 0.001911s
120-
mergeSort : 0.000883s
121-
mergeSort2 : 0.000956s
122-
mergeSort3 : 0.000783s
123-
quickSort : 0.001217s
124-
quickSort2 : 0.001238s
125-
quickSort3 : 0.001716s
126-
quickSort4 : 0.001163s
127-
quickSort5 : 0.001221s
128-
quickSort6 : 0.001058s
129-
sort : 0.000993s
130-
stable_sort : 0.000961s
131-
ANSICqsort : 0.002528s
132-
stdSetSort : 0.003949s
133-
bstSort : 0.001610s
119+
bubbleSort : 0.428157s
120+
bubbleSort2 : 0.443338s
121+
selectionSort : 0.498512s
122+
insertionSort : 0.061895s
123+
shellSort : 0.002262s
124+
heapSort : 0.001639s
125+
mergeSort : 0.000924s
126+
mergeSort2 : 0.000937s
127+
mergeSort3 : 0.000780s
128+
mergeSort4 : 0.001058s
129+
quickSort : 0.001052s
130+
quickSort2 : 0.001060s
131+
quickSort3 : 0.001413s
132+
quickSort4 : 0.000957s
133+
quickSort5 : 0.001046s
134+
quickSort6 : 0.000961s
135+
sort : 0.000823s
136+
stable_sort : 0.000963s
137+
ANSICqsort : 0.002565s
138+
stdSetSort : 0.003261s
139+
bstSort : 0.001652s
134140
@@@@ len = 16.000K, sorted
135-
bubbleSort : 0.100523s
136-
bubbleSort2 : 0.000012s
137-
selectionSort : 0.097883s
138-
insertionSort : 0.000023s
139-
shellSort : 0.000488s
140-
heapSort : 0.001497s
141-
mergeSort : 0.000654s
142-
mergeSort2 : 0.000654s
143-
mergeSort3 : 0.000474s
144-
quickSort : 0.063353s
145-
quickSort2 : 0.000426s
146-
quickSort3 : 0.001058s
147-
quickSort4 : 0.000322s
148-
quickSort5 : 0.000549s
149-
quickSort6 : 0.000278s
150-
sort : 0.000267s
151-
stable_sort : 0.000437s
152-
ANSICqsort : 0.001068s
153-
stdSetSort : 0.002391s
154-
bstSort : 0.001360s
141+
bubbleSort : 0.091772s
142+
bubbleSort2 : 0.000011s
143+
selectionSort : 0.136723s
144+
insertionSort : 0.000021s
145+
shellSort : 0.000486s
146+
heapSort : 0.001327s
147+
mergeSort : 0.000608s
148+
mergeSort2 : 0.000550s
149+
mergeSort3 : 0.000423s
150+
mergeSort4 : 0.000754s
151+
quickSort : 0.057623s
152+
quickSort2 : 0.000330s
153+
quickSort3 : 0.000880s
154+
quickSort4 : 0.000241s
155+
quickSort5 : 0.000455s
156+
quickSort6 : 0.000170s
157+
sort : 0.000154s
158+
stable_sort : 0.000440s
159+
ANSICqsort : 0.000800s
160+
stdSetSort : 0.001619s
161+
bstSort : 0.001105s
155162
@@@@ len = 256.001K,
156-
shellSort : 0.155103s
157-
heapSort : 0.042918s
158-
mergeSort : 0.017492s
159-
mergeSort2 : 0.018633s
160-
mergeSort3 : 0.015845s
161-
quickSort : 0.024810s
162-
quickSort2 : 0.025433s
163-
quickSort3 : 0.033174s
164-
quickSort4 : 0.023976s
165-
quickSort5 : 0.024547s
166-
quickSort6 : 0.021576s
167-
sort : 0.020152s
168-
stable_sort : 0.019599s
169-
ANSICqsort : 0.050289s
170-
stdSetSort : 0.140234s
171-
bstSort : 0.062390s
163+
shellSort : 0.161508s
164+
heapSort : 0.035582s
165+
mergeSort : 0.017815s
166+
mergeSort2 : 0.018205s
167+
mergeSort3 : 0.016152s
168+
mergeSort4 : 0.020804s
169+
quickSort : 0.022059s
170+
quickSort2 : 0.021702s
171+
quickSort3 : 0.027312s
172+
quickSort4 : 0.019861s
173+
quickSort5 : 0.021092s
174+
quickSort6 : 0.019686s
175+
sort : 0.016512s
176+
stable_sort : 0.019270s
177+
ANSICqsort : 0.051030s
178+
stdSetSort : 0.178732s
179+
bstSort : 0.064254s
172180
@@@@ len = 1025.000K,
173-
shellSort : 0.378536s
174-
heapSort : 0.211736s
175-
mergeSort : 0.075987s
176-
mergeSort2 : 0.079016s
177-
mergeSort3 : 0.069840s
178-
quickSort : 0.109760s
179-
quickSort2 : 0.111771s
180-
quickSort3 : 0.142622s
181-
quickSort4 : 0.106491s
182-
quickSort5 : 0.110362s
183-
quickSort6 : 0.095375s
184-
sort : 0.089236s
185-
stable_sort : 0.083558s
186-
ANSICqsort : 0.219952s
187-
stdSetSort : 1.004901s
188-
bstSort : 0.418940s
181+
shellSort : 0.374373s
182+
heapSort : 0.185371s
183+
mergeSort : 0.078237s
184+
mergeSort2 : 0.081115s
185+
mergeSort3 : 0.070777s
186+
mergeSort4 : 0.089369s
187+
quickSort : 0.095600s
188+
quickSort2 : 0.096249s
189+
quickSort3 : 0.119631s
190+
quickSort4 : 0.088815s
191+
quickSort5 : 0.093334s
192+
quickSort6 : 0.088196s
193+
sort : 0.073647s
194+
stable_sort : 0.085703s
195+
ANSICqsort : 0.223894s
196+
stdSetSort : 1.095602s
197+
bstSort : 0.547030s

0 commit comments

Comments
 (0)
Please sign in to comment.