-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.js
More file actions
187 lines (180 loc) · 5.59 KB
/
sort.js
File metadata and controls
187 lines (180 loc) · 5.59 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//-----------------------------PREPARATION------------------------------
let divs = document.getElementsByClassName("lightBar");
let theBody = document.querySelector(".lightBody");
let hOne= document.querySelector(".lightH1");
let box= document.querySelector(".lightBox");
let buttonBox=document.querySelector(".lightButtonBox");
let barChange='lightBarChange'
//------------------------------buttons---------------------------------
//bubble
let bubbleBtn = document.querySelector("#bubble");
bubbleBtn.onclick = bubbleSort;
//selection
let selectionBtn = document.querySelector("#selection");
selectionBtn.onclick = selectionSort;
//insertion
let insertionBtn = document.querySelector("#insertion");
insertionBtn.onclick = insertionSort;
//quicksort
let quicksortBtn = document.querySelector("#quicksort");
quicksortBtn.onclick = fullQuickSort;
//randomize button
let randomizeBtn = document.querySelector("#randomize");
randomizeBtn.onclick = randomize;
//theme button
let themeBtn= document.querySelector("#themeBtn");
themeBtn.onclick=themeChange;
// ----------------------------------------------------------------------
let values = [];
//GENERATING RANDOM VALUES AND SETTING UP BARS
function randomize() {
for (let i = 0; i < 10; i++) {values[i] = Math.floor(Math.random() * 90 + 5);}
//SETTING UP BARS FOR ACTION
for (let i = 0; i < 10; i++) {
divs[i].style.height = `${values[i]}%`;
divs[i].innerText = `${values[i]}`;
divs[i].classList.remove(barChange)
}
}
//DELAYED COLOR CHANGE FUNCTION
let delayedColorChange = function(element,classChange,delay) {
return new Promise(function(resolve, reject){
setTimeout(function() {
element.classList.toggle(classChange);
resolve();
},delay)
})
}
//THEME CHANGE FUNCTION
function themeChange()
{
for(let j=0;j<10;j++){
divs[j].classList.toggle('darkBar')
divs[j].classList.remove('darkBarChange')
}
theBody.classList.toggle('darkBody')
hOne.classList.toggle('darkH1')
box.classList.toggle('darkBox')
buttonBox.classList.toggle('darkButtonBox')
bubbleBtn.classList.toggle('darkBubble')
selectionBtn.classList.toggle('darkSelection')
randomizeBtn.classList.toggle('darkRandomize')
insertionBtn.classList.toggle('darkInsertion')
quicksortBtn.classList.toggle('darkQuicksort')
themeBtn.classList.toggle('darkTheme')
if(themeBtn.innerText==='Light'){
themeBtn.innerText='Dark'
barChange='lightBarChange'
}
else{
themeBtn.innerText='Light'
barChange='darkBarChange'
}
}
//-----------------------------SHOW STARTS----------------------------
randomize();
//BUBBLE SORT
async function bubbleSort()
{
for (let i = 0; i < 10-1; i++)
{
for (let j = 0; j < 10-i-1; j++)
{
if (values[j] > values[j+1])
{
//swapping values
[values[j], values[j+1]] = [values[j+1], values[j]];
divs[j].style.height = `${values[j]}%`;
divs[j].innerText = `${values[j]}`;
divs[j+1].style.height = `${values[j+1]}%`;
divs[j+1].innerText = `${values[j+1]}`;
}
}
await delayedColorChange(divs[9-i],barChange,600)
}
await delayedColorChange(divs[0],barChange,600)
}
//SELECTION SORT
async function selectionSort()
{
for (let i = 0; i < 9; i++)
{
let mindex = i;
for (let j = i + 1; j < 10; j++)
{
if (values[j] < values[mindex])
{
mindex = j;
}
}
[values[mindex], values[i]] = [values[i], values[mindex]];
divs[mindex].style.height = `${values[mindex]}%`;
divs[mindex].innerText = `${values[mindex]}`;
divs[i].style.height = `${values[i]}%`;
divs[i].innerText = `${values[i]}`;
await delayedColorChange(divs[i],barChange,600)
}
await delayedColorChange(divs[9],barChange,600)
}
//INSERTION SORT
async function insertionSort() {
let n = values.length;
await delayedColorChange(divs[0],barChange,600)
for (let i = 1; i < n; i++) {
let current = values[i];
let j = i-1;
while ((j > -1) && (current < values[j])) {
values[j+1] = values[j];
divs[j+1].style.height = `${values[j+1]}%`;
divs[j+1].innerText = `${values[j+1]}`;
j--;
}
values[j+1] = current;
divs[j+1].style.height = `${values[j+1]}%`;
divs[j+1].innerText = `${values[j+1]}`;
await delayedColorChange(divs[i],barChange,600)
}
}
//QUICKSORT
async function fullQuickSort() {
let left=0;let right=0;
function partition(left,right){
let pivot=values[Math.floor((right+left)/2)];
let i=left;
let j=right;
while(i<=j){
while(values[i]<pivot){
i++;
}
while(values[j]>pivot){
j--;
}
if(i<=j){
[values[i],values[j]]=[values[j],values[i]];
divs[i].style.height = `${values[i]}%`;
divs[i].innerText = `${values[i]}`;
divs[j].style.height = `${values[j]}%`;
divs[j].innerText = `${values[j]}`;
i++;j--;
}
}
// await delayedColorChange(divs[i],barChange,600) <- with this, the function works only once
return i;
}
async function quickSort(left,right) {
let index;
if(values.length>1){
index=partition(left,right);
await delayedColorChange(divs[index],barChange,1000)
if(left<index-1){
quickSort(left,index-1);
}
if(index<right){
quickSort(index,right);
}
}
}
await quickSort(0,9);
// divs[0].classList.add(barChange);
await delayedColorChange(divs[0],barChange,600)
}