Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add Quick Sort implementation in JavaScript
  • Loading branch information
MayankSharma-2812 committed Jan 24, 2026
commit da50af39f6e4c814cf91ccb80f9312a4246f38f8
38 changes: 18 additions & 20 deletions Sorts/QuickSort.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
/**
* @function QuickSort
* @description Quick sort is a comparison sorting algorithm that uses a divide and conquer strategy.
* @param {Integer[]} items - Array of integers
* @return {Integer[]} - Sorted array.
* @see [QuickSort](https://en.wikipedia.org/wiki/Quicksort)
* @function quickSort
* @description Quick Sort is a divide-and-conquer comparison-based sorting algorithm.
* @param {number[]} items - Array of integers
* @returns {number[]} - Sorted array
* @timecomplexity Average: O(n log n), Worst: O(n²)
* @spacecomplexity O(n)
* @see https://en.wikipedia.org/wiki/Quicksort
*/
function quickSort(items) {
const length = items.length
if (items.length <= 1) return items

if (length <= 1) {
return items
}
const PIVOT = items[0]
const GREATER = []
const LESSER = []
const pivotIndex = Math.floor(items.length / 2)
const pivot = items[pivotIndex]

const lesser = []
const greater = []

for (let i = 1; i < length; i++) {
if (items[i] > PIVOT) {
GREATER.push(items[i])
} else {
LESSER.push(items[i])
}
for (let i = 0; i < items.length; i++) {
if (i === pivotIndex) continue
if (items[i] < pivot) lesser.push(items[i])
else greater.push(items[i])
}

const sorted = [...quickSort(LESSER), PIVOT, ...quickSort(GREATER)]
return sorted
return [...quickSort(lesser), pivot, ...quickSort(greater)]
}

export { quickSort }