Skip to content

Commit 7d39924

Browse files
committedMar 27, 2014
C++/AlgoAndADT: 3-way quicksort
1 parent ef64e99 commit 7d39924

File tree

2 files changed

+201
-175
lines changed

2 files changed

+201
-175
lines changed
 

‎C++/AlgoAndADT/sort.h

+17
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,22 @@ static void quickSort(T *begin, T *end) {
326326
quickSort(cur + 1, end);
327327
}
328328

329+
template<typename T>
330+
static void quickSort1(T *begin, T *end) {
331+
if (end - begin <= 1) return;
332+
333+
T *lo = begin + 1, *hi = end - 1;
334+
for (T *p = lo; p <= hi; ) {
335+
if (*p < *begin) swap(*p++, *lo++);
336+
else if (*p > *begin) swap(*p, *hi--);
337+
else ++p;
338+
}
339+
swap(*begin, lo[-1]);
340+
341+
quickSort1(begin, lo - 1);
342+
quickSort1(hi + 1, end);
343+
}
344+
329345
template<typename T>
330346
static void quickSort2(T *begin, T *end) {
331347
if (end - begin <= 1) return;
@@ -557,6 +573,7 @@ int main() {
557573
ITEM(mergeSort3, 0),
558574
ITEM(mergeSort4, 0),
559575
ITEM(quickSort, 0),
576+
ITEM(quickSort1, 0),
560577
ITEM(quickSort2, 0),
561578
ITEM(quickSort3, 0),
562579
ITEM(quickSort4, 0),

0 commit comments

Comments
 (0)