-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.js
43 lines (36 loc) · 1.04 KB
/
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
function partition(arr, left, right) {
let l = left, r = right, tmp = arr[left];
while (l != r) {
while (l < r && arr[r] >= tmp) --r;
while (l < r && arr[l] <= tmp) ++l;
if (l < r) [arr[r], arr[l]] = [arr[l], arr[r]];
}
[arr[l], arr[left]] = [arr[left], arr[l]];
return l;
}
function quick_sort(arr, left, right) {
if (left > right) return;
const pivot = partition(arr, left, right);
quick_sort(arr, left, pivot - 1);
quick_sort(arr, pivot + 1, right);
}
function generateRandomArray(num, rangeFrom, rangeTo) {
if (rangeTo < rangeFrom) {
return;
}
const arr = new Array(num);
const range = rangeTo - rangeFrom + 1;
for (let i = 0; i < num; ++i) {
arr[i] = Math.floor(Math.random() * range + rangeFrom);
}
return arr;
}
const num = 20;
const rangeFrom = 1;
const rangeTo = 10000;
const arr = generateRandomArray(20, 1, 1000)
console.log("Original array:");
console.log(arr);
quick_sort(arr, 0, arr.length - 1)
console.log("Sorted array:");
console.log(arr);