-
Notifications
You must be signed in to change notification settings - Fork 0
/
quicksort.js
executable file
·51 lines (43 loc) · 948 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
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env node
function processData(input) {
//Enter your code here
var lines = input.split(/\r?\n/)
var size = lines[0]
var arr = lines[1].split(' ').map(Number)
console.log(divideAndConqure(arr).join(' '));
}
function divideAndConqure(arr) {
var p = arr[0]
var left = []
var right = []
var equal = []
for (var i = 0; i < arr.length; i += 1) {
if (arr[i] < p) {
left.push(arr[i])
} else if (arr[i] > p) {
right.push(arr[i])
} else {
equal.push(arr[i])
}
}
if (left.length > 1) {
left = divideAndConqure(left)
console.log(left.join(' '));
} else {
}
if (right.length > 1) {
right = divideAndConqure(right)
console.log(right.join(' '));
} else {
}
return left.concat(equal.concat(right))
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});