-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsertion.html
196 lines (165 loc) · 7.64 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
191
192
193
194
195
196
<!DOCTYPE html>
<html>
<title>Insertion Sort</title>
<script src="https://d3js.org/d3.v3.js"></script>
<meta charset="UTF-8">
<head>
<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(10, 223, 28);}
.min {fill: rgb(182, 50, 50);;}
.testing {fill: red;}
</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="Styling.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 class="w3-bar w3-theme">
<a href="Home.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="Help.html" class="w3-bar-item w3-button w3-padding-16">Help</a>
<a href="About.html" class="w3-bar-item w3-button w3-padding-16">About</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,bubbleSort()">Resume</button>
<button class="btn-default" onclick="reset()">Reset</button>
<button class="btn-deafault btn-danger" onClick="window.location.reload();">Clear</button>
</div>
</div>
</div>
</body>
<script>
var count = 1 + 10,
durationTime = 400,
array = [],
unsortedArray = [...array],
sortedArray = [],
stop = false;
var barWidth = width/count;
var margin = {top: 40, right: 40, bottom: 180, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
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 rects = svg.append("g")
.attr("transform", "translate(" + barWidth + ",2)")
.selectAll("rect")
.data(unsortedArray)
.enter().append("rect")
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")
label2=svg.select("text").data(sortedArray).enter().append("text")
label2.text("dslfjadslf")
label2.attr("x",50).attr("y",50);
label2.attr("font-family","sans-serif").attr("font-size",12);
}
function reset() {
unsortedArray = [...array];
sortedArray = [];
stop = false;
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; });
rects.attr("style","fill:blue")
}
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(10, 223, 28)");
}
}
}
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>
<!-- Footer -->
<footer class="w3-container w3-theme-dark w3-padding-16">
<div style="position:relative;bottom:55px;" class="w3-tooltip w3-right">
<span class="w3-text w3-theme-light w3-padding">Go To Top</span>
<a class="w3-text-white" href="#myHeader"><span class="w3-xlarge">
<i class="fa fa-chevron-circle-up"></i></span></a>
</div>
</footer>
</html>