Sorting algorithms/Quicksort: Difference between revisions
Content deleted Content added
Added an assertion which holds after each call to partition() |
|||
Line 1,124:
//
//[Assert]left
//▼
//
var leftLength = right + 1 - first;
Line 1,146 ⟶ 1,142:
last = right;
length = leftLength;
}▼
}▼
private static void partition(T[] entries, T pivot, ref Int32 left, ref Int32 right) {▼
while (left <= right) {▼
while (pivot.CompareTo(entries[left]) > 0) left++;▼
while (pivot.CompareTo(entries[right]) < 0) right--;▼
if (left < right) // Move entries to their correct partition▼
Swap(entries, left++, right--);▼
left++;▼
right--;▼
}
}
Line 1,164 ⟶ 1,174:
InsertionSort<T>.Sort(entries, first, right);
return entries[first + sampleSize / 2];
▲ }
▲ private static void partition(T[] entries, T pivot, ref Int32 left, ref Int32 right) {
▲ while (left <= right) {
▲ while (pivot.CompareTo(entries[left]) > 0)
▲ while (pivot.CompareTo(entries[right]) < 0)
▲ if (left < right) // Move entries to their correct partition
▲ Swap(entries, left++, right--);
▲ else if (left == right) { // No swap needed
▲ left++;
▲ right--;
▲ }
}
|