-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quicksort.js
40 lines (37 loc) · 1.13 KB
/
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
//Author: Harry Wang
//Date: May 26 2021
//Description: Same as my python version, but in javascript. Requires Node.js.
const readline = require('readline');
function quick_sort(data) {
//console.log("data");
//console.log(data);
var l, pivot, item
var greater_list = [];
var lower_list = [];
l = data.length;
if(l <= 1) {
return data;
} else {
pivot = data.pop();
}
for(var i = 0; i < l-1; i++) {
item = data[i];
if(item > pivot) {
//console.log(i.toString()+" Greater: "+item.toString());
greater_list.push(item);
} else {
//console.log(i.toString()+" Lower: "+item.toString());
lower_list.push(item);
}
}
var pivot_list = [pivot];
return quick_sort(lower_list).concat(pivot_list).concat(quick_sort(greater_list));
}
const aIn = readline.createInterface({
input: process.stdin,
output: process.stdout
});
aIn.question('Input list with integers, all elements seperated by a space\n', arrayInput => {
console.log(quick_sort(arrayInput.split(" ").map(x => parseInt(x))));
aIn.close();
});