-
Notifications
You must be signed in to change notification settings - Fork 0
/
quicksort
77 lines (64 loc) · 2.01 KB
/
quicksort
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
//QuickSort O(n logn)/worse case O(n^2)
//1.Functional programming way.
var a = [34, 203, 3, 746, 200, 984, 198, 764, 9];
Array.prototype.quick_sort = function ()
{
if (this.length <= 1)
return this;
var pivot = this[Math.round(this.length / 2)];
return this.filter(function (x) { return x < pivot }).quick_sort().concat(
this.filter(function (x) { return x == pivot })).concat(
this.filter(function (x) { return x > pivot }).quick_sort());
}
a.quick_sort();
//2.Normal
var a = [34, 203, 3, 746, 200, 984, 198, 764, 9];
function getPivot (arr) {
var iValFirst = arr[0],
iLast = arr.length - 1,
iValLast = arr[iLast],
iMid = Math.floor(arr.length / 2),
iValMid = arr[iMid];
// first position is lowest
if (iValFirst < iValLast && iValFirst < iValMid) {
return iValMid < iValLast ? iMid : iLast;
}
// mid position is lowest
else if (iMid < iValLast && iMid < iValMid) {
return iValFirst < iValLast ? 0 : iLast;
}
// last position is lowest
else {
return iValFirst < iValMid ? 0 : iMid;
}
}
function quicksort (arr) {
var aLess = [],
aGreater = [],
i,
j,
iPivot,
iPivotVal;
// array of length zero or one is already sorted
if (arr.length <= 1) {
return arr;
}
iPivot = getPivot(arr);
iPivotVal = arr[iPivot];
// the function to process the value and compare it to
// the pivot value and put into the correct array
function compVal(iVal) {
(iVal <= iPivotVal ? aLess : aGreater).push(iVal);
}
// compare values before the pivot
for (i = 0, j = iPivot; i < j; i++) {
compVal(arr[i]);
}
// compare values after the pivot
for (i = iPivot + 1, j = arr.length; i < j; i++) {
compVal(arr[i]);
}
// create a new list from sorted sublists around the pivot value
return quicksort(aLess).concat([iPivotVal], quicksort(aGreater));
}
quicksort(a);