-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathquicksort.js
39 lines (32 loc) · 868 Bytes
/
quicksort.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
console.log('quicksort:')
/* input start */
var input = require('../../../generator/index').getRandomNumbers();
/* input end */
console.log('> input: ' + input);
// quicksort
function quicksort(input) {
sort(0, input.length - 1);
return input;
function sort(start, end) {
if(start >= end) {
return;
}
var mid = partition(start, end);
sort(start, mid - 1);
sort(mid + 1, end);
}
function partition(start, end) {
var i = start, j = end + 1, k = input[start];
while(true) {
while(input[++i] < k) if( i === end) break;
while(input[--j] > k) if( j === start) break;
if(i >= j) break;
input[i] = [input[j], input[j] = input[i]][0];
}
input[j] = [input[start], input[start] = input[j]][0];
return j;
}
}
/* output start */
console.log('> output: ' + quicksort(input));
/* output end */