-
Notifications
You must be signed in to change notification settings - Fork 0
/
quick_sort.js
42 lines (40 loc) · 874 Bytes
/
quick_sort.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
class SortableArray
{
constructor(...arr)
{
this.array = new Array(...arr)
}
disp()
{
console.log(this.array);
}
partition(letftPointer, rightPointer)
{
let pivotIndex = rightPointer;
let pivot = this.array[pivotIndex];
rightPointer--;
while(true)
{
while(this.array[letftPointer] < pivot)
{
letftPointer++;
}
while(this.array[rightPointer] > pivot)
{
rightPointer--;
}
if(letftPointer >= rightPointer)
break;
else
{
[this.array[letftPointer], this.array[rightPointer]] = [this.array[rightPointer], this.array[letftPointer]];
letftPointer++;
}
}
[this.array[leftPointer], this.array[pivotIndex]] = [this.array[pivotIndex], this.array[leftPointer]];
return leftPointer;
}
}
let arr = new SortableArray(4, 5, 2, 3, 1, 6);
arr.partition();
arr.disp();