-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSorting.cpp
More file actions
69 lines (57 loc) · 1.27 KB
/
Sorting.cpp
File metadata and controls
69 lines (57 loc) · 1.27 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
#include <iostream>
contexpr void swap(int& a, int& b)
{
a ^= b ^= a ^= b;
}
void Negate(int* vals, size_t count)
{
for (int ii = count; ii--;)
vals[ii] *= -1;
}
void RadixSort(int* vals, size_t count, int bit)
{
size_t front = 0, back = count-1;
bool const signBit = (bit == 0x80000000);
while (front <= back)
{
// LOOP INVARIANT
// if signBit is true, then :
// vals[i] is positive for all 0 <= i < front
// vals[i[ is negative for all back < i <= count-1
// else:
// bit & vals[i] is set for all 0 <= i < front
// bit & vals[i] is clear for all back < i <= count-1
if (!!(vals[front] & bit) == signBit)
++front;
else if (!(vals[back] & bit) == signBit)
--back;
else
swap(vals[front], vals[back]);
++front;
--back;
}
if (signBit)
Negate(vals + front, count - front);
bit >>= 1;
if (bit)
{
RadixSort(vals, front, bit);
RadixSort(vals + front, count - front, bit);
}
if (signBit)
Negate(vals + front, count - front);
}
/*
*/
vector<int> randomArray(size_t count)
{
vector<int> vals(count);
for (int val& : vals)
val = rand();
}
void main()
{
vector<int> vals = randomArray(42);
RadixSort(&vals.first(), vals.count());
bool correct =
}