xonecas.io
2620
Ja

js-algos / commit 32606b6

Merge pull request #2 from michaelheyman/random-quicksort

added random quicksort pivot (Thank you Michael Heyman)

Sean Caetano Martin2016-08-04 32606b676af55d75aa5dda827d0714644002d011 parent de69529parent f47d56a

3 files changed

algos.js+33−0
@@ -74,6 +74,39 @@
7474 }
7575 return list;
7676 };
77+ var randomizedPartition, randomizedQuickSort;
78+ randomizedPartition = function(list, left, right) {
79+ var pivot = list[Math.floor(Math.random() * (right - left) + left)];
80+ while (left <= right) {
81+ while (list[left] < pivot) {
82+ left += 1;
83+ }
84+ while (list[right] > pivot) {
85+ right -= 1;
86+ }
87+ if (left <= right) {
88+ swap(list, left, right);
89+ left += 1;
90+ right -= 1;
91+ }
92+ }
93+ return left;
94+ };
95+ randomizedQuickSort = algos.randomizedQuickSort = function(list, left, right) {
96+ var idx;
97+ if (list.length >= 2) {
98+ left = left === undefined ? 0 : left;
99+ right = right === undefined ? list.length - 1 : right;
100+ idx = randomizedPartition(list, left, right);
101+ if (left < idx - 1) {
102+ randomizedQuickSort(list, left, idx - 1);
103+ }
104+ if (idx < right) {
105+ randomizedQuickSort(list, idx, right);
106+ }
107+ }
108+ return list;
109+ };
77110 var bubbleSort;
78111 bubbleSort = algos.bubbleSort = function(list) {
79112 var len = list.length, i, j;
js/algos.js+57−1
@@ -116,7 +116,9 @@ mergeSort = algos.mergeSort = function (list) {
116116
117117 var swap,
118118 partition,
119- quickSort;
119+ quickSort,
120+ randomizedPartition,
121+ randomizedQuickSort;
120122
121123
122124 // easy peasy swap function.
@@ -198,6 +200,60 @@ quickSort = algos.quickSort = function (list, left, right) {
198200 return list;
199201 };
200202
203+// This version of partition and quick sort calculates the pivot at random,
204+// since the probability of the randomizer finding an item in the list that divides
205+// the list in a 25-75 split is superior.
206+// The expected time requirement becomes O(n log n), and the worst-case O(n^2) is very
207+// unlikely to happen
208+
209+// var randomizedPartition,
210+ // randomizedQuickSort;
211+
212+randomizedPartition = function (list, left, right) {
213+ var pivot = list[Math.floor(Math.random() * (right - left) + left)];
214+
215+ while (left <= right) {
216+
217+ while (list[left] < pivot) {
218+ left += 1;
219+ }
220+
221+ while (list[right] > pivot) {
222+ right -= 1;
223+ }
224+
225+ if (left <= right) {
226+ swap(list, left, right);
227+ left += 1;
228+ right -= 1;
229+ }
230+
231+ }
232+
233+ return left;
234+};
235+
236+randomizedQuickSort = algos.randomizedQuickSort = function (list, left, right) {
237+ var idx;
238+
239+ if (list.length >= 2) {
240+
241+ left = left === undefined ? 0 : left;
242+ right = right === undefined ? list.length - 1 : right;
243+
244+ idx = randomizedPartition(list, left, right);
245+
246+ if (left < idx - 1) {
247+ randomizedQuickSort(list, left, idx - 1);
248+ }
249+
250+ if (idx < right) {
251+ randomizedQuickSort(list, idx, right);
252+ }
253+ }
254+
255+ return list;
256+};
201257
202258
203259 // Bubble sort, aka blubbly
test/sorts.js+4−0
@@ -27,3 +27,7 @@ var bubbleSort = algos.bubbleSort;
2727 console.log("JS bubble sort");
2828 console.log(bubbleSort(list));
2929
30+list = algos.shuffle(list);
31+var randomizedQuickSort = algos.randomizedQuickSort;
32+console.log("JS randomized quick sort");
33+console.log(randomizedQuickSort(list));