@@ -6,73 +6,55 @@ export default class QuickSortInPlace extends Sort {
6
6
* This process is difficult to describe, but much clearer with a visualization:
7
7
* @see : http://www.algomation.com/algorithm/quick-sort-visualization
8
8
*
9
- * @param {*[] } originalArray - Not sorted array.
9
+ * @param {*[] } array - Not sorted array.
10
10
* @param {number } inputLowIndex
11
11
* @param {number } inputHighIndex
12
- * @param {boolean } recursiveCall
13
- * @return {*[] } - Sorted array.
12
+ * @return {*[] } array - Sorted array.
14
13
*/
15
- sort (
16
- originalArray ,
17
- inputLowIndex = 0 ,
18
- inputHighIndex = originalArray . length - 1 ,
19
- recursiveCall = false ,
20
- ) {
21
- // Copies array on initial call, and then sorts in place.
22
- const array = recursiveCall ? originalArray : [ ...originalArray ] ;
23
-
24
- /**
25
- * The partitionArray() operates on the subarray between lowIndex and highIndex, inclusive.
26
- * It arbitrarily chooses the last element in the subarray as the pivot.
27
- * Then, it partially sorts the subarray into elements than are less than the pivot,
28
- * and elements that are greater than or equal to the pivot.
29
- * Each time partitionArray() is executed, the pivot element is in its final sorted position.
30
- *
31
- * @param {number } lowIndex
32
- * @param {number } highIndex
33
- * @return {number }
34
- */
35
- const partitionArray = ( lowIndex , highIndex ) => {
36
- /**
37
- * Swaps two elements in array.
38
- * @param {number } leftIndex
39
- * @param {number } rightIndex
40
- */
41
- const swap = ( leftIndex , rightIndex ) => {
42
- const temp = array [ leftIndex ] ;
43
- array [ leftIndex ] = array [ rightIndex ] ;
44
- array [ rightIndex ] = temp ;
45
- } ;
46
-
47
- const pivot = array [ highIndex ] ;
48
- // visitingCallback is used for time-complexity analysis.
49
- this . callbacks . visitingCallback ( array [ pivot ] ) ;
50
-
51
- let partitionIndex = lowIndex ;
52
- for ( let currentIndex = lowIndex ; currentIndex < highIndex ; currentIndex += 1 ) {
53
- if ( this . comparator . lessThan ( array [ currentIndex ] , pivot ) ) {
54
- swap ( partitionIndex , currentIndex ) ;
55
- partitionIndex += 1 ;
56
- }
57
- }
58
-
59
- // The element at the partitionIndex is guaranteed to be greater than or equal to pivot.
60
- // All elements to the left of partitionIndex are guaranteed to be less than pivot.
61
- // Swapping the pivot with the partitionIndex therefore places the pivot in its
62
- // final sorted position.
63
- swap ( partitionIndex , highIndex ) ;
64
-
65
- return partitionIndex ;
66
- } ;
67
-
14
+ sort ( array , inputLowIndex = 0 , inputHighIndex = array . length - 1 ) {
68
15
// Base case is when low and high converge.
69
16
if ( inputLowIndex < inputHighIndex ) {
70
- const partitionIndex = partitionArray ( inputLowIndex , inputHighIndex ) ;
71
- const RECURSIVE_CALL = true ;
72
- this . sort ( array , inputLowIndex , partitionIndex - 1 , RECURSIVE_CALL ) ;
73
- this . sort ( array , partitionIndex + 1 , inputHighIndex , RECURSIVE_CALL ) ;
17
+ const partitionIndex = this . partitionArray ( array , inputLowIndex , inputHighIndex ) ;
18
+ this . sort ( array , inputLowIndex , partitionIndex - 1 ) ;
19
+ this . sort ( array , partitionIndex + 1 , inputHighIndex ) ;
74
20
}
75
21
76
22
return array ;
77
23
}
24
+
25
+ /**
26
+ * The partitionArray() operates on the subarray between lowIndex and highIndex, inclusive.
27
+ * It arbitrarily chooses the last element in the subarray as the pivot.
28
+ * Then, it partially sorts the subarray into elements than are less than the pivot,
29
+ * and elements that are greater than or equal to the pivot.
30
+ * Each time partitionArray() is executed, the pivot element is in its final sorted position.
31
+ *
32
+ * @param {*[] } array - Array for partitioning
33
+ * @param {number } lowIndex
34
+ * @param {number } highIndex
35
+ * @return {number } - Partition index
36
+ */
37
+ partitionArray ( array , lowIndex , highIndex ) {
38
+ const pivot = array [ highIndex ] ;
39
+
40
+ // visitingCallback is used for time-complexity analysis.
41
+ this . callbacks . visitingCallback ( pivot ) ;
42
+
43
+ let partitionIndex = lowIndex ;
44
+ for ( let currentIndex = lowIndex ; currentIndex < highIndex ; currentIndex += 1 ) {
45
+ if ( this . comparator . lessThan ( array [ currentIndex ] , pivot ) ) {
46
+ /* eslint no-param-reassign: ["error", { "props": false }] */
47
+ [ array [ partitionIndex ] , array [ currentIndex ] ] = [ array [ currentIndex ] , array [ partitionIndex ] ] ;
48
+ partitionIndex += 1 ;
49
+ }
50
+ }
51
+
52
+ // The element at the partitionIndex is guaranteed to be greater than or equal to pivot.
53
+ // All elements to the left of partitionIndex are guaranteed to be less than pivot.
54
+ // Swapping the pivot with the partitionIndex therefore places the pivot in its
55
+ // final sorted position.
56
+ [ array [ partitionIndex ] , array [ highIndex ] ] = [ array [ highIndex ] , array [ partitionIndex ] ] ;
57
+
58
+ return partitionIndex ;
59
+ }
78
60
}
0 commit comments