Skip to content

Instantly share code, notes, and snippets.

@Aleksey-Danchin
Last active March 18, 2021 17:56
Show Gist options
  • Save Aleksey-Danchin/b889524922949d2aecbaba2bd5cff17a to your computer and use it in GitHub Desktop.
Save Aleksey-Danchin/b889524922949d2aecbaba2bd5cff17a to your computer and use it in GitHub Desktop.
Быстрая сортировка числового ряда на месте (JavaScript)
function quickSort(array) {
if (array.length < 2) {
return array
}
quickSortMaster(0, array.length - 1)
return array
function quickSortMaster (left, right) {
const pivot = array[Math.floor((right + left) / 2)]
let l = left
let r = right
while (l <= r) {
while (array[l] < pivot) {
l++
}
while (array[r] > pivot) {
r--
}
if (l <= r) {
[array[r], array[l]] = [array[l], array[r]]
l++
r--
}
}
if (left < l - 1) {
quickSortMaster(left, l - 1);
}
if (l < right) {
quickSortMaster(l, right);
}
}
}
https://repl.it/@Aleksey_Danchin/BitesizedGregariousCharacterencoding
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment