Skip to content

Commit 8a4e8c4

Browse files
committed
>> git commit -m "Fixes: #1703 Improve QuickSort Algorithm with Better Space Complexity"
1 parent 9010481 commit 8a4e8c4

File tree

4 files changed

+81
-77
lines changed

4 files changed

+81
-77
lines changed

Sorts/QuickSort.js

+33-17
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,46 @@
11
/**
2-
* @function QuickSort
2+
* @function quickSort
33
* @description Quick sort is a comparison sorting algorithm that uses a divide and conquer strategy.
4-
* @param {Integer[]} items - Array of integers
4+
* This version optimizes space complexity by sorting the array in place.
5+
* @param {Integer[]} items - Array of integers to be sorted.
6+
* @param {number} left - The starting index (defaults to 0).
7+
* @param {number} right - The ending index (defaults to array length - 1).
58
* @return {Integer[]} - Sorted array.
69
* @see [QuickSort](https://en.wikipedia.org/wiki/Quicksort)
710
*/
8-
function quickSort(items) {
9-
const length = items.length
11+
function quickSort(items, left = 0, right = items.length - 1) {
12+
if (left < right) {
13+
let pivotIndex = partition(items, left, right);
1014

11-
if (length <= 1) {
12-
return items
15+
quickSort(items, left, pivotIndex - 1);
16+
quickSort(items, pivotIndex + 1, right);
1317
}
14-
const PIVOT = items[0]
15-
const GREATER = []
16-
const LESSER = []
18+
return items;
19+
}
20+
21+
/**
22+
* @function partition
23+
* @description This function partitions the array using the last element as the pivot.
24+
* It ensures that all elements smaller than the pivot are on the left side,
25+
* and all greater elements are on the right side.
26+
* @param {Integer[]} items - Array of integers to partition.
27+
* @param {number} left - The starting index for partitioning.
28+
* @param {number} right - The ending index for partitioning.
29+
* @return {number} - The index of the pivot element after partitioning.
30+
*/
31+
function partition(items, left, right) {
32+
const pivot = items[right];
33+
let i = left - 1;
1734

18-
for (let i = 1; i < length; i++) {
19-
if (items[i] > PIVOT) {
20-
GREATER.push(items[i])
21-
} else {
22-
LESSER.push(items[i])
35+
for (let j = left; j < right; j++) {
36+
if (items[j] <= pivot) {
37+
i++;
38+
[items[i], items[j]] = [items[j], items[i]];
2339
}
2440
}
2541

26-
const sorted = [...quickSort(LESSER), PIVOT, ...quickSort(GREATER)]
27-
return sorted
42+
[items[i + 1], items[right]] = [items[right], items[i + 1]];
43+
return i + 1;
2844
}
2945

30-
export { quickSort }
46+
export { quickSort };

Sorts/QuickSortRecursive.js

+39-53
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,51 @@
1-
/*
2-
Quicksort is the most popular sorting algorithm and there have
3-
lots of different implementations but the "recursive" or "Partition in place"
4-
is one of the most efficient implementations below we have discussed how to
5-
implement it.
6-
7-
Partition in place => "in place" Partition in place indicates that we
8-
do not need any other space to store the auxiliary array and the term
9-
"partition" denotes that we split the list into two parts one is less
10-
than the pivot and the other is greater than the pivot and repeats this
11-
process recursively and breaks the problem into sub-problems and makes
12-
it singular so that the behavior or "divide and conquer" get involved
13-
too.
14-
15-
Problem & Source of Explanation => https://www.cs.auckland.ac.nz/software/AlgAnim/qsort1a.html
16-
*/
17-
181
/**
19-
* Partition in place QuickSort.
20-
* @param {number[]} inputList list of values.
21-
* @param {number} low lower index for partition.
22-
* @param {number} high higher index for partition.
2+
* @function quickSort
3+
* @description Quick sort is a comparison sorting algorithm that uses a divide and conquer strategy.
4+
* This version optimizes space complexity by sorting the array in place.
5+
* @param {Integer[]} items - Array of integers to be sorted.
6+
* @param {number} left - The starting index (defaults to 0).
7+
* @param {number} right - The ending index (defaults to array length - 1).
8+
* @return {Integer[]} - Sorted array.
9+
* @throws Will throw an error if the input is not an array.
10+
* @see [QuickSort](https://en.wikipedia.org/wiki/Quicksort)
2311
*/
24-
const quickSort = (inputList, low, high) => {
25-
if (!Array.isArray(inputList)) {
26-
throw new TypeError('Please input a valid list or array.')
12+
function quickSort(items, left = 0, right = items.length - 1) {
13+
if (!Array.isArray(items)) {
14+
throw new Error('Please input a valid list or array.');
2715
}
28-
if (low < high) {
29-
// get the partition index.
30-
const pIndex = partition(inputList, low, high)
31-
// recursively call the quickSort method again.
32-
quickSort(inputList, low, pIndex - 1)
33-
quickSort(inputList, pIndex + 1, high)
16+
17+
if (left < right) {
18+
let pivotIndex = partition(items, left, right);
19+
quickSort(items, left, pivotIndex - 1);
20+
quickSort(items, pivotIndex + 1, right);
3421
}
35-
return inputList
22+
23+
return items;
3624
}
3725

3826
/**
39-
* Partition In Place method.
40-
* @param {number[]} partitionList list for partitioning.
41-
* @param {number} low lower index for partition.
42-
* @param {number} high higher index for partition.
43-
* @returns {number} `pIndex` pivot index value.
27+
* @function partition
28+
* @description This function partitions the array using the last element as the pivot.
29+
* It ensures that all elements smaller than the pivot are on the left side,
30+
* and all greater elements are on the right side.
31+
* @param {Integer[]} items - Array of integers to partition.
32+
* @param {number} left - The starting index for partitioning.
33+
* @param {number} right - The ending index for partitioning.
34+
* @return {number} - The index of the pivot element after partitioning.
4435
*/
45-
const partition = (partitionList, low, high) => {
46-
const pivot = partitionList[high]
47-
let pIndex = low
48-
for (let index = low; index <= high - 1; index++) {
49-
if (partitionList[index] < pivot) {
50-
// swap variables using array destructuring
51-
;[partitionList[index], partitionList[pIndex]] = [
52-
partitionList[pIndex],
53-
partitionList[index]
54-
]
55-
pIndex += 1
36+
function partition(items, left, right) {
37+
const pivot = items[right];
38+
let i = left - 1;
39+
40+
for (let j = left; j < right; j++) {
41+
if (items[j] <= pivot) {
42+
i++;
43+
[items[i], items[j]] = [items[j], items[i]];
5644
}
5745
}
58-
;[partitionList[pIndex], partitionList[high]] = [
59-
partitionList[high],
60-
partitionList[pIndex]
61-
]
62-
return pIndex
46+
47+
[items[i + 1], items[right]] = [items[right], items[i + 1]];
48+
return i + 1;
6349
}
6450

65-
export { quickSort }
51+
export { quickSort };

package-lock.json

+6-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
"author": "TheAlgorithms",
1414
"license": "GPL-3.0",
1515
"devDependencies": {
16+
"@vitest/coverage-v8": "^1.2.1",
1617
"globby": "^13.2.2",
17-
"husky": "^8.0.3",
18+
"husky": "^9.1.6",
1819
"prettier": "^3.0.3",
19-
"vitest": "^1.2.1",
20-
"@vitest/coverage-v8": "^1.2.1"
20+
"vitest": "^1.2.1"
2121
},
2222
"engines": {
2323
"node": ">=20.6.0"

0 commit comments

Comments
 (0)