/
Quick sort Code Snippet.h
56 lines (43 loc) · 1.23 KB
/
Quick sort Code Snippet.h
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
//-----------------------------------------------------------
// Game::Sort
// Sort the dots according to depth
//-----------------------------------------------------------
//////////////////////////////////////////////////////////////////////////
void Game::QuickSort(float3* array, int startIndex, int endIndex)
{
float3 pivot = array[startIndex];
int splitPoint;
if (endIndex > startIndex)
{
splitPoint = SplitArray(array, pivot, startIndex, endIndex);
array[splitPoint] = pivot;
QuickSort(array, startIndex, splitPoint - 1);
QuickSort(array, splitPoint + 1, endIndex);
}
}
int Game::SplitArray(float3* array, float3 pivot, int startIndex, int endIndex)
{
int leftBoundary = startIndex;
int rightBoundary = endIndex;
while (leftBoundary < rightBoundary)
{
while (pivot.z < array[rightBoundary].z && rightBoundary > leftBoundary)
{
rightBoundary--;
}
swap(array[leftBoundary], array[rightBoundary]);
while (pivot.z >= array[leftBoundary].z && leftBoundary < rightBoundary)
{
leftBoundary++;
}
swap(array[rightBoundary], array[leftBoundary]);
}
return leftBoundary;
}
void Game::swap(float3 &a, float3 &b)
{
float3 temp;
temp = a;
a = b;
b = temp;
}