Overview
Data Structures
Searching
Quick Sort is a divide-and-conquer algorithm that selects a pivot element, partitions the array into two sub-arrays (elements less than and greater than the pivot), and recursively sorts them.
Quick Sort is efficient for large datasets with average time complexity of O(n log n), but in the worst case (already sorted array), it becomes O(n²).
// Quick Sort in JavaScript
function quickSort(arr) {
if (arr.length < 2) return arr;
const pivot = arr[0];
const left = arr.slice(1).filter(el => el <= pivot);
const right = arr.slice(1).filter(el => el > pivot);
return [...quickSort(left), pivot, ...quickSort(right)];
}
console.log(quickSort([8, 4, 7, 3, 9, 1])); // Output: [1, 3, 4, 7, 8, 9]Start with an empty array of a chosen length. New slots start as NULL — fill them with Push or Insert below.
Picks a pivot, partitions smaller values to its left and larger to its right, then repeats on each side.
Quick Sort is widely used in real-world systems like database engines, search engines, and programming libraries for its speed and efficiency.
For example, Quick Sort can be used to sort customer transactions in banking apps or search results in e-commerce platforms.
// Sorting transaction amounts const transactions = [1200, 350, 890, 2200, 100]; console.log(quickSort(transactions)); // Output: [100, 350, 890, 1200, 2200]