Skip to content

Commit 20536e9

Browse files
authored
Merge pull request #23 from mohilkhare1708/beta
Implemented Insertion Sort Functionality
2 parents 30142a7 + 4b02a90 commit 20536e9

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

public/sorting/js/insertion_sort.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,35 @@ import {
55
} from "./main_sort.js";
66

77
var c_delay = 1;
8-
var delay_time = 40 - 2*(speed - 1);
8+
var delay_time = 40 - 2 * (speed - 1);
99

1010
function div_update(cont, h, color) {
11-
setTimeout(function(){
11+
setTimeout(function() {
1212
cont.style =
1313
" background:" + color + "; width:" + width + "%;" +
1414
"box-shadow: 2px -2px 3px #5F5F5F, 2px -2px 3px white; " +
1515
" height:" + h + "%;";
16-
}, c_delay+=delay_time);
16+
}, c_delay += delay_time);
1717
}
1818

1919

2020
export function insertion(divs, height) {
21-
22-
// Write your code here
23-
21+
c_delay = 0;
22+
delay_time = 10000 / (Math.floor(n / 30) * speed);
23+
console.log(c_delay, delay_time);
24+
var i, keyD, keyH, j;
25+
for (i = 1; i < n; i++) {
26+
div_update(divs[2 * i + 1], height[i], "red");
27+
keyH = height[i];
28+
j = i - 1;
29+
while (j >= 0 && height[j] > height[j + 1]) {
30+
var temp = height[j];
31+
height[j] = height[j + 1];
32+
height[j + 1] = temp;
33+
div_update(divs[2 * (j + 1) + 1], height[j + 1], "green");
34+
div_update(divs[2 * j + 1], height[j], "red");
35+
j = j - 1;
36+
}
37+
div_update(divs[2 * (j + 1) + 1], height[j + 1], "green");
38+
}
2439
}

public/sorting/js/main_sort.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
// Imported Functions
32
import {
43
bubble
@@ -9,6 +8,9 @@ import {
98
import {
109
quick
1110
} from './quick_sort.js';
11+
import {
12+
insertion
13+
} from './insertion_sort.js';
1214
import {
1315
selection
1416
} from './selection_sort.js';
@@ -71,6 +73,7 @@ function start() {
7173
else if (algo === "quick") quick(divs, arr);
7274
else if (algo === "heap") heap(divs, arr);
7375
else if (algo === "selection") selection(divs, arr);
76+
else if (algo === "insertion") insertion(divs, arr);
7477
console.log(algo);
7578
}
7679

@@ -80,14 +83,14 @@ function refresh() {
8083
}
8184

8285
// Changing the size of array on changing the array count slider
83-
sliderCount.oninput = function () {
86+
sliderCount.oninput = function() {
8487
n = sliderCount.value;
8588
width = 100 / (2 * n);
8689
GenerateArr();
8790
}
8891

8992
// Changing the speed of array on changing the value of Visuaal Speed Slider
90-
sliderSpeed.oninput = function () {
93+
sliderSpeed.oninput = function() {
9194
speed = sliderSpeed.value;
9295
}
9396

0 commit comments

Comments
 (0)