/
app.js
52 lines (45 loc) · 1.06 KB
/
app.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
const { Console } = require("./console");
const console = new Console();
let measures = [9,2,5,7,1,3,8,0,4,6];
print("desordenados", measures);
sort(measures);
print("ordenados", measures);
function print(title, data) {
console.writeln(`Valores ${title} :`);
for (let i = 0; i < data.length; i++) {
console.writeln(data[i]);
}
}
function sort (arr) { // quick sort
let stack = [];
let start = 0;
let end = arr.length - 1;
stack.push({x: start, y: end});
while(stack.length){
const { x, y } = stack.shift();
const PI = partitionHigh(arr, x, y);
if(PI - 1 > x){
stack.push({x: x, y: PI - 1});
}
if(PI + 1 < y){
stack.push({x: PI + 1, y: y});
}
}
function partitionHigh (arr, low, high) {
let pivot = arr[high];
let i = low;
for(let j = low; j < high; j++){
if(arr[j] <= pivot){
swap(arr, i, j);
i++;
}
}
swap(arr, i, high);
return i;
}
function swap(arr, left, right) {
const temp = arr[left]
arr[left] = arr[right]
arr[right] = temp;
}
}