-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort.js
More file actions
38 lines (36 loc) · 1 KB
/
quickSort.js
File metadata and controls
38 lines (36 loc) · 1 KB
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
//generate an array with 10 random elements, with random values from 0 to 49
var numbers = [];
while(numbers.length < 10){
numbers.push(Math.floor(Math.random() * 50));
}
console.log("Unsorted array: \n ", JSON.stringify(numbers));
console.log("Steps:");
// quick sort
function quickSort(first, last){
let p, l, r;
p = last;
l = first;
r = last - 1;
while(l != r){
while(numbers[l] <= numbers[p] && l!= r){
l++;
}
while(numbers[r] >= numbers[p] && l!= r){
r--;
}
[numbers[l], numbers[r]] = [numbers[r], numbers[l]];
}
if(l != p - 1 || (l == p - 1 && numbers[l] > numbers[p])){
[numbers[l], numbers[p]] = [numbers[p], numbers[l]];
p = r;
}
console.log(" ",JSON.stringify(numbers), p);
if(p - first >= 2){
quickSort(first, p - 1);
}
if(last - p >= 2){
quickSort(p + 1, last);
}
}
quickSort(0, numbers.length - 1);
console.log("Sorted array: \n ", JSON.stringify(numbers));