Skip to content

Commit 9a390fc

Browse files
committed
first commit
1 parent 6839e21 commit 9a390fc

File tree

11 files changed

+675
-0
lines changed

11 files changed

+675
-0
lines changed

README.md

35 Bytes

Sorting Algorith js/bubble.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
async function bubble() {
2+
console.log('In bubbe()');
3+
const ele = document.querySelectorAll(".bar");
4+
for(let i = 0; i < ele.length-1; i++){
5+
console.log('In ith loop');
6+
for(let j = 0; j < ele.length-i-1; j++){
7+
console.log('In jth loop');
8+
ele[j].style.background = 'blue';
9+
ele[j+1].style.background = 'blue';
10+
if(parseInt(ele[j].style.height) > parseInt(ele[j+1].style.height)){
11+
console.log('In if condition');
12+
await waitforme(delay);
13+
swap(ele[j], ele[j+1]);
14+
}
15+
ele[j].style.background = 'cyan';
16+
ele[j+1].style.background = 'cyan';
17+
}
18+
ele[ele.length-1-i].style.background = 'green';
19+
}
20+
ele[0].style.background = 'green';
21+
}
22+
23+
const bubSortbtn = document.querySelector(".bubbleSort");
24+
bubSortbtn.addEventListener('click', async function(){
25+
disableSortingBtn();
26+
disableSizeSlider();
27+
disableNewArrayBtn();
28+
await bubble();
29+
enableSortingBtn();
30+
enableSizeSlider();
31+
enableNewArrayBtn();
32+
});

Sorting Algorith js/insertion.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
async function insertion(){
2+
console.log('In insertion()');
3+
const ele = document.querySelectorAll(".bar");
4+
// color
5+
ele[0].style.background = 'green';
6+
for(let i = 1; i < ele.length; i++){
7+
console.log('In ith loop');
8+
let j = i - 1;
9+
let key = ele[i].style.height;
10+
// color
11+
ele[i].style.background = 'blue';
12+
13+
await waitforme(delay);
14+
15+
while(j >= 0 && (parseInt(ele[j].style.height) > parseInt(key))){
16+
console.log('In while loop');
17+
// color
18+
ele[j].style.background = 'blue';
19+
ele[j + 1].style.height = ele[j].style.height;
20+
j--;
21+
22+
await waitforme(delay);
23+
24+
// color
25+
for(let k = i; k >= 0; k--){
26+
ele[k].style.background = 'green';
27+
}
28+
}
29+
ele[j + 1].style.height = key;
30+
// color
31+
ele[i].style.background = 'green';
32+
}
33+
}
34+
35+
const inSortbtn = document.querySelector(".insertionSort");
36+
inSortbtn.addEventListener('click', async function(){
37+
disableSortingBtn();
38+
disableSizeSlider();
39+
disableNewArrayBtn();
40+
await insertion();
41+
enableSortingBtn();
42+
enableSizeSlider();
43+
enableNewArrayBtn();
44+
});

Sorting Algorith js/merge.js

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
async function merge(ele, low, mid, high){
2+
console.log('In merge()');
3+
console.log(`low=${low}, mid=${mid}, high=${high}`);
4+
const n1 = mid - low + 1;
5+
const n2 = high - mid;
6+
console.log(`n1=${n1}, n2=${n2}`);
7+
let left = new Array(n1);
8+
let right = new Array(n2);
9+
10+
for(let i = 0; i < n1; i++){
11+
await waitforme(delay);
12+
console.log('In merge left loop');
13+
console.log(ele[low + i].style.height + ' at ' + (low+i));
14+
// color
15+
ele[low + i].style.background = 'orange';
16+
left[i] = ele[low + i].style.height;
17+
}
18+
for(let i = 0; i < n2; i++){
19+
await waitforme(delay);
20+
console.log('In merge right loop');
21+
console.log(ele[mid + 1 + i].style.height + ' at ' + (mid+1+i));
22+
// color
23+
ele[mid + 1 + i].style.background = 'yellow';
24+
right[i] = ele[mid + 1 + i].style.height;
25+
}
26+
await waitforme(delay);
27+
let i = 0, j = 0, k = low;
28+
while(i < n1 && j < n2){
29+
await waitforme(delay);
30+
console.log('In merge while loop');
31+
console.log(parseInt(left[i]), parseInt(right[j]));
32+
33+
// To add color for which two r being compared for merging
34+
35+
if(parseInt(left[i]) <= parseInt(right[j])){
36+
console.log('In merge while loop if');
37+
// color
38+
if((n1 + n2) === ele.length){
39+
ele[k].style.background = 'green';
40+
}
41+
else{
42+
ele[k].style.background = 'lightgreen';
43+
}
44+
45+
ele[k].style.height = left[i];
46+
i++;
47+
k++;
48+
}
49+
else{
50+
console.log('In merge while loop else');
51+
// color
52+
if((n1 + n2) === ele.length){
53+
ele[k].style.background = 'green';
54+
}
55+
else{
56+
ele[k].style.background = 'lightgreen';
57+
}
58+
ele[k].style.height = right[j];
59+
j++;
60+
k++;
61+
}
62+
}
63+
while(i < n1){
64+
await waitforme(delay);
65+
console.log("In while if n1 is left");
66+
// color
67+
if((n1 + n2) === ele.length){
68+
ele[k].style.background = 'green';
69+
}
70+
else{
71+
ele[k].style.background = 'lightgreen';
72+
}
73+
ele[k].style.height = left[i];
74+
i++;
75+
k++;
76+
}
77+
while(j < n2){
78+
await waitforme(delay);
79+
console.log("In while if n2 is left");
80+
// color
81+
if((n1 + n2) === ele.length){
82+
ele[k].style.background = 'green';
83+
}
84+
else{
85+
ele[k].style.background = 'lightgreen';
86+
}
87+
ele[k].style.height = right[j];
88+
j++;
89+
k++;
90+
}
91+
}
92+
93+
async function mergeSort(ele, l, r){
94+
console.log('In mergeSort()');
95+
if(l >= r){
96+
console.log(`return cause just 1 elemment l=${l}, r=${r}`);
97+
return;
98+
}
99+
const m = l + Math.floor((r - l) / 2);
100+
console.log(`left=${l} mid=${m} right=${r}`, typeof(m));
101+
await mergeSort(ele, l, m);
102+
await mergeSort(ele, m + 1, r);
103+
await merge(ele, l, m, r);
104+
}
105+
106+
const mergeSortbtn = document.querySelector(".mergeSort");
107+
mergeSortbtn.addEventListener('click', async function(){
108+
let ele = document.querySelectorAll('.bar');
109+
let l = 0;
110+
let r = parseInt(ele.length) - 1;
111+
disableSortingBtn();
112+
disableSizeSlider();
113+
disableNewArrayBtn();
114+
await mergeSort(ele, l, r);
115+
enableSortingBtn();
116+
enableSizeSlider();
117+
enableNewArrayBtn();
118+
});

