-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.js
More file actions
38 lines (35 loc) · 1005 Bytes
/
sort.js
File metadata and controls
38 lines (35 loc) · 1005 Bytes
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
/* function quicksort(arr, left, right) {
if(right-left <=0){
return;
}
let pivot = arr[right];
let partitionedIndex = partition(arr, left, right, pivot);
quicksort(arr, left, partitionedIndex - 1);
quicksort(arr, partitionedIndex + 1, right);
}
function partition(arr, left, right, pivot) {
let leftIndex = left;
let rightIndex = right - 1;
while (true) {
while(arr[leftIndex] < pivot && leftIndex < right){
leftIndex++;
}
while(rightIndex >= 0 && arr[rightIndex] > pivot){
rightIndex--;
}
if(leftIndex >= rightIndex){
break;
}else{
let temp = arr[rightIndex];
arr[rightIndex] = arr[leftIndex];
arr[leftIndex] = temp;
}
}
let temp = arr[right];
arr[right] = arr[leftIndex];
arr[leftIndex] = temp;
return leftIndex;
}
let nums = [3,8,6,11,9,10,1,2,5];
quicksort(nums, 0, nums.length - 1);
console.log(nums); */