-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubble_sort.html
188 lines (163 loc) · 5.63 KB
/
bubble_sort.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bubble Sort Visualization</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
background-image: url('https://img.freepik.com/free-vector/gradient-style-network-connection-background_23-2148876716.jpg?size=626&ext=jpg&ga=GA1.1.508824631.1698051133&semt=ais');
}
#inputForm {
margin-bottom: 20px;
}
#barsContainer {
display: flex;
justify-content: center;
align-items: flex-end;
height: 700px;
}
.bar {
width: 7vw;
margin-right: 5px;
display: inline-block;
background-color:white;
height: 20px;
transition: background-color 0.5s;
position: relative;
}
.bar span {
position: absolute;
bottom: -20px;
left: 0;
width: 100%;
text-align: center;
font-size: 20px;
color:white;
}
label{
font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
font-size: xx-large;
margin: 10px;
font: bolder;
color: white;
}
button{
padding: 10px;
width: 200px;
margin-top: 50px;
font: bolder;
font-size: xx-large;
border-radius: 5px;
border-color:aliceblue;
text-align: center;
margin-left: 40%;
background-color: orangered;
color: white;
}
button:hover {
background: #03f40f;
color: #fff;
border-radius: 5px;
box-shadow: 0 0 5px #03f40f,
0 0 25px #03f40f,
0 0 50px #03f40f,
0 0 100px #03f40f;
transition: 0.5s;
}
input{
box-sizing: border-box;
height: 35px;
width: 400px;
border-radius: 8px;
background-color: white;
font-size: x-large;
}
h1{
font-family: Cambria, Cochin, Georgia, Times, 'Times New Roman', serif;
font-size: 50px;
color:white;
letter-spacing: 5px;
}
</style>
</head>
<body>
<div id="inputForm">
<form onsubmit="startVisualization(event)">
<h1>Bubble Sort Visualization</h1>
<label for="arraySize" >Enter Array size:</label>
<input type="number" id="arraySize" name="arraySize" required>
<br>
<label for="arrayElements">Enter Array elements:</label>
<input type="text" id="arrayElements" name="arrayElements" required placeholder="input number less than 70">
<br>
<button id="mybutton" type="submit">Sort</button>
</form>
</div>
<div id="barsContainer">
<div id="bars"></div>
</div>
<script>
// Function to create and display bars representing array elements
function displayBars(array, selectedIndexes = []) {
var barsDiv = document.getElementById("bars");
barsDiv.innerHTML = "";
for (var i = 0; i < array.length; i++) {
var barHeight = array[i] * 10; // Scale factor to make bars visible
var bar = document.createElement("div");
bar.className = "bar";
if (selectedIndexes.includes(i)) {
bar.style.backgroundColor = "red";
}
bar.style.height = barHeight + "px";
var valueSpan = document.createElement("span");
valueSpan.textContent = array[i];
bar.appendChild(valueSpan);
barsDiv.appendChild(bar);
}
}
// Function to perform bubble sort and update the visualization
async function bubbleSort(array) {
for (var i = 0; i < array.length; i++) {
var swaped= false;
for (var j = 0; j < array.length-i-1; j++) {
if (array[j+1] < array[j]) {
// Swap elements
var temp = array[j+1];
array[j+1] = array[j];
array[j] = temp;
}
// Update visualization
displayBars(array, [j+1, j]);
await sleep(500); // Delay for visualization
}
if (swaped)
{
break;
}
}
// Final visualization with sorted array
displayBars(array);
}
// Helper function for delaying execution
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// Function to start the visualization
async function startVisualization(event) {
event.preventDefault(); // Prevent form submission
var arraySizeInput = document.getElementById("arraySize");
var arrayElementsInput = document.getElementById("arrayElements");
var n = parseInt(arraySizeInput.value);
var array = arrayElementsInput.value
.split(" ")
.map(element => parseInt(element));
await bubbleSort(array);
console.log("Sorted array:", array);
}
</script>
</body>
</html>