-
-
Save abenteuerzeit/9296adc95a6864bc5f04a061cc318661 to your computer and use it in GitHub Desktop.
Quicksort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const swap = require('./swap'); | |
const quicksort = (array, leftBound = 0, rightBound = array.length - 1) => { | |
if (leftBound < rightBound) { | |
console.log('. Calling partition', array, `with leftBound ${leftBound} and rightBound ${rightBound}`); | |
const pivotIndex = partition(array, leftBound, rightBound); | |
console.log(`. Returning pivotIndex = ${pivotIndex}`); | |
console.log(`\nCalling quicksort for left partition with leftBound ${leftBound} and (pivotIndex-1) ${pivotIndex - 1}`); | |
quicksort(array, leftBound, pivotIndex - 1); | |
console.log(`\nCalling quicksort for right partition with pivotIndex ${pivotIndex} and rightBound ${rightBound}`); | |
quicksort(array, pivotIndex, rightBound); | |
} | |
return array; | |
} | |
const partition = (array, leftIndex, rightIndex) => { | |
const pivot = array[Math.floor((rightIndex + leftIndex) / 2)]; | |
console.log(`.. Partitioning with pivot ${pivot} leftIndex ${leftIndex} rightIndex ${rightIndex}`); | |
while (leftIndex <= rightIndex) { | |
while (array[leftIndex] < pivot) { | |
leftIndex++; | |
console.log(`.. ${array[leftIndex-1]} < ${pivot} : Incremented leftIndex => ${leftIndex}`); | |
} | |
while (array[rightIndex] > pivot) { | |
rightIndex--; | |
console.log(`.. ${array[rightIndex+1]} > ${pivot} : Decremented rightIndex => ${rightIndex}`); | |
} | |
if (leftIndex <= rightIndex) { | |
const string = `${leftIndex} <= ${rightIndex}`; | |
swap(array, leftIndex, rightIndex); | |
console.log(`.. ${string} : Swapped leftIndex ${leftIndex} with rightIndex ${rightIndex}`, array); | |
leftIndex++; | |
rightIndex--; | |
console.log(`......... : Incremented leftIndex => ${leftIndex} Decremented rightIndex => ${rightIndex}`); | |
} | |
} | |
return leftIndex; | |
} | |
module.exports = { | |
quicksort, | |
partition | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { quicksort, partition } = require('./quicksort'); | |
const randomize = () => Math.floor(Math.random() * 40); | |
let numbers = []; | |
for (let i = 0; i < 5; i++) { | |
numbers.push(randomize()); | |
} | |
console.log('Before quicksort:', numbers); | |
const sorted = quicksort(numbers); | |
console.log('After quicksort:', sorted); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const swap = (arr, indexOne, indexTwo) => { | |
const temp = arr[indexTwo]; | |
arr[indexTwo] = arr[indexOne]; | |
arr[indexOne] = temp; | |
} | |
module.exports = swap; |
Author
abenteuerzeit
commented
Aug 17, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment