-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquicksort.js
More file actions
59 lines (52 loc) · 1.53 KB
/
quicksort.js
File metadata and controls
59 lines (52 loc) · 1.53 KB
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
/*
Implement
1. Quicksort. (done)
2. provide a callback for a compare function (todo).
3. Time to sort. (done)
*/
function RandomArray(size) {
var foo = [];
for(var i = 0; i < size; i++) {
foo.push(Math.ceil(Math.random() * 100));
}
return foo;
}
Array.prototype.quicksort = function() {
var complexity = 0, //Number of times the qsort is called. For worst case, it's called array.length - 1 times.
a = this,
fixpivot = function (lower, upper) {
var pivot = lower,
p = lower + 1,
q = upper;
while (q >= p) { // q should cross p. Only then you can swap.
while(a[pivot] > a[p]) p++;
while(a[pivot] <= a[q]) q--;
if (q>p) {
[a[p], a[q]] = [a[q], a[p]];
p++;
q--;
}
}
[a[pivot], a[p-1]] = [a[p-1], a[pivot]]; // p - 1 = q
return p-1;
},
qsort = function (lower, upper) {
var pivot_index;
if (upper > lower) {
pivot_index = fixpivot(lower, upper);
complexity++;
qsort(lower, pivot_index - 1);
qsort(pivot_index + 1, upper);
}
},
sortedarr = (function () {
var lastindex = arr.length - 1;
qsort(0, lastindex);
return a;
})();
return sortedarr;
};
var arr = RandomArray(10);
console.log(arr);
arr.quicksort();
console.log(arr);