forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.js
44 lines (39 loc) · 996 Bytes
/
Main.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
var buf = "";
process.stdin.on("readable", function () {
var chunk = process.stdin.read();
if (chunk) buf += chunk.toString();
});
let getInputArgs = line => {
return line
.split(" ")
.filter(s => s !== "")
.map(x => parseInt(x));
};
function quickSort(nums, left, right) {
if (left >= right) {
return;
}
let i = left - 1;
let j = right + 1;
let x = nums[(left + right) >> 1];
while (i < j) {
while (nums[++i] < x);
while (nums[--j] > x);
if (i < j) {
const t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
}
quickSort(nums, left, j);
quickSort(nums, j + 1, right);
}
process.stdin.on("end", function () {
buf.split("\n").forEach(function (line, lineIdx) {
if (lineIdx % 2 === 1) {
nums = getInputArgs(line);
quickSort(nums, 0, nums.length - 1);
console.log(nums.join(" "));
}
});
});