forked from zhangguixu/sourcecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort.js
More file actions
52 lines (37 loc) · 808 Bytes
/
Copy pathquickSort.js
File metadata and controls
52 lines (37 loc) · 808 Bytes
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
/*
快排
*/
(function (exports){
//每次确定一个位置
function partition(a,low,high){
var pivot = a[low],
tmp;
while(low < high){
while(low < high && a[high] > pivot)high--;
tmp = a[low];
a[low] = a[high];
a[high] = tmp;
while(low < high && a[low] <= pivot)low++;
tmp = a[low];
a[low] = a[high];
a[high] = tmp;
}
return low;
}
function quickSort(a,low,high){
if(low < high){
var pivotKey = partition(a,low,high); //排好序
quickSort(a,low,pivotKey-1);
quickSort(a,pivotKey+1,high);
}
return a;
}
function sort(array){
if(Object.prototype.toString.call(array) === '[object Array]'){
return quickSort(array,0,array.length-1);
} else {
throw new Error('illegal parameter');
}
}
exports.quickSort = sort;
})(window);