<script>
function quickSort(arr) {
if (arr.length < 2) {
return arr;
}
let chooseOne = arr[0];
let left = [];
let right = [];
let result = [];
for (let index = 0; index < arr.length; index++) {
const element = arr[index];
if (element < chooseOne) {
left.push(element)
} else if (element > chooseOne) {
right.push(element);
}
}
let leftResult = quickSort(left);
for (let index = 0; index < leftResult.length; index++) {
result.push(leftResult[index]);
}
let equalValueCount = arr.length - (left.length + right.length);
while (equalValueCount--) {
result.push(chooseOne);
}
let rightResult = quickSort(right);
for (let index = 0; index < rightResult.length; index++) {
result.push(rightResult[index]);
}
return result;
}
let myArr = [3, 4, 6, 2, 3, 4, 23, 1, 34, 61, 45, 34, 55, 88, 23, 33, 1, 2, 3, 4, 45, 6];
alert(quickSort(myArr));
</script>
|