-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort.js
More file actions
65 lines (63 loc) · 1.88 KB
/
quickSort.js
File metadata and controls
65 lines (63 loc) · 1.88 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
53
54
55
56
57
58
59
60
61
62
63
64
65
let hg1,hg2,fh1,fh2,fh3,hg3,fh4,hg4,fh5,fh6;
function swap(i,j){
let temp=height[i];
height[i]=height[j];
height[j]=temp;
}
async function partition(low,high){
let pivot=height[high];
fh6=children.item(high);
fh6.style.backgroundColor="blue";
let i=(low-1);
for(let j=low;j<=high-1;j++){
await new Promise(resolve => setTimeout(() => {resolve()},speed));
if(height[j]<pivot){
i++;
fh1=children.item(i);
fh1.style.backgroundColor="purple";
fh2=children.item(j);
fh2.style.backgroundColor="purple";
hg1=fh1.style.height;
hg2=fh2.style.height;
fh1.style.height=hg2;
fh2.style.height=hg1;
swap(i,j);
if(fh1.style.backgroundColor!="green") fh1.style.backgroundColor="red";
if(fh2.style.backgroundColor="green") fh2.style.backgroundColor="red";
}
}
swap(i+1,high);
fh3=children.item(i+1);
hg3=fh3.style.height;
fh4=children.item(high);
hg4=fh4.style.height;
fh3.style.backgroundColor="orange";
fh4.style.backgroundColor="orange";
fh3.style.height=hg4;
fh4.style.height=hg3;
fh3.style.backgroundColor="green";
await new Promise(resolve => setTimeout(() => {resolve()},speed));
return (i+1);
}
async function quickSort(low,high){
if(low<high){
let pi=await partition(low,high);
await quickSort(low,pi-1);
await quickSort(pi+1,high);
}
else{
if(low>=0 && low<sze){
fh5=children.item(low);
fh5.style.backgroundColor="green";
}
}
}
document.getElementById("quickSort").addEventListener("click",async function(){
disableSortingButton();
disableNewArrayBtn();
disableSizeSlider();
await quickSort(0,sze-1);
enableSortingBtn();
enableNewArrayBtn();
enableSizeSlider();
});