Last active
November 5, 2021 11:09
-
-
Save ramsunvtech/404ca8b53e4f8f3f4da8 to your computer and use it in GitHub Desktop.
Quick Sort in 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(arr, left, right){ | |
var len = arr.length, | |
pivot, | |
partitionIndex; | |
if(left < right){ | |
pivot = right; | |
partitionIndex = partition(arr, pivot, left, right); | |
//sort left and right | |
quickSort(arr, left, partitionIndex - 1); | |
quickSort(arr, partitionIndex + 1, right); | |
} | |
return arr; | |
} | |
function partition(arr, pivot, left, right){ | |
var pivotValue = arr[pivot], | |
partitionIndex = left; | |
for(var i = left; i < right; i++){ | |
if(arr[i] < pivotValue){ | |
swap(arr, i, partitionIndex); | |
partitionIndex++; | |
} | |
} | |
swap(arr, right, partitionIndex); | |
return partitionIndex; | |
} | |
function swap(arr, i, j){ | |
var temp = arr[i]; | |
arr[i] = arr[j]; | |
arr[j] = temp; | |
} |
Sort Descending with Duplicates
function quickSort(arr) {
if (arr.length <= 1) {
return arr;
}
const pivot = arr[arr.length-1];
const lesserNumbersThanPivot = [];
const equalToPivot = [];
const greaterNumbersThanPivot = [];
for(let arrayIndex = 0;arrayIndex<arr.length-1;arrayIndex++) {
if (arr[arrayIndex] < pivot) {
lesserNumbersThanPivot.push(arr[arrayIndex]);
} if (arr[arrayIndex] === pivot) {
equalToPivot.push(arr[arrayIndex]);
} else if (arr[arrayIndex] > pivot) {
greaterNumbersThanPivot.push(arr[arrayIndex]);
}
}
if(lesserNumbersThanPivot.length > 0 && greaterNumbersThanPivot.length > 0) {
return [...quickSort(greaterNumbersThanPivot), pivot, ...equalToPivot, ...quickSort(lesserNumbersThanPivot)];
} else if(greaterNumbersThanPivot.length > 0) {
return [...quickSort(greaterNumbersThanPivot), pivot, ...equalToPivot];
} else {
return [pivot, ...equalToPivot, ...quickSort(lesserNumbersThanPivot)];
}
}
Sort Ascending with Duplicates.
function quickSort(arr) {
if (arr.length <= 1) {
return arr;
}
const pivot = arr[arr.length-1];
const lesserNumbersThanPivot = [];
const equalToPivot = [];
const greaterNumbersThanPivot = [];
for(let arrayIndex = 0;arrayIndex<arr.length-1;arrayIndex++) {
if (arr[arrayIndex] < pivot) {
lesserNumbersThanPivot.push(arr[arrayIndex]);
} if (arr[arrayIndex] === pivot) {
equalToPivot.push(arr[arrayIndex]);
} else if (arr[arrayIndex] > pivot) {
greaterNumbersThanPivot.push(arr[arrayIndex]);
}
}
if(lesserNumbersThanPivot.length > 0 && greaterNumbersThanPivot.length > 0) {
return [...quickSort(lesserNumbersThanPivot), pivot, ...equalToPivot, ...quickSort(greaterNumbersThanPivot)];
} else if(lesserNumbersThanPivot.length > 0) {
return [...quickSort(lesserNumbersThanPivot), pivot, ...equalToPivot];
} else {
return [pivot, ...equalToPivot, ...quickSort(greaterNumbersThanPivot)];
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ascending in ES2021 spread operator.