sorting.hpp 6.32 KB
Newer Older
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// Software License for MTL
// 
// Copyright (c) 2007 The Trustees of Indiana University.
//               2008 Dresden University of Technology and the Trustees of Indiana University.
//               2010 SimuNova UG (haftungsbeschränkt), www.simunova.com.
// All rights reserved.
// Authors: Peter Gottschling and Andrew Lumsdaine
// 
// This file is part of the Matrix Template Library
// 
// See also license.mtl.txt in the distribution.
//
// Algorithm inspired by Nick Vannieuwenhoven, written by Cornelius Steinhardt

/*
 *
 *  Created on: Nov 3, 2009
 *      Author: heazk
 */

#ifndef MTL_SORTING_INCLUDE
#define MTL_SORTING_INCLUDE

#include <string.h>
#include <stdlib.h>

#define BITS_PER_BYTE 8

////////////////////////////////////////////////////////////////////////////////
// RADIX-SORT
////////////////////////////////////////////////////////////////////////////////

template<
	class Type
>
void radix_sort(
	Type* values,
	const int size,
	Type* tmp = 0
) {
	if(size < 2) {
		return;
	}

	Type *const orig_ptr = values;
	if(tmp == 0) {
		tmp = new Type[size];
	}

#if VERBOSE_MODE > 10
	std::cout << "unsorted: ";
	for(int i = 0; i < size; ++i) {
		std::cout << values[i] << ", ";
	}
	std::cout << std::endl;
#endif

	// Determine the maximum number of bits needed.
	int max = INT_MIN;
	for(int i = 0; i < size; ++i) {
		max = (values[i] <= max) ? max : values[i];
	}
	int maxBits = 1;
	for(; max; max >>= 1, ++maxBits){};
	for(max = 0; max < maxBits; max += BITS_PER_BYTE){};
	maxBits = max;

	const int groupBits = BITS_PER_BYTE;
	const int intBits   = maxBits;
	const int groupSize = 1 << groupBits;

	const int nbIterations = intBits / groupBits;
	const int mask = groupSize-1;

	// Counting and prefix arrays.
	int* count = new int[groupSize];

	for (
			int shift = 0, it = 0;
			it < nbIterations;
			++it, shift += groupBits
	) {
		// Reset count array.
		memset(count, 0, groupSize*sizeof(int));

		// Counting elements in the it-th group.
		for (int i = 0; i < size; ++i) {
			count[ (values[i] >> shift) & mask ]++;
		}

		// Calculate prefixes.
		int currIncrement = -1;
		int prevCount = count[0];
		count[0] = 0;
		for (int i = 1; i < groupSize; ++i) {
			currIncrement = prevCount;
			prevCount = count[i];
			count[i] = count[i-1] + currIncrement;
		}

		// Sort elements in the it-th group.
		for (int i = 0; i < size; ++i) {
			int& index = count[( values[i] >> shift) & mask];
			tmp[ index ] = values[i];
			++index;
		}

		// Swap pointers.
		Type* tmp_values = values;
		values = tmp;
		tmp = tmp_values;
	}

	// Make sure the sorted keys and values reside in the input array.
	if( values != orig_ptr ) {
		memcpy( orig_ptr, values, sizeof(Type)*size );
		tmp = values;
	}

#if VERBOSE_MODE > 10
	std::cout << "sorted: ";
	for(int i = 0; i < size; ++i) {
		std::cout << values[i] << ", ";
	}
	std::cout << std::endl;
#endif

	if(count) {
		delete[] count;
	}
	if(tmp) {
		delete[] tmp;
	}
}

template<
	class Key, class Val
>
void radix_sort(
		Key* keys, Val* vals,
		const int size,
		Key* tmpKeys = 0, Val* tmpVals = 0
) {
	if(size < 2) {
		return;
	}

	// The pointers to the original data.
	Key *const origKey = keys;
	Val *const origVal = vals;

	// Helper arrays.
	bool ownKeys = false;
	bool ownVals = false;
	if(!tmpKeys) {
		tmpKeys = new Key[size];
		ownKeys = true;
	}
	if(!tmpVals) {
		tmpVals = new Val[size];
		ownVals = true;
	}

	// Determine the maximum number of bits needed.
	int max = -(INT_MAX-1);
	for(int i = 0; i < size; ++i) {
		max = keys[i] <= max ? max : keys[i];
	}
	int maxBits = 1;
	for(; max; max >>= 1, ++maxBits){};
	for(max = 0; max < maxBits; max += BITS_PER_BYTE){};
	maxBits = max;

	const int groupBits = BITS_PER_BYTE;
	const int intBits   = maxBits;
	const int groupSize = 1 << groupBits;

	const int nbIterations = intBits / groupBits;
	const int mask = groupSize-1;

	// Counting and prefix arrays.
	int* count = new int[groupSize];
	// Allocation of memory could fail!

	for (
			int shift = 0, it = 0;
			it < nbIterations;
			++it, shift += groupBits
	) {
		// Reset count array.
		for(int i = 0; i < groupSize; ++i) {
			count[i] = 0;
		}

		// Counting elements in the it-th group.
		for (int i = 0; i < size; ++i) {
			count[ (keys[i] >> shift) & mask ]++;
		}

		// Calculate prefixes.
		int currIncrement = -1;
		int prevCount = count[0];
		count[0] = 0;
		for (int i = 1; i < groupSize; ++i) {
			currIncrement = prevCount;
			prevCount = count[i];
			count[i] = count[i-1] + currIncrement;
		}

		// Sort elements in the it-th group.
		for (int i = 0; i < size; ++i) {
			int& index = count[( keys[i] >> shift) & mask];
			tmpKeys[ index ] = keys[i];
			tmpVals[ index ] = vals[i];
			++index;
		}

		// Swap pointers.
		Key* tmpKeyPtr = tmpKeys;
		Val* tmpValuePtr = tmpVals;
		tmpKeys = keys;
		tmpVals = vals;
		keys = tmpKeyPtr;
		vals = tmpValuePtr;
	}

	// Make sure the sorted keys and values reside in the input array.
	if(keys != origKey) {
		memcpy( origKey, keys, sizeof(Key)*size );
		tmpKeys = keys;
	}
	if(vals != origVal) {
		memcpy( origVal, vals, sizeof(Val)*size );
		tmpVals = vals;
	}

	if(count) 				delete[] count;
	if(ownKeys && tmpKeys) 	delete[] tmpKeys;
	if(ownVals && tmpVals)	delete[] tmpVals;
	tmpKeys = 0;
	tmpVals = 0;
	count = 0;
}

////////////////////////////////////////////////////////////////////////////////
// QUICK-SORT
////////////////////////////////////////////////////////////////////////////////

template<typename Key, typename Val>
inline void swap(Key& key0, Val& val0, Key& key1, Val& val1) {
	Key tmp_key = key0;
	Val tmp_val = val0;
	key0 = key1;
	val0 = val1;
	key1 = tmp_key;
	val1 = tmp_val;
}

template<typename Key, typename Value>
inline void sort_along(Key* keys, Value* values, int size) {


	if(size < 2) {
		return;
	}
	if( size == 2 ) {
		if(keys[0] > keys[1]) {
			swap(
				keys[0], values[0],
				keys[1], values[1]
			);
		}
		return;
	}

	int piv = std::rand() % size;


	swap(
		keys[0],   values[0],
		keys[piv], values[piv]
	);
	int begin = 1;
	int end = size-1;
	while(begin < end) {
		for(; (begin < end) && (keys[begin] <= keys[0]); ++begin){};
		for(; (begin <= end) && (keys[end] > keys[0]); --end){};

		if(begin < end) {
			swap(
				keys[begin], values[begin],
				keys[end],   values[end]
			);
		}
	}
	piv = end;
	swap(
		keys[0],   values[0],
		keys[piv], values[piv]
	);


	sort_along( keys, values, piv );
	sort_along( keys+(piv+1), values+(piv+1), size-piv-1);
}

#endif // MTL_SORTING_INCLUDE