Sorting Algorith js/quick.js

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
2+
async function partitionLomuto(ele, l, r){
3+
console.log('In partitionLomuto()');
4+
let i = l - 1;
5+
// color pivot element
6+
ele[r].style.background = 'red';
7+
for(let j = l; j <= r - 1; j++){
8+
console.log('In partitionLomuto for j');
9+
// color current element
10+
ele[j].style.background = 'yellow';
11+
// pauseChamp
12+
await waitforme(delay);
13+
14+
if(parseInt(ele[j].style.height) < parseInt(ele[r].style.height)){
15+
console.log('In partitionLomuto for j if');
16+
i++;
17+
swap(ele[i], ele[j]);
18+
// color
19+
ele[i].style.background = 'orange';
20+
if(i != j) ele[j].style.background = 'orange';
21+
// pauseChamp
22+
await waitforme(delay);
23+
}
24+
else{
25+
// color if not less than pivot
26+
ele[j].style.background = 'pink';
27+
}
28+
}
29+
i++;
30+
// pauseChamp
31+
await waitforme(delay);
32+
swap(ele[i], ele[r]); // pivot height one
33+
console.log(`i = ${i}`, typeof(i));
34+
// color
35+
ele[r].style.background = 'pink';
36+
ele[i].style.background = 'green';
37+
38+
// pauseChamp
39+
await waitforme(delay);
40+
41+
// color
42+
for(let k = 0; k < ele.length; k++){
43+
if(ele[k].style.background != 'green')
44+
ele[k].style.background = 'cyan';
45+
}
46+
47+
return i;
48+
}
49+
50+
async function quickSort(ele, l, r){
51+
console.log('In quickSort()', `l=${l} r=${r}`, typeof(l), typeof(r));
52+
if(l < r){
53+
let pivot_index = await partitionLomuto(ele, l, r);
54+
await quickSort(ele, l, pivot_index - 1);
55+
await quickSort(ele, pivot_index + 1, r);
56+
}
57+
else{
58+
if(l >= 0 && r >= 0 && l <ele.length && r <ele.length){
59+
ele[r].style.background = 'green';
60+
ele[l].style.background = 'green';
61+
}
62+
}
63+
}
64+
65+
66+
const quickSortbtn = document.querySelector(".quickSort");
67+
quickSortbtn.addEventListener('click', async function(){
68+
let ele = document.querySelectorAll('.bar');
69+
let l = 0;
70+
let r = ele.length - 1;
71+
disableSortingBtn();
72+
disableSizeSlider();
73+
disableNewArrayBtn();
74+
await quickSort(ele, l, r);
75+
enableSortingBtn();
76+
enableSizeSlider();
77+
enableNewArrayBtn();
78+
});

Sorting Algorith js/selection.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
async function selection(){
2+
console.log('In selection()');
3+
const ele = document.querySelectorAll(".bar");
4+
for(let i = 0; i < ele.length; i++){
5+
console.log('In ith loop');
6+
let min_index = i;
7+
// Change color of the position to swap with the next min
8+
ele[i].style.background = 'blue';
9+
for(let j = i+1; j < ele.length; j++){
10+
console.log('In jth loop');
11+
// Change color for the current comparision (in consideration for min_index)
12+
ele[j].style.background = 'red';
13+
14+
await waitforme(delay);
15+
if(parseInt(ele[j].style.height) < parseInt(ele[min_index].style.height)){
16+
console.log('In if condition height comparision');
17+
if(min_index !== i){
18+
// new min_index is found so change prev min_index color back to normal
19+
ele[min_index].style.background = 'cyan';
20+
}
21+
min_index = j;
22+
}
23+
else{
24+
// if the currnent comparision is more than min_index change is back to normal
25+
ele[j].style.background = 'cyan';
26+
}
27+
}
28+
await waitforme(delay);
29+
swap(ele[min_index], ele[i]);
30+
// change the min element index back to normal as it is swapped
31+
ele[min_index].style.background = 'cyan';
32+
// change the sorted elements color to green
33+
ele[i].style.background = 'green';
34+
}
35+
}
36+
37+
const selectionSortbtn = document.querySelector(".selectionSort");
38+
selectionSortbtn.addEventListener('click', async function(){
39+
disableSortingBtn();
40+
disableSizeSlider();
41+
disableNewArrayBtn();
42+
await selection();
43+
enableSortingBtn();
44+
enableSizeSlider();
45+
enableNewArrayBtn();
46+
});

0 commit comments

Comments
 (0)