Last active
March 18, 2021 17:56
-
-
Save Aleksey-Danchin/b889524922949d2aecbaba2bd5cff17a to your computer and use it in GitHub Desktop.
Быстрая сортировка числового ряда на месте (JavaScript)
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
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); | |
} | |
} | |
} |
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
https://repl.it/@Aleksey_Danchin/BitesizedGregariousCharacterencoding |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment