-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquicksort.js
106 lines (75 loc) · 2 KB
/
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
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
async function partitionindex(ele,start,end)
{
let i=start-1;
ele[end].style.background='red';
for(let j=start;j<=end-1;j++)
{
ele[j].style.background='yellow';
await waitforme(delay);
if(parseInt(ele[j].style.height)<parseInt(ele[end].style.height))
{
i++;
if(i!=j)
{
ele[i].style.background='yellow';
await waitforme(delay);
swap(ele[i],ele[j]);
}
ele[i].style.background='pink';
if(i!=j)
ele[j].style.background='#FF1493';
}
else
{
ele[j].style.background='#FF1493';
}
}
i++;
await waitforme(delay);
ele[end].style.background='red';
ele[i].style.background='red';
swap(ele[i],ele[end]);
await waitforme(delay);
ele[end].style.background='#FF1493';
ele[i].style.background='green';
await waitforme(delay);
for(let k=0;k<ele.length;k++)
{
if(ele[k].style.background!='green')
{
ele[k].style.background='#1E90FF';
}
}
return i;
}
async function quick_sort(ele,start,end)
{
if(start<end)
{
let pivot = await partitionindex(ele,start,end);
await quick_sort(ele,start,pivot-1);
await quick_sort(ele,pivot+1,end);
}
else
{
await waitforme(delay);
if(start>=0 && end<ele.length && start<ele.length && end>=0)
{
ele[start].style.background = 'green';
ele[end].style.background = 'green';
}
}
}
const quicksortbtn = document.querySelector(".quickSort");
quicksortbtn.addEventListener('click',async function(){
let ele = document.querySelectorAll(".bar");
let start=0;
let end=ele.length-1;
disablesortbtn();
disableSizeSlider();
disableNewArrayBtn();
await quick_sort(ele,start,end);
enablingsortbtn();
enableSizeSlider();
enableNewArrayBtn();
})