-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort.js
More file actions
52 lines (46 loc) · 1.48 KB
/
quickSort.js
File metadata and controls
52 lines (46 loc) · 1.48 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
async function quickSortHelper(array, startIdx, endIdx) {
if (startIdx >= endIdx) return;
const pivotIdx = startIdx;
let leftIdx = startIdx + 1;
let rightIdx = endIdx;
while (rightIdx >= leftIdx) {
await delay(sortingSpeed)
if (array[leftIdx].style.height > array[pivotIdx].style.height && array[rightIdx].style.height < array[pivotIdx].style.height) {
swap(leftIdx, rightIdx, array)
}
if (array[leftIdx].style.height <= array[pivotIdx].style.height) {
leftIdx++;
}
if (array[rightIdx].style.height >= array[pivotIdx].style.height) {
rightIdx--;
}
}
swap(pivotIdx, rightIdx, array);
const leftSubarrayIsSmaller = rightIdx - 1 - startIdx < endIdx - (rightIdx + 1);
if (leftSubarrayIsSmaller) {
quickSortHelper(array, startIdx, rightIdx - 1);
quickSortHelper(array, rightIdx + 1, endIdx);
}
else {
quickSortHelper(array, rightIdx + 1, endIdx);
quickSortHelper(array, startIdx, rightIdx - 1);
}
}
function swap(i, j, array) {
let temp = array[j].style.height;
array[j].style.height = array[i].style.height;
array[i].style.height = temp;
}
function quickSort(array) {
quickSortHelper(array, 0, array.length - 1);
return array;
}
quickSortButton.addEventListener('click', () => {
var nodes = Array.prototype.slice.call(document.querySelector(".createColumns").children);
async function repeatChain(times, chain) {
for (let i = 0; i < times; i++) {
await chain(nodes);
}
}
repeatChain(1, quickSort)
})