Skip to content

Commit e573010

Browse files
committed
fix: repair style with prettier
1 parent cc07003 commit e573010

File tree

2 files changed

+39
-37
lines changed

2 files changed

+39
-37
lines changed

Sorts/QuickSortRecursive.js

+35-25
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
Problem & Source of Explanation => https://www.cs.auckland.ac.nz/software/AlgAnim/qsort1a.html
1616
*/
1717

18-
1918
/**
2019
* Sorts the input list using the quicksort algorithm.
2120
*
@@ -27,10 +26,12 @@ const quickSort = (inputList) => {
2726
if (!Array.isArray(inputList)) {
2827
throw new TypeError('Please input a valid list or array.')
2928
}
29+
if (inputList.length <= 1) {
30+
return inputList
31+
}
3032
return quickSortHelper(inputList, 0, inputList.length - 1)
3133
}
3234

33-
3435
/**
3536
* Recursively sorts the input list using the quicksort algorithm.
3637
*
@@ -41,13 +42,10 @@ const quickSort = (inputList) => {
4142
*/
4243

4344
const quickSortHelper = (inputList, low, high) => {
44-
// base case
4545
if (low < high) {
46-
// get the partition index.
47-
const pIndex = partition(inputList, low, high)
48-
// recursively call the quickSort method again.
49-
quickSortHelper(inputList, low, pIndex - 1)
50-
quickSortHelper(inputList, pIndex + 1, high)
46+
const pIndex = partition(inputList, low, high) // Partition the array
47+
quickSortHelper(inputList, low, pIndex - 1) // Sort left subarray
48+
quickSortHelper(inputList, pIndex + 1, high) // Sort right subarray
5149
}
5250
return inputList
5351
}
@@ -60,28 +58,37 @@ const quickSortHelper = (inputList, low, high) => {
6058
* @returns {number} `pIndex` pivot index value.
6159
*/
6260
const partition = (partitionList, low, high) => {
63-
let mid =Math.floor((low + high) / 2)
64-
// get the median of three is good technique for partitioning to be sure that the 2 sub-arrays will be almost equal or near
65-
const pivot = medianOfThree(partitionList, low, mid, high)
61+
const mid = Math.floor((low + high) / 2)
62+
// get the median of three is good technique for partitioning to be sure that the 2 sub-arrays will be almost equal or nearly equal in size
63+
const pivot = medianOfThree(partitionList, low, mid, high) // Find the pivot element
64+
65+
// Move pivot to the end
66+
let pivotIndex = partitionList.indexOf(pivot)
67+
;[partitionList[pivotIndex], partitionList[high]] = [
68+
partitionList[high],
69+
partitionList[pivotIndex]
70+
]
71+
6672
let pIndex = low
67-
for (let index = low; index <= high - 1; index++) {
68-
if (partitionList[index] < pivot) {
69-
// swap variables using array destructuring
70-
;[partitionList[index], partitionList[pIndex]] = [
73+
74+
// Perform the partitioning
75+
for (let i = low; i < high; i++) {
76+
if (partitionList[i] < pivot) {
77+
;[partitionList[i], partitionList[pIndex]] = [
7178
partitionList[pIndex],
72-
partitionList[index]
79+
partitionList[i]
7380
]
74-
pIndex += 1
81+
pIndex++
7582
}
7683
}
84+
// Swap the pivot element back to its correct position
7785
;[partitionList[pIndex], partitionList[high]] = [
7886
partitionList[high],
7987
partitionList[pIndex]
8088
]
81-
return pIndex
82-
}
83-
8489

90+
return pIndex // Return the partition index
91+
}
8592
/**
8693
* Returns the median value of three elements in an array.
8794
*
@@ -92,11 +99,14 @@ const partition = (partitionList, low, high) => {
9299
* @return {number} the median value of the three elements
93100
*/
94101

95-
const medianOfThree = (partitionList, low,mid, high) => {
96-
const a = partitionList[low], b = partitionList[mid], c = partitionList[high];
97-
if ((a > b) !== (a > c)) return a;
98-
else if ((b > a) !== (b > c)) return b;
99-
else return c;
102+
const medianOfThree = (partitionList, low, mid, high) => {
103+
const a = partitionList[low]
104+
const b = partitionList[mid]
105+
const c = partitionList[high]
106+
107+
if (a > b !== a > c) return a
108+
else if (b > a !== b > c) return b
109+
else return c
100110
}
101111

102112
export { quickSort }

Sorts/test/QuickSortRecursive.test.js

+4-12
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,19 @@ describe('QuickSortRecursive | Partition In Place Method', () => {
55
expect(() => quickSort('xyz')).toThrow(
66
'Please input a valid list or array.'
77
)
8-
expect(() => quickSort(null)).toThrow(
9-
'Please input a valid list or array.'
10-
)
11-
expect(() => quickSort(55)).toThrow(
12-
'Please input a valid list or array.'
13-
)
8+
expect(() => quickSort(null)).toThrow('Please input a valid list or array.')
9+
expect(() => quickSort(55)).toThrow('Please input a valid list or array.')
1410
})
1511

1612
it('Expectedly, the quickSort method will sort the unsorted list in ascending order', () => {
1713
const unSortArray = [5, 9, 3, 4, 6, 2, 0, 1, 7, 8]
1814
const sortedExpectedArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
19-
expect(quickSort(unSortArray)).toEqual(
20-
sortedExpectedArray
21-
)
15+
expect(quickSort(unSortArray)).toEqual(sortedExpectedArray)
2216
})
2317

2418
it('Expectedly, the quickSort method will arrange the list of character values in dictionary order.', () => {
2519
const unSortList = ['d', 'e', 'c', 'a', 'f', 'b']
2620
const sortedExpectedList = ['a', 'b', 'c', 'd', 'e', 'f']
27-
expect(quickSort(unSortList)).toEqual(
28-
sortedExpectedList
29-
)
21+
expect(quickSort(unSortList)).toEqual(sortedExpectedList)
3022
})
3123
})

0 commit comments

Comments
 (0)