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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
/*
* Copyright 2002 Adrian Thurston <thurston@cs.queensu.ca>
*/
/* This file is part of Aapl.
*
* Aapl is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* Aapl is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
* more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Aapl; if not, write to the Free Software Foundation, Inc., 59
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef _AAPL_QUICKSORT_H
#define _AAPL_QUICKSORT_H
#include "insertsort.h"
#ifdef AAPL_NAMESPACE
namespace Aapl {
#endif
/**
* \addtogroup sort
* @{
*/
/**
* \class QuickSort
* \brief Quick sort an array of data.
*
* QuickSort can be used to sort any array of objects of type T provided a
* compare class is given. QuickSort is in-place. It does not require any
* temporary storage.
*
* Objects are not made aware that they are being moved around in memory.
* Assignment operators, constructors and destructors are never invoked by the
* sort.
*
* QuickSort runs in O(n*log(n)) time in the average case. It is faster than
* mergsort in the average case because it does less moving of data. The
* performance of quicksort depends mostly on the choice of pivot. This
* implementation picks the pivot as the median of first, middle, last. This
* choice of pivot avoids the O(n^2) worst case for input already sorted, but
* it is still possible to encounter the O(n^2) worst case. For example an
* array of identical elements will run in O(n^2)
*
* QuickSort is not a stable sort. Elements with the same key will not have
* their relative ordering preserved. QuickSort switches to an InsertSort
* when the size of the array being sorted is small. This happens when
* directly sorting a small array or when QuickSort calls iteself recursively
* on a small portion of a larger array.
*/
/*@}*/
/* QuickSort. */
template <class T, class Compare> class QuickSort :
public InsertSort<T, Compare>
{
public:
/* Sorting interface routine. */
void sort(T *data, long len);
private:
/* Recursive worker. */
void doSort(T *start, T *end);
T *partition(T *start, T *end);
inline T *median(T *start, T *end);
};
#define _QS_INSERTION_THRESH 16
/* Finds the median of start, middle, end. */
template <class T, class Compare> T *QuickSort<T,Compare>::
median(T *start, T *end)
{
T *pivot, *mid = start + (end-start)/2;
/* CChoose the pivot. */
if ( compare(*start, *mid) < 0 ) {
if ( compare(*mid, *end) < 0 )
pivot = mid;
else if ( compare(*start, *end) < 0 )
pivot = end;
else
pivot = start;
}
else if ( compare(*start, *end) < 0 )
pivot = start;
else if ( compare(*mid, *end) < 0 )
pivot = end;
else
pivot = mid;
return pivot;
}
template <class T, class Compare> T *QuickSort<T,Compare>::
partition(T *start, T *end)
{
/* Use the median of start, middle, end as the pivot. First save
* it off then move the last element to the free spot. */
char pcPivot[sizeof(T)];
T *pivot = median(start, end);
memcpy( pcPivot, pivot, sizeof(T) );
if ( pivot != end )
memcpy( pivot, end, sizeof(T) );
T *first = start-1;
T *last = end;
pivot = (T*) pcPivot;
/* Shuffle element to the correct side of the pivot, ending
* up with the free spot where the pivot will go. */
while ( true ) {
/* Throw one element ahead to the free spot at last. */
while ( true ) {
first += 1;
if ( first == last )
goto done;
if ( compare( *first, *pivot ) > 0 ) {
memcpy(last, first, sizeof(T));
break;
}
}
/* Throw one element back to the free spot at first. */
while ( true ) {
last -= 1;
if ( last == first )
goto done;
if ( compare( *last, *pivot ) < 0 ) {
memcpy(first, last, sizeof(T));
break;
}
}
}
done:
/* Put the pivot into the middle spot for it. */
memcpy( first, pivot, sizeof(T) );
return first;
}
template< class T, class Compare> void QuickSort<T,Compare>::
doSort(T *start, T *end)
{
long len = end - start + 1;
if ( len > _QS_INSERTION_THRESH ) {
/* Use quicksort. */
T *pivot = partition( start, end );
doSort(start, pivot-1);
doSort(pivot+1, end);
}
else if ( len > 1 ) {
/* Array is small, use insertion sort. */
InsertSort<T, Compare>::sort( start, len );
}
}
/**
* \brief Quick sort an array of data.
*/
template< class T, class Compare>
void QuickSort<T,Compare>::sort(T *data, long len)
{
/* Call recursive worker. */
doSort(data, data+len-1);
}
#ifdef AAPL_NAMESPACE
}
#endif
#endif /* _AAPL_QUICKSORT_H */
|