Introduction
Quick Sort is an important sorting algorithm based on the divide-and-conquer approach. It selects one element as a pivot and rearranges the remaining elements around that pivot. Elements smaller than the pivot are placed on one side, while larger elements are placed on the other. In this chapter, you will practice Quick Sort with JavaScript through 10 solved questions covering pivot selection, partitioning, ascending and descending sorting, duplicates, strings, objects, and reusable functions. Data Structure Quick Sort practice questions with solutions help to build concepts.
Question 1: Sort an Array Using Quick Sort
Question
Sort the following array in ascending order using Quick Sort:
let numbers = [10, 7, 8, 9, 1, 5];
Solution
function quickSort(arr) {
if (arr.length <= 1) {
return arr;
}
let pivot = arr[arr.length - 1];
let left = [];
let right = [];
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] < pivot) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
return [
...quickSort(left),
pivot,
...quickSort(right)
];
}
let numbers = [10, 7, 8, 9, 1, 5];
console.log(quickSort(numbers));
Output
[1, 5, 7, 8, 9, 10]
Here, the last element 5 is initially selected as the pivot.
Question 2: Find the Pivot and Partition an Array
Question
If the last element is selected as the pivot, partition this array around the pivot:
let numbers = [12, 7, 14, 9, 10, 11];
Solution
The last element is:
Pivot = 11
Compare every other element with 11.
Smaller values:
[7, 9, 10]
Larger values:
[12, 14]
So the partition becomes:
[7, 9, 10] 11 [12, 14]
Output
Left side: [7, 9, 10]
Pivot: 11
Right side: [12, 14]
The important idea is that the pivot is placed between values smaller and larger than it.
Question 3: Sort an Array in Descending Order
Question
Use Quick Sort to arrange the following numbers from largest to smallest:
let numbers = [4, 9, 2, 7, 1, 6];
Solution
For descending order, reverse the comparison used while partitioning.
function quickSort(arr) {
if (arr.length <= 1) {
return arr;
}
let pivot = arr[arr.length - 1];
let left = [];
let right = [];
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] > pivot) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
return [
...quickSort(left),
pivot,
...quickSort(right)
];
}
let numbers = [4, 9, 2, 7, 1, 6];
console.log(quickSort(numbers));
Output
[9, 7, 6, 4, 2, 1]
Question 4: Count How Many Times the Pivot Is Selected
Question
Modify Quick Sort so that you can count how many pivot selections occur while sorting:
[20, 5, 15, 10, 25]
Solution
let pivotCount = 0;
function quickSort(arr) {
if (arr.length <= 1) {
return arr;
}
let pivot = arr[arr.length - 1];
pivotCount++;
let left = [];
let right = [];
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] < pivot) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
return [
...quickSort(left),
pivot,
...quickSort(right)
];
}
let numbers = [20, 5, 15, 10, 25];
console.log(quickSort(numbers));
console.log("Pivot selections:", pivotCount);
Output
[5, 10, 15, 20, 25]
Pivot selections: 4
The exact number of pivot selections depends on how the array is partitioned during recursion.
Question 5: Quick Sort with Duplicate Values
Question
Sort this array using Quick Sort while keeping all duplicate values:
let numbers = [5, 3, 8, 3, 9, 5, 1];
Solution
function quickSort(arr) {
if (arr.length <= 1) {
return arr;
}
let pivot = arr[arr.length - 1];
let smaller = [];
let equal = [];
let larger = [];
for (let value of arr) {
if (value < pivot) {
smaller.push(value);
} else if (value === pivot) {
equal.push(value);
} else {
larger.push(value);
}
}
return [
...quickSort(smaller),
...equal,
...quickSort(larger)
];
}
let numbers = [5, 3, 8, 3, 9, 5, 1];
console.log(quickSort(numbers));
Output
[1, 3, 3, 5, 5, 8, 9]
The equal array makes the handling of duplicate values clear.
Question 6: Sort an Array of Strings Using Quick Sort
Question
Sort these names alphabetically using Quick Sort:
let names = ["Neha", "Aman", "Ravi", "Karan", "Priya"];
Solution
function quickSort(arr) {
if (arr.length <= 1) {
return arr;
}
let pivot = arr[arr.length - 1];
let left = [];
let right = [];
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i].localeCompare(pivot) < 0) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
return [
...quickSort(left),
pivot,
...quickSort(right)
];
}
let names = ["Neha", "Aman", "Ravi", "Karan", "Priya"];
console.log(quickSort(names));
Output
["Aman", "Karan", "Neha", "Priya", "Ravi"]
Quick Sort can work with strings when an appropriate comparison is used.
Question 7: Sort Products by Price
Question
Sort the following products from the lowest price to the highest price using Quick Sort:
let products = [
{ name: "Keyboard", price: 1200 },
{ name: "Mouse", price: 700 },
{ name: "Monitor", price: 8500 },
{ name: "Webcam", price: 2500 }
];
Solution
function quickSort(products) {
if (products.length <= 1) {
return products;
}
let pivot = products[products.length - 1];
let cheaper = [];
let expensive = [];
for (let i = 0; i < products.length - 1; i++) {
if (products[i].price < pivot.price) {
cheaper.push(products[i]);
} else {
expensive.push(products[i]);
}
}
return [
...quickSort(cheaper),
pivot,
...quickSort(expensive)
];
}
let products = [
{ name: "Keyboard", price: 1200 },
{ name: "Mouse", price: 700 },
{ name: "Monitor", price: 8500 },
{ name: "Webcam", price: 2500 }
];
console.log(quickSort(products));
Output
[
{ name: "Mouse", price: 700 },
{ name: "Keyboard", price: 1200 },
{ name: "Webcam", price: 2500 },
{ name: "Monitor", price: 8500 }
]
The objects are sorted according to their price property.
Question 8: Sort Students by Marks
Question
Sort these students from highest marks to lowest marks using Quick Sort:
let students = [
{ name: "Aman", marks: 76 },
{ name: "Riya", marks: 92 },
{ name: "Karan", marks: 84 },
{ name: "Neha", marks: 68 }
];
Solution
function quickSort(students) {
if (students.length <= 1) {
return students;
}
let pivot = students[students.length - 1];
let higher = [];
let lower = [];
for (let i = 0; i < students.length - 1; i++) {
if (students[i].marks > pivot.marks) {
higher.push(students[i]);
} else {
lower.push(students[i]);
}
}
return [
...quickSort(higher),
pivot,
...quickSort(lower)
];
}
let students = [
{ name: "Aman", marks: 76 },
{ name: "Riya", marks: 92 },
{ name: "Karan", marks: 84 },
{ name: "Neha", marks: 68 }
];
console.log(quickSort(students));
Output
[
{ name: "Riya", marks: 92 },
{ name: "Karan", marks: 84 },
{ name: "Aman", marks: 76 },
{ name: "Neha", marks: 68 }
]
Question 9: Check Whether Quick Sort Correctly Sorts an Array
Question
Create a Quick Sort function and then check whether the returned array is actually sorted.
Use:
let numbers = [18, 4, 12, 7, 3, 15];
Solution
function quickSort(arr) {
if (arr.length <= 1) {
return arr;
}
let pivot = arr[arr.length - 1];
let left = [];
let right = [];
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] < pivot) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
return [
...quickSort(left),
pivot,
...quickSort(right)
];
}
function isSorted(arr) {
for (let i = 1; i < arr.length; i++) {
if (arr[i - 1] > arr[i]) {
return false;
}
}
return true;
}
let numbers = [18, 4, 12, 7, 3, 15];
let sorted = quickSort(numbers);
console.log(sorted);
console.log("Is sorted:", isSorted(sorted));
Output
[3, 4, 7, 12, 15, 18]
Is sorted: true
The isSorted() function checks every pair of neighboring elements.
Question 10: Create a Reusable Quick Sort Function
Question
Create a reusable Quick Sort function and use it to sort multiple arrays:
[31, 12, 45, 7, 19]
and
[50, 10, 40, 20, 30]
Solution
function quickSort(arr) {
if (arr.length <= 1) {
return arr;
}
let pivot = arr[arr.length - 1];
let left = [];
let right = [];
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] < pivot) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
return [
...quickSort(left),
pivot,
...quickSort(right)
];
}
let numbers1 = [31, 12, 45, 7, 19];
let numbers2 = [50, 10, 40, 20, 30];
console.log(quickSort(numbers1));
console.log(quickSort(numbers2));
Output
[7, 12, 19, 31, 45]
[10, 20, 30, 40, 50]
The same Quick Sort function can be reused for different numeric arrays.
Key Takeaways
- Quick Sort is based on the divide-and-conquer approach.
- It selects an element as a pivot.
- Elements are partitioned around the pivot.
- Values smaller than the pivot can be placed on one side.
- Values larger than the pivot can be placed on the other side.
- Quick Sort can sort numbers, strings, and objects.
- Duplicate values can be handled separately using an
equalgroup. - Quick Sort has an average time complexity of O(n log n).
- Its worst-case time complexity can become O(n²) when partitions are highly unbalanced.
- Pivot selection can significantly affect Quick Sort’s performance.
- Recursive calls continue sorting the partitions.
- Quick Sort can be implemented as a reusable function.
FAQs
1. What is Quick Sort in Data Structures?
Quick Sort is a sorting algorithm that selects a pivot and partitions the remaining elements around that pivot before recursively sorting the partitions.
2. What technique does Quick Sort use?
Quick Sort uses the divide-and-conquer technique.
3. What is the average time complexity of Quick Sort?
The average time complexity of Quick Sort is O(n log n).
4. What is the worst-case time complexity of Quick Sort?
The worst-case time complexity is O(n²). This can happen when the selected pivot repeatedly creates very unbalanced partitions.
5. What is a pivot in Quick Sort?
A pivot is the element selected to divide the array into groups of values that are smaller and larger than it.
6. Can Quick Sort handle duplicate values?
Yes. Quick Sort can handle duplicates. A three-way partitioning approach can separate values into smaller, equal, and larger groups.
7. Can Quick Sort sort objects and strings?
Yes. Quick Sort can sort strings or objects by changing the comparison logic. For objects, a particular property such as price, marks, or age can be used.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
