| | 1 | | using System; |
| | 2 | |
|
| | 3 | | namespace Itinero.IO.Osm.Collections; |
| | 4 | |
|
| | 5 | | internal static class QuickSort |
| | 6 | | { |
| | 7 | | public static void Sort(Func<long, long> value, Action<long, long> swap, long left, long right) |
| 0 | 8 | | { |
| 0 | 9 | | Sort((i1, i2) => value(i1).CompareTo(value(i2)), swap, left, right); |
| 0 | 10 | | } |
| | 11 | |
|
| | 12 | | public static void Sort(Func<long, long, int> compare, Action<long, long> swap, long left, long right) |
| 0 | 13 | | { |
| 0 | 14 | | if (left >= right) |
| 0 | 15 | | { |
| 0 | 16 | | return; |
| | 17 | | } |
| | 18 | |
|
| 0 | 19 | | var stack = new System.Collections.Generic.Stack<Pair>(); |
| 0 | 20 | | stack.Push(new Pair(left, right)); |
| 0 | 21 | | while (stack.Count > 0) |
| 0 | 22 | | { |
| 0 | 23 | | var pair = stack.Pop(); |
| 0 | 24 | | var pivot = Partition(compare, swap, pair.Left, pair.Right); |
| 0 | 25 | | if (pair.Left < pivot) |
| 0 | 26 | | { |
| 0 | 27 | | stack.Push(new Pair(pair.Left, pivot - 1)); |
| 0 | 28 | | } |
| | 29 | |
|
| 0 | 30 | | if (pivot < pair.Right) |
| 0 | 31 | | { |
| 0 | 32 | | stack.Push(new Pair(pivot + 1, pair.Right)); |
| 0 | 33 | | } |
| 0 | 34 | | } |
| 0 | 35 | | } |
| | 36 | |
|
| | 37 | | public static bool IsSorted(Func<long, long> value, long left, long right) |
| 0 | 38 | | { |
| 0 | 39 | | var previous = value(left); |
| 0 | 40 | | for (var i = left + 1; i <= right; i++) |
| 0 | 41 | | { |
| 0 | 42 | | var val = value(i); |
| 0 | 43 | | if (previous > val) |
| 0 | 44 | | { |
| 0 | 45 | | return false; |
| | 46 | | } |
| | 47 | |
|
| 0 | 48 | | previous = val; |
| 0 | 49 | | } |
| | 50 | |
|
| 0 | 51 | | return true; |
| 0 | 52 | | } |
| | 53 | |
|
| | 54 | | private struct Pair |
| | 55 | | { |
| | 56 | | public Pair(long left, long right) |
| 0 | 57 | | : this() |
| 0 | 58 | | { |
| 0 | 59 | | this.Left = left; |
| 0 | 60 | | this.Right = right; |
| 0 | 61 | | } |
| | 62 | |
|
| 0 | 63 | | public long Left { get; set; } |
| 0 | 64 | | public long Right { get; set; } |
| | 65 | | } |
| | 66 | |
|
| | 67 | | private static long Partition(Func<long, long, int> compare, Action<long, long> swap, long left, long right) |
| 0 | 68 | | { |
| | 69 | | // get the pivot value. |
| 0 | 70 | | if (left > right) |
| 0 | 71 | | { |
| 0 | 72 | | throw new ArgumentException("left should be smaller than or equal to right."); |
| | 73 | | } |
| | 74 | |
|
| 0 | 75 | | if (left == right) |
| 0 | 76 | | { |
| | 77 | | // sorting just one item results in that item being sorted already and a pivot equal to that item itself. |
| 0 | 78 | | return right; |
| | 79 | | } |
| | 80 | |
|
| | 81 | | // select the middle one as the pivot value. |
| 0 | 82 | | var pivot = (left + right) / (long)2; |
| 0 | 83 | | if (pivot != left) |
| 0 | 84 | | { // switch. |
| 0 | 85 | | swap(pivot, left); |
| 0 | 86 | | } |
| | 87 | |
|
| | 88 | | // start with the left as pivot value. |
| 0 | 89 | | pivot = left; |
| | 90 | | //var pivotValue = value(pivot); |
| | 91 | |
|
| 0 | 92 | | while (true) |
| 0 | 93 | | { |
| | 94 | | // move the left to the right until the first value bigger than pivot. |
| | 95 | | // var leftValue = value(left + 1); |
| 0 | 96 | | var leftComparison = compare(left + 1, pivot); |
| 0 | 97 | | while (leftComparison <= 0) |
| 0 | 98 | | { |
| 0 | 99 | | left++; |
| 0 | 100 | | if (left == right) |
| 0 | 101 | | { |
| 0 | 102 | | break; |
| | 103 | | } |
| | 104 | |
|
| 0 | 105 | | leftComparison = compare(left + 1, pivot); |
| 0 | 106 | | } |
| | 107 | |
|
| | 108 | | // move the right to left until the first value smaller than pivot. |
| 0 | 109 | | if (left != right) |
| 0 | 110 | | { |
| | 111 | | // var rightValue = value(right); |
| 0 | 112 | | var rightComparison = compare(right, pivot); |
| 0 | 113 | | while (rightComparison > 0) |
| 0 | 114 | | { |
| 0 | 115 | | right--; |
| 0 | 116 | | if (left == right) |
| 0 | 117 | | { |
| 0 | 118 | | break; |
| | 119 | | } |
| | 120 | |
|
| 0 | 121 | | rightComparison = compare(right, pivot); |
| 0 | 122 | | } |
| 0 | 123 | | } |
| | 124 | |
|
| 0 | 125 | | if (left == right) |
| 0 | 126 | | { // we are done searching, left == right. |
| 0 | 127 | | if (pivot != left) |
| 0 | 128 | | { // make sure the pivot value is where it is supposed to be. |
| 0 | 129 | | swap(pivot, left); |
| 0 | 130 | | } |
| | 131 | |
|
| 0 | 132 | | return left; |
| | 133 | | } |
| | 134 | |
|
| | 135 | | // switch left<->right. |
| 0 | 136 | | swap(left + 1, right); |
| 0 | 137 | | } |
| 0 | 138 | | } |
| | 139 | |
|
| | 140 | | public static void ThreewayPartition(Func<long, long> value, Action<long, long> swap, long left, long right, |
| | 141 | | out long highestLowest, out long lowestHighest) |
| 0 | 142 | | { |
| 0 | 143 | | ThreewayPartition(value, swap, left, right, left, out highestLowest, |
| 0 | 144 | | out lowestHighest); // default, the left a pivot. |
| 0 | 145 | | } |
| | 146 | |
|
| | 147 | | public static void ThreewayPartition(Func<long, long> value, Action<long, long> swap, long left, long right, |
| | 148 | | long pivot, |
| | 149 | | out long highestLowest, out long lowestHighest) |
| 0 | 150 | | { |
| 0 | 151 | | if (left > right) |
| 0 | 152 | | { |
| 0 | 153 | | throw new ArgumentException("left should be smaller than or equal to right."); |
| | 154 | | } |
| | 155 | |
|
| 0 | 156 | | if (left == right) |
| 0 | 157 | | { |
| | 158 | | // sorting just one item results in that item being sorted already and a pivot equal to that item itself. |
| 0 | 159 | | highestLowest = right; |
| 0 | 160 | | lowestHighest = right; |
| 0 | 161 | | return; |
| | 162 | | } |
| | 163 | |
|
| | 164 | | // get pivot value. |
| 0 | 165 | | var pivotValue = value(pivot); |
| | 166 | |
|
| 0 | 167 | | var i = left; |
| 0 | 168 | | var j = left; |
| 0 | 169 | | var n = right; |
| | 170 | |
|
| 0 | 171 | | while (j <= n) |
| 0 | 172 | | { |
| 0 | 173 | | var valueJ = value(j); |
| 0 | 174 | | if (valueJ < pivotValue) |
| 0 | 175 | | { |
| 0 | 176 | | swap(i, j); |
| 0 | 177 | | i++; |
| 0 | 178 | | j++; |
| 0 | 179 | | } |
| 0 | 180 | | else if (valueJ > pivotValue) |
| 0 | 181 | | { |
| 0 | 182 | | swap(j, n); |
| 0 | 183 | | n--; |
| 0 | 184 | | } |
| | 185 | | else |
| 0 | 186 | | { |
| 0 | 187 | | j++; |
| 0 | 188 | | } |
| 0 | 189 | | } |
| | 190 | |
|
| 0 | 191 | | highestLowest = i - 1; |
| 0 | 192 | | lowestHighest = n + 1; |
| 0 | 193 | | } |
| | 194 | | } |