| 260 | /// <param name="count">The elements count.</param> |
| 261 | template<typename T> |
| 262 | static void QuickSortObj(T* data, int32 count) |
| 263 | { |
| 264 | if (count < 2) |
| 265 | return; |
| 266 | auto& stack = SortingStack::Get(); |
| 267 | |
| 268 | // Push left and right |
| 269 | stack.Push(0); |
| 270 | stack.Push(count - 1); |
| 271 | |
| 272 | // Keep sorting from stack while is not empty |
| 273 | while (stack.Count) |
| 274 | { |
| 275 | // Pop right and left |
| 276 | int32 right = stack.Pop(); |
| 277 | const int32 left = stack.Pop(); |
| 278 | |
| 279 | // Partition |
| 280 | T x = data[right]; |
| 281 | int32 i = left - 1; |
| 282 | for (int32 j = left; j <= right - 1; j++) |
| 283 | { |
| 284 | if (*data[j] < *x) |
| 285 | { |
| 286 | i++; |
| 287 | Swap(data[i], data[j]); |
| 288 | } |
| 289 | } |
| 290 | Swap(data[i + 1], data[right]); |
| 291 | const int32 pivot = i + 1; |
| 292 | |
| 293 | // If there are elements on left side of pivot, then push left side to stack |
| 294 | if (pivot - 1 > left) |
| 295 | { |
| 296 | stack.Push(left); |
| 297 | stack.Push(pivot - 1); |
| 298 | } |
| 299 | |
| 300 | // If there are elements on right side of pivot, then push right side to stack |
| 301 | if (pivot + 1 < right) |
| 302 | { |
| 303 | stack.Push(pivot + 1); |
| 304 | stack.Push(right); |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | /// <summary> |
| 310 | /// Sorts the linear data array using Merge Sort algorithm (recursive version, uses temporary memory). |