forked from MastanSayyad/Visual-Sort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Insertion.html
190 lines (160 loc) · 7.65 KB
/
Insertion.html
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
188
189
190
<!DOCTYPE html>
<html>
<title>Insertion Sort</title>
<script src="https://d3js.org/d3.v3.js"></script>
<meta charset="UTF-8">
<head>
<link rel="stylesheet" href="style.css">
<style>
/* div {margin-left: 40px;margin-top: 20px} */
#buttons {margin-left: 40px;margin-top: 20px}
text {fill: black;}
rect {fill: white;}
.unsorted{ fill:white}
.sorted {fill: rgb(235, 241, 235);}
.min {fill: rgb(217, 239, 21);;}
.testing {fill: rgb(217, 239, 21);}
</style>
<script src="http://d3js.org/d3.v4.min.js"></script>
</head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://www.w3schools.com/lib/w3-theme-black.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.3.0/css/font-awesome.min.css">
<link rel="stylesheet" href="css/Styling.css">
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"/>
<body>
<div id="Loader"></div>
<div class="w3-bar w3-theme">
<a href="index.html" class="w3-bar-item w3-button w3-padding-16">Home</a>
<a href="Bubble.html" class="w3-bar-item w3-button w3-padding-16">Bubble Sort</a>
<a href="Insertion.html" class="w3-bar-item w3-button w3-padding-16">Insertion Sort</a>
<a href="selectionsort.html" class="w3-bar-item w3-button w3-padding-16">Selection Sort</a>
<a href="mergesort.html" class="w3-bar-item w3-button w3-padding-16">Merge Sort</a>
</div>
<div id="contain">
<p><h2 id="heading">INSERTION SORT</h2></p>
<div class="sh"></div>
<div>
<div id="buttons">
<input id= "array" type="text" placeholder="Enter array seperated by space" size="30">
<button id="submit" class="btn-primary btn-default" onclick="submit()">Submit</button>
<button class="btn-default " onclick="insertionSort()" >Insertion Sort</button>
<button class="btn-default" onclick="stop = true" style="margin-left:40px">Stop</button>
<button class="btn-default" onclick="stop = false,insertionSort()">Resume</button>
<button class="btn-default" onclick="reset()">Reset</button>
<button class="btn-deafault btn-danger" onClick="window.location.reload();">Clear</button>
<button id="submit" class="btn-primary btn-default" onclick="submit()" style="margin-left:40px">Submit</button>
<button class="btn-default " onclick="insertionSort()" >Insertion Sort</button>
</div>
</div>
</div>
<script>
var loader = document.getElementById("Loader");
window.addEventListener("load", function(){
loader.style.display = "none";
})
</script>
</body>
<script>
var count = 9 + 1,
durationTime = 800,
array = [],
unsortedArray = [...array],
sortedArray = [],
stop = false;
steps=0;
var margin = {top: 40, right: 40, bottom: 180, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var barWidth = width/count;
var x = d3.scaleLinear()
.domain([0,count])
.range([0, width]);
var svg = d3.select('.sh').append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
var labels;
var rects;
function submit(){
document.getElementById("submit").disabled = true;
var array2 = document.getElementById("array");
array2 = array2.value.split(" ").map(function (item) {
return parseInt(item, 10);
});
console.log(array2);
array = array2;
unsortedArray = [...array];
rects = svg.append("g")
.attr("transform", "translate(" + barWidth + ",2)")
.selectAll("rect")
.data(unsortedArray)
.enter().append("rect");
rects.attr("id", function(d) {return "rect" + d})
.attr("transform", function(d, i) {return "translate(" + (x(i) - barWidth) + ",0)"})
.attr("width", barWidth *.9)
.attr("height", function(d) {return d*barWidth/3});
rects.attr("y",function(d){ return 400-d*barWidth/3; });
labels = svg.selectAll("text")
.data(unsortedArray)
.enter().append("text")
labels.attr("id", function(d) {return "text" + d})
.attr("transform", function(d, i) {return "translate(" + x(i) + ",0)"})
.html(function(d) {return d;})
.attr("class","unsorted")
}
function reset() {
unsortedArray = [...array];
sortedArray = [];
stop = false;
d3.select("#counter").html(steps = 0)
labels.attr("class", "")
.transition().duration(2000)
.attr("transform", function(d, i) {return "translate(" + (x(i)) + ", 0)"})
.attr("class","unsorted")
rects.attr("class", "")
.transition().duration(2000)
.attr("transform", function(d, i) {return "translate(" + (x(i-1)) + ", 0)"})
rects.attr("id", function(d) {return "rect" + d})
.attr("transform", function(d, i) {return "translate(" + (x(i) - barWidth) + ",0)"})
.attr("width", barWidth *.9)
.attr("height", function(d) {return d*barWidth/3})
rects.attr("y",function(d){ return 400-d*barWidth/3; });
}
function insertionSort() {
var value = unsortedArray.shift();
sortedArray.push(value);
reArrange(sortedArray.length-1);
function reArrange(n) {
if (stop) return stop = false;
d3.selectAll("rect").attr("class", "")
d3.select("#rect" + value).attr("class", "testing")
d3.select("#text" + value).attr("class", "sorted")
if (n > 0 && sortedArray[n-1] > value) {
d3.timeout(function() {
sortedArray.splice(n,1);
sortedArray.splice(n-1,0,value);
slide(sortedArray[n], n);
slide(sortedArray[n-1], n-1);
reArrange(--n)
}, durationTime * 2);
} else if (unsortedArray.length) {
d3.timeout(function() {insertionSort()}, durationTime * 2);
} else {
d3.selectAll("rect").attr("class", "").attr("style","fill:rgb(235, 241, 235)");
}
}
}
function slide(d, i) {
d3.select("#text" + d)
.transition().duration(durationTime)
.attr("transform", function(d) {return "translate(" + (x(i)) + ", 0)"})
d3.select("#rect" + d)
.transition().duration(durationTime)
.attr("transform", function(d) {return "translate(" + (x(i-1)) + ", 0)"})
}
</script>
</html>