-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathquick.js
77 lines (67 loc) · 2.25 KB
/
quick.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
async function partion(e,l,r){
let i = l-1;
//pivot
e[r].style.background = ' var(--bars-sorting-red-color)';
for(let j = l ; j<=r-1;j++)
{
e[j].style.background = 'yellow';
await waitForMe(delay);
if(parseInt(e[j].style.height) < parseInt(e[r].style.height)){
i++;
swap(e[i],e[j]);
e[i].style.background = 'orange';
if(i!=j)
e[j].style.background = 'orange';
await waitForMe(delay);
}
else{
e[j].style.background ='var(--bars-sorting-blue-color)';
}
}
i++;
await waitForMe(delay);
swap(e[i],e[r]);
e[r].style.background = 'var(--bars-sorting-blue-color)';
e[i].style.background = 'var(--bars-sorted-color)';
await waitForMe(delay);
// for(let k = 0 ; k<e.length;k++){
// if(e[k].style.background!='green');
// e[k].style.background = 'var(--bars-color)';
// }
return i; //returning pivot position;
}
async function quicksort(e,l,r){
if(l<r){
let pivot = await partion(e,l,r);
await quicksort(e,l,pivot-1);
await quicksort(e,pivot+1,r);
}
else{
if(l>=0 && r>=0 && l <e.length && r<e.length){
e[r].style.background = 'var(--bars-sorted-color)';
e[l].style.background = 'var(--bars-sorted-color)';
}
}
}
const quick = document.querySelector('.quickSort');
quick.addEventListener("click",async function(){
let e = document.querySelectorAll(".bar");
let l = 0;
let r = e.length-1;
disableSortBtn();
disableSizeSlider();
disableNewArrayBtn();
diablePerformanceBtn();
const startTime = performance.now();
await quicksort(e,l,r);
const endTime = performance.now();
console.log(`Call to doSomething took ${endTime - startTime} milliseconds.`);
var text = document.getElementById("content");
const p = document.querySelectorAll(".bar");
var time = (endTime - startTime)/1000;
text.textContent = "Array Consist of "+ p.length+" elements and got sorted in "+time.toPrecision(2)+" seconds with a Speed Delay of " + delay+" miliseconds.";
enableSortBtn();
enablePerformanceBtn();
enableNewArrayBtn();
enableSizeSlider();
})