MCPcopy Create free account
hub / github.com/FlaxEngine/FlaxEngine / QuickSort

Function QuickSort

Source/Engine/Core/Collections/Sorting.h:107–152  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

105 /// <param name="count">The elements count.</param>
106 template<typename T>
107 static void QuickSort(T* data, int32 count)
108 {
109 if (count < 2)
110 return;
111 auto& stack = SortingStack::Get();
112
113 // Push left and right
114 stack.Push(0);
115 stack.Push(count - 1);
116
117 // Keep sorting from stack while is not empty
118 while (stack.Count)
119 {
120 // Pop right and left
121 int32 right = stack.Pop();
122 const int32 left = stack.Pop();
123
124 // Partition
125 T* x = &data[right];
126 int32 i = left - 1;
127 for (int32 j = left; j <= right - 1; j++)
128 {
129 if (data[j] < *x)
130 {
131 i++;
132 Swap(data[i], data[j]);
133 }
134 }
135 Swap(data[i + 1], data[right]);
136 const int32 pivot = i + 1;
137
138 // If there are elements on left side of pivot, then push left side to stack
139 if (pivot - 1 > left)
140 {
141 stack.Push(left);
142 stack.Push(pivot - 1);
143 }
144
145 // If there are elements on right side of pivot, then push right side to stack
146 if (pivot + 1 < right)
147 {
148 stack.Push(pivot + 1);
149 stack.Push(right);
150 }
151 }
152 }
153
154 /// <summary>
155 /// Sorts the linear data array using Quick Sort algorithm (non recursive version, uses temporary stack collection).

Callers 15

Sorting.hFile · 0.70
BlendSettingsMethod · 0.50
SetupLightsMethod · 0.50
DrawDecalsMethod · 0.50
RenderMethod · 0.50
packChartsMethod · 0.50
DumpFunction · 0.50
UpdateGPUFunction · 0.50
SetupRecursiveMethod · 0.50
DumpResourcesToLogMethod · 0.50
OnCollectDrawCallsMethod · 0.50
SortMethod · 0.50

Calls 5

compareFunction · 0.85
GetFunction · 0.70
SwapFunction · 0.70
PushMethod · 0.45
PopMethod · 0.45

Tested by

no test coverage detected