Created
July 22, 2020 21:28
-
-
Save greathmaster/e2af585d29deef18d9b7259f0eabf370 to your computer and use it in GitHub Desktop.
Variations on a theme by 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
//Recursive Quicksort using Lomuto's partitioning method | |
function lomutosPartition(array, start, end) { | |
let i = start; | |
let pivot = array[end] | |
for(let j = start; j < end; j++) { | |
if(pivot > array[j]) { | |
[array[j], array[i]] = [array[i], array[j]]; | |
i++; | |
} | |
} | |
[array[i], array[end]] = [array[end], array[i]] | |
return i; | |
} | |
function quickLomuto(array, start, end) { | |
if(end - start <= 1) { | |
return; | |
} | |
let pivotIndex = lomutosPartition(array, start, end) | |
quickLomuto(array, pivotIndex + 1, end) | |
quickLomuto(array, start, pivotIndex - 1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment