Created
February 21, 2019 07:21
-
-
Save alaingoldman/ddf7ff687eec51f6bf8e39b68624ffec to your computer and use it in GitHub Desktop.
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
var a = ['S', 'O', 'R', 'T', 'E', 'X', 'A', 'M', 'P', 'L', 'E']; | |
function qSort(from, to) { | |
if (from >= to) { | |
return; | |
} | |
var pivot = a[to]; | |
var counter = from; | |
for (var i = from; i < to; i++) { | |
if (a[i] < pivot) { | |
qSwap(i, counter); | |
counter++; | |
} | |
} | |
qSwap(counter, to); | |
qSort(from, counter - 1); | |
qSort(counter + 1, to); | |
} | |
function qSwap(x, y) { | |
let temp = a[x]; | |
a[x] = a[y]; | |
a[y] = temp; | |
} | |
qSort(0, a.length - 1); | |
console.log(a); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment