|
| 1 | +import Sort from '../Sort'; |
| 2 | +import Stack from '../../../data-structures/stack/Stack'; |
| 3 | + |
| 4 | +export default class QuickSortIterative extends Sort { |
| 5 | + /** |
| 6 | + * Iterative Quick Sort |
| 7 | + * |
| 8 | + * @param {*[]} originalArray - Not sorted array. |
| 9 | + * @param {number} inputLowIndex |
| 10 | + * @param {number} inputHighIndex |
| 11 | + * @return {*[]} - Sorted array. |
| 12 | + */ |
| 13 | + sort( |
| 14 | + originalArray, |
| 15 | + inputLowIndex = 0, |
| 16 | + inputHighIndex = originalArray.length - 1, |
| 17 | + sortInPlace = true, |
| 18 | + ) { |
| 19 | + // Copies array on in case we don't want to sort in place |
| 20 | + const array = sortInPlace ? originalArray : [...originalArray]; |
| 21 | + |
| 22 | + // If array has less than or equal to one elements then it is already sorted. |
| 23 | + if (array.length <= 1) { |
| 24 | + return array; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * The partitionArray() operates on the subarray between lowIndex and highIndex, inclusive. |
| 29 | + * It arbitrarily chooses the last element in the subarray as the pivot. |
| 30 | + * Then, it partially sorts the subarray into elements than are less than the pivot, |
| 31 | + * and elements that are greater than or equal to the pivot. |
| 32 | + * Each time partitionArray() is executed, the pivot element is in its final sorted position. |
| 33 | + * |
| 34 | + * @param {number} lowIndex |
| 35 | + * @param {number} highIndex |
| 36 | + * @return {number} |
| 37 | + */ |
| 38 | + const partitionArray = (lowIndex, highIndex) => { |
| 39 | + /** |
| 40 | + * Swaps two elements in array. |
| 41 | + * @param {number} leftIndex |
| 42 | + * @param {number} rightIndex |
| 43 | + */ |
| 44 | + const swap = (leftIndex, rightIndex) => { |
| 45 | + const temp = array[leftIndex]; |
| 46 | + array[leftIndex] = array[rightIndex]; |
| 47 | + array[rightIndex] = temp; |
| 48 | + }; |
| 49 | + |
| 50 | + const pivot = array[highIndex]; |
| 51 | + // visitingCallback is used for time-complexity analysis. |
| 52 | + this.callbacks.visitingCallback(array[pivot]); |
| 53 | + |
| 54 | + let partitionIndex = lowIndex; |
| 55 | + for (let currentIndex = lowIndex; currentIndex < highIndex; currentIndex += 1) { |
| 56 | + if (this.comparator.lessThan(array[currentIndex], pivot)) { |
| 57 | + swap(partitionIndex, currentIndex); |
| 58 | + partitionIndex += 1; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // The element at the partitionIndex is guaranteed to be greater than or equal to pivot. |
| 63 | + // All elements to the left of partitionIndex are guaranteed to be less than pivot. |
| 64 | + // Swapping the pivot with the partitionIndex therefore places the pivot in its |
| 65 | + // final sorted position. |
| 66 | + swap(partitionIndex, highIndex); |
| 67 | + |
| 68 | + return partitionIndex; |
| 69 | + }; |
| 70 | + |
| 71 | + /** |
| 72 | + * Replace recursion with auxiliary stack |
| 73 | + */ |
| 74 | + const stack = new Stack(); |
| 75 | + stack.push(inputLowIndex); |
| 76 | + stack.push(inputHighIndex); |
| 77 | + |
| 78 | + while (!stack.isEmpty()) { |
| 79 | + const highIndex = stack.pop(); |
| 80 | + const lowIndex = stack.pop(); |
| 81 | + const partitionIndex = partitionArray(lowIndex, highIndex); |
| 82 | + |
| 83 | + if (partitionIndex - 1 > lowIndex) { |
| 84 | + stack.push(lowIndex); |
| 85 | + stack.push(partitionIndex - 1); |
| 86 | + } |
| 87 | + |
| 88 | + if (partitionIndex + 1 < highIndex) { |
| 89 | + stack.push(partitionIndex + 1); |
| 90 | + stack.push(highIndex); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return array; |
| 95 | + } |
| 96 | +} |
0 commit comments