[go: up one dir, main page]

Menu

[r303]: / libpetey / quicksort.cc  Maximize  Restore  History

Download this file

208 lines (172 with data), 6.5 kB

  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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <stdio.h>
namespace libpetey {
template<class type>
long qslt_partition(type *array, long left, long right, long pivotIndex) {
type pivotValue;
type swap;
long storeIndex;
pivotValue = array[pivotIndex];
swap=array[pivotIndex];
array[pivotIndex]=array[right]; // Move pivot to end
array[right]=swap;
storeIndex = left;
for (long i=left; i<right; i++) { // left = i < right
if (array[i] <= pivotValue) {
//swap array[i] and array[storeIndex]
swap=array[storeIndex];
array[storeIndex]=array[i]; // Move pivot to end
array[i]=swap;
storeIndex = storeIndex + 1;
}
}
//swap array[storeIndex] and array[right] // Move pivot to its final place
swap=array[storeIndex];
array[storeIndex]=array[right]; // Move pivot to its final place
array[right]=swap;
return storeIndex;
}
template<class type>
long qslt_partition(type *array, long *ind, long left, long right, long pivotIndex) {
type pivotValue;
long swap;
long storeIndex;
pivotValue = array[ind[pivotIndex]];
swap=ind[pivotIndex];
ind[pivotIndex]=ind[right]; // Move pivot to end
ind[right]=swap;
storeIndex = left;
for (long i=left; i<right; i++) { // left = i < right
if (array[ind[i]] <= pivotValue) {
//swap array[i] and array[storeIndex]
swap=ind[storeIndex];
ind[storeIndex]=ind[i]; // Move pivot to end
ind[i]=swap;
storeIndex = storeIndex + 1;
}
}
//swap array[storeIndex] and array[right] // Move pivot to its final place
swap=ind[storeIndex];
ind[storeIndex]=ind[right]; // Move pivot to its final place
ind[right]=swap;
return storeIndex;
}
template<class type>
long qsgt_partition(type *array, long left, long right, long pivotIndex) {
type pivotValue;
type swap;
long storeIndex;
pivotValue = array[pivotIndex];
swap=array[pivotIndex];
array[pivotIndex]=array[right]; // Move pivot to end
array[right]=swap;
storeIndex = left;
for (long i=left; i<right; i++) { // left = i < right
if (array[i] >= pivotValue) {
//swap array[i] and array[storeIndex]
swap=array[storeIndex];
array[storeIndex]=array[i]; // Move pivot to end
array[i]=swap;
storeIndex = storeIndex + 1;
}
}
//swap array[storeIndex] and array[right] // Move pivot to its final place
swap=array[storeIndex];
array[storeIndex]=array[right]; // Move pivot to its final place
array[right]=swap;
return storeIndex;
}
template<class type>
long qsgt_partition(type *array, long *ind, long left, long right, long pivotIndex) {
type pivotValue;
long swap;
long storeIndex;
pivotValue = array[ind[pivotIndex]];
swap=ind[pivotIndex];
ind[pivotIndex]=ind[right]; // Move pivot to end
ind[right]=swap;
storeIndex = left;
for (long i=left; i<right; i++) { // left = i < right
if (array[ind[i]] >= pivotValue) {
//swap array[i] and array[storeIndex]
swap=ind[storeIndex];
ind[storeIndex]=ind[i]; // Move pivot to end
ind[i]=swap;
storeIndex = storeIndex + 1;
}
}
//swap array[storeIndex] and array[right] // Move pivot to its final place
swap=ind[storeIndex];
ind[storeIndex]=ind[right]; // Move pivot to its final place
ind[right]=swap;
return storeIndex;
}
//cribbed straight from Wikipedia:
template<class type>
void quicksort(type *array, long n, long left, long right) {
long pivotIndex;
long pivotNewIndex;
//for (long i=0; i<n; i++) printf("%g ", array[i]);
//printf("\n");
if (right > left) {
pivotIndex = (left+right)/2;
pivotNewIndex = qslt_partition(array, left, right, pivotIndex);
quicksort(array, n, left, pivotNewIndex - 1);
quicksort(array, n, pivotNewIndex + 1, right);
}
}
template<class type>
void quicksort(type *data, long n) {
quicksort(data, n, 0, n-1);
}
template void quicksort<float>(float *data, long n);
template void quicksort<double>(double *data, long n);
template<class type>
void kleast_quick(type *array, long n, long k, long left, long right) {
long pivotIndex;
long pivotNewIndex;
//for (long i=0; i<n; i++) printf("%g ", array[i]);
//printf("\n");
if (right > left) {
pivotIndex = (left+right)/2;
pivotNewIndex = qslt_partition(array, left, right, pivotIndex);
if (pivotNewIndex >= k) kleast_quick(array, n, k, left, pivotNewIndex - 1);
//kleast(array, n, k, left, pivotNewIndex - 1);
if (pivotNewIndex+1 < k) kleast_quick(array, n, k, pivotNewIndex + 1, right);
}
}
template<class type>
void kleast_quick(type *data, long n, long k, type *result) {
kleast_quick(data, n, k, 0, n-1);
for (long i=0; i<k; i++) result[i]=data[i];
}
template void kleast_quick<float>(float *, long, long, float *);
template void kleast_quick<double>(double *, long, long, double *);
template<class type>
void kleast_quick(type *array, long n, long k, long *ind, long left, long right) {
long pivotIndex;
long pivotNewIndex;
//for (long i=0; i<n; i++) printf("%g ", array[i]);
//printf("\n");
if (right > left) {
pivotIndex = (left+right)/2;
pivotNewIndex = qslt_partition(array, ind, left, right, pivotIndex);
if (pivotNewIndex >= k) kleast_quick(array, n, k, ind, left, pivotNewIndex - 1);
//kleast(array, n, k, left, pivotNewIndex - 1);
if (pivotNewIndex+1 < k) kleast_quick(array, n, k, ind, pivotNewIndex + 1, right);
}
}
template<class type>
void kleast_quick(type *data, long n, long k, type *result, long *ind) {
long *ind2;
ind2=new long[n];
for (long i=0; i<n; i++) ind2[i]=i;
kleast_quick(data, n, k, ind2, 0, n-1);
for (long i=0; i<k; i++) {
result[i]=data[ind2[i]];
ind[i]=ind2[i];
}
delete [] ind2;
}
template void kleast_quick<float>(float *, long, long, float *, long *);
template void kleast_quick<double>(double *, long, long, double *, long *);
} //end namespace libpetey