15 class SCR_SortCompare<Class T>
20 static int Compare(T left, T right);
27 class SCR_Sorting<Class T, SCR_SortCompare TCompare>
32 static void HeapSort(array<T> a,
bool inverse =
false)
35 Heap_Init(a, inverse);
37 int endId = a.Count() - 1;
48 Heap_SiftDown(a, 0, endId, inverse);
61 static int Heap_IdParent(
int nodeId)
64 return (nodeId - 1) >> 1;
68 static int Heap_IdLeftChild(
int nodeId)
75 static void Heap_SiftDown(array<T> a,
int startId,
int endId,
bool inverse)
80 int invertMask = inverse;
82 while (Heap_IdLeftChild(rootId) <= endId)
84 int leftChildId = Heap_IdLeftChild(rootId);
85 int rightChildId = leftChildId + 1;
89 if (TCompare.Compare(a[maxId], a[leftChildId]) ^ invertMask)
91 if (rightChildId <= endId)
93 if (TCompare.Compare(a[maxId], a[rightChildId]) ^ invertMask)
104 a[maxId] = a[rootId];
113 static void Heap_Init(array<T> a,
bool inverse)
115 int count = a.Count();
117 int startId = Heap_IdParent(count - 1);
121 Heap_SiftDown(a, startId, count - 1, inverse);