-
Notifications
You must be signed in to change notification settings - Fork 501
/
quicksort-counters.js
127 lines (109 loc) · 3.44 KB
/
quicksort-counters.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// sample of arrays to sort
var arrayRandom = [9, 2, 5, 6, 4, 3, 7, 10, 1, 8];
var arrayOrdered = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var arrayReversed = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
var countOuter = 0;
var countInner = 0;
var countSwap = 0;
function resetCounters() {
countOuter = 0;
countInner = 0;
countSwap = 0;
}
// basic implementation (pivot is the first element of the array)
function quicksortBasic(array) {
countOuter++;
if(array.length < 2) {
return array;
}
var pivot = array[0];
var lesser = [];
var greater = [];
for(var i = 1; i < array.length; i++) {
countInner++;
if(array[i] < pivot) {
lesser.push(array[i]);
} else {
greater.push(array[i]);
}
}
return quicksortBasic(lesser).concat(pivot, quicksortBasic(greater));
}
quicksortBasic(arrayRandom.slice()); // => outer: 13 inner: 25 swap: 0
console.log('outer:', countOuter, 'inner:', countInner, 'swap:', countSwap);
resetCounters();
quicksortBasic(arrayOrdered.slice()); // => outer: 19 inner: 45 swap: 0
console.log('outer:', countOuter, 'inner:', countInner, 'swap:', countSwap);
resetCounters();
quicksortBasic(arrayReversed.slice()); // => outer: 19 inner: 45 swap: 0
console.log('outer:', countOuter, 'inner:', countInner, 'swap:', countSwap);
resetCounters();
// swap function helper
function swap(array, i, j) {
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
// classic implementation (with Hoare or Lomuto partition scheme, you can comment either one method or the other to see the difference)
function quicksort(array, left, right) {
countOuter++;
left = left || 0;
right = right || array.length - 1;
// var pivot = partitionLomuto(array, left, right); // you can play with both partition
var pivot = partitionHoare(array, left, right); // you can play with both partition
if(left < pivot - 1) {
quicksort(array, left, pivot - 1);
}
if(right > pivot) {
quicksort(array, pivot, right);
}
return array;
}
// Lomuto partition scheme, it is less efficient than the Hoare partition scheme
function partitionLomuto(array, left, right) {
var pivot = right;
var i = left;
for(var j = left; j < right; j++) {
countInner++;
if(array[j] <= array[pivot]) {
countSwap++;
swap(array, i, j);
i = i + 1;
}
}
countSwap++;
swap(array, i, j);
return i;
}
// Hoare partition scheme, it is more efficient than the Lomuto partition scheme because it does three times fewer swaps on average
function partitionHoare(array, left, right) {
var pivot = Math.floor((left + right) / 2 );
while(left <= right) {
countInner++;
while(array[left] < array[pivot]) {
left++;
}
while(array[right] > array[pivot]) {
right--;
}
if(left <= right) {
countSwap++;
swap(array, left, right);
left++;
right--;
}
}
return left;
}
quicksort(arrayRandom.slice());
// => Hoare: outer: 9 inner: 12 swap: 12 - Lomuto: outer: 10 inner: 35 swap: 28
console.log('outer:', countOuter, 'inner:', countInner, 'swap:', countSwap);
resetCounters();
quicksort(arrayOrdered.slice());
// => Hoare: outer: 9 inner: 9 swap: 9 - Lomuto: outer: 9 inner: 45 swap: 54
console.log('outer:', countOuter, 'inner:', countInner, 'swap:', countSwap);
resetCounters();
quicksort(arrayReversed.slice());
// => Hoare: outer: 9 inner: 13 swap: 13 - Lomuto: outer: 10 inner: 54 swap: 39
console.log('outer:', countOuter, 'inner:', countInner, 'swap:', countSwap);
resetCounters();