Skip to content

Commit 77a2411

Browse files
chore: merge add QuickSortRecursive method. (TheAlgorithms#690)
* add QuickSortRecursive method * added an exception for test cases * fix for test cases * added test cases for QuickSortRecursive
1 parent c1030ec commit 77a2411

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

Sorts/QuickSortRecursive.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
18+
/**
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.
23+
*/
24+
const quickSort = (inputList, low, high) => {
25+
if (!Array.isArray(inputList)) {
26+
throw new TypeError('Please input a valid list or array.')
27+
}
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)
34+
}
35+
return inputList
36+
}
37+
38+
/**
39+
* Partition In Place method.
40+
* @param {number[]} partitionList list for partiting.
41+
* @param {number} low lower index for partition.
42+
* @param {number} high higher index for partition.
43+
* @returns {number} `pIndex` pivot index value.
44+
*/
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]] = [partitionList[pIndex], partitionList[index]]
52+
pIndex += 1
53+
}
54+
}
55+
[partitionList[pIndex], partitionList[high]] = [partitionList[high], partitionList[pIndex]]
56+
return pIndex
57+
}
58+
59+
export { quickSort }

Sorts/QuickSortRecursive.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { quickSort } from './QuickSortRecursive'
2+
3+
describe('QuickSortRecursive | Partition In Place Method', () => {
4+
it('Expectedly, throw some error if we pass a non-array input', () => {
5+
expect(() => quickSort('xyz', 0, 2)).toThrow('Please input a valid list or array.')
6+
expect(() => quickSort(null, 0, 4)).toThrow('Please input a valid list or array.')
7+
expect(() => quickSort(55, 0, 2)).toThrow('Please input a valid list or array.')
8+
})
9+
10+
it('Expectedly, the quickSort method will sort the unsorted list in ascending order', () => {
11+
const unSortArray = [5, 9, 3, 4, 6, 2, 0, 1, 7, 8]
12+
const sortedExpectedArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
13+
expect(quickSort(unSortArray, 0, unSortArray.length - 1)).toEqual(sortedExpectedArray)
14+
})
15+
16+
it('Expectedly, the quickSort method will arrange the list of character values in dictionary order.', () => {
17+
const unSortList = ['d', 'e', 'c', 'a', 'f', 'b']
18+
const sortedExpectedList = ['a', 'b', 'c', 'd', 'e', 'f']
19+
expect(quickSort(unSortList, 0, unSortList.length - 1)).toEqual(sortedExpectedList)
20+
})
21+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy