| 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). |
no test coverage detected