-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort.js
33 lines (30 loc) · 931 Bytes
/
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
class QuickSort {
sort(array, low, high) {
if (low < high) {
let partition_point = this.partition(array, low, high);
this.sort(array, low, partition_point - 1);
this.sort(array, partition_point + 1, high);
}
}
partition(array, low, high) {
let pivot = array[high];
let i = low - 1;
for (let j = low; j < high; j++) {
if (array[j] < pivot) {
i++;
this.swap(array, i, j);
}
}
this.swap(array, i+1, high);
return i + 1;
}
swap(array, index1, index2){
let temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
}
}
const quicksort = new QuickSort();
var testArr = [93, 48, 348, 345, 12, 49, 45, 294, 2334];
quicksort.sort(testArr, 0, testArr.length - 1);
console.log(testArr);