File size: 32,283 Bytes
2340e29 |
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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Insertion Sort Visualizer</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<script>
tailwind.config = {
theme: {
extend: {
colors: {
primary: '#3B82F6',
secondary: '#10B981',
highlight: '#F59E0B',
sorted: '#10B981',
comparing: '#FBBF24',
current: '#EF4444',
info: '#60A5FA'
}
}
}
}
</script>
<style>
.array-bar {
transition: all 0.3s ease;
}
.algorithm-step {
transition: background-color 0.3s ease;
}
.card-hover {
transition: transform 0.3s ease, box-shadow 0.3s ease;
}
.card-hover:hover {
transform: translateY(-5px);
box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
}
</style>
</head>
<body class="bg-gray-50 font-sans">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
<!-- Header -->
<header class="text-center mb-12">
<h1 class="text-4xl font-bold text-gray-900 mb-2">Insertion Sort Visualizer</h1>
<p class="text-gray-600 max-w-3xl mx-auto">
Understand insertion sort algorithm through step-by-step visualization and detailed explanation
</p>
</header>
<div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
<!-- Algorithm Explanation -->
<div class="lg:col-span-2 space-y-8">
<div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
<div class="p-6 border-b border-gray-200 bg-gradient-to-r from-primary to-blue-400">
<h2 class="text-2xl font-bold text-white">Understanding Insertion Sort</h2>
</div>
<div class="p-6">
<div class="flex items-start mb-6">
<div class="bg-blue-100 p-4 rounded-full mr-4">
<i class="fas fa-lightbulb text-blue-600 text-xl"></i>
</div>
<div>
<h3 class="text-lg font-semibold text-gray-800 mb-2">What is Insertion Sort?</h3>
<p class="text-gray-600">
Insertion sort is a simple, efficient sorting algorithm that builds the final sorted array one element at a time.
It works similarly to how you might sort a hand of playing cards by repeatedly inserting a new card into
its correct position in your already sorted hand.
</p>
</div>
</div>
<div class="bg-blue-50 rounded-lg p-5 mb-6">
<h3 class="font-bold text-gray-800 flex items-center mb-3">
<i class="fas fa-code mr-2 text-blue-600"></i> Algorithmic Steps
</h3>
<ul class="space-y-3">
<li class="flex items-start">
<span class="bg-blue-500 text-white rounded-full h-6 w-6 flex items-center justify-center mr-3 flex-shrink-0">1</span>
<span class="text-gray-700">Start with the second element (index 1) and consider it as the key</span>
</li>
<li class="flex items-start">
<span class="bg-blue-500 text-white rounded-full h-6 w-6 flex items-center justify-center mr-3 flex-shrink-0">2</span>
<span class="text-gray-700">Compare key with elements to its left</span>
</li>
<li class="flex items-start">
<span class="bg-blue-500 text-white rounded-full h-6 w-6 flex items-center justify-center mr-3 flex-shrink-0">3</span>
<span class="text-gray-700">If key is smaller, shift elements to the right</span>
</li>
<li class="flex items-start">
<span class="bg-blue-500 text-white rounded-full h-6 w-6 flex items-center justify-center mr-3 flex-shrink-0">4</span>
<span class="text-gray-700">Insert key in its correct sorted position</span>
</li>
<li class="flex items-start">
<span class="bg-blue-500 text-white rounded-full h-6 w-6 flex items-center justify-center mr-3 flex-shrink-0">5</span>
<span class="text-gray-700">Repeat for all elements in the array</span>
</li>
</ul>
</div>
<div class="grid grid-cols-1 md:grid-cols-2 gap-4 mb-6">
<div class="bg-emerald-50 rounded-lg p-5">
<h3 class="font-bold text-gray-800 flex items-center mb-3">
<i class="fas fa-check-circle mr-2 text-emerald-600"></i> Advantages
</h3>
<ul class="space-y-2 text-gray-700">
<li class="flex items-start">
<i class="fas fa-chevron-right text-emerald-500 mt-1 mr-2 text-xs"></i>
<span>Efficient for small data sets</span>
</li>
<li class="flex items-start">
<i class="fas fa-chevron-right text-emerald-500 mt-1 mr-2 text-xs"></i>
<span>Adaptive - efficient for nearly sorted data</span>
</li>
<li class="flex items-start">
<i class="fas fa-chevron-right text-emerald-500 mt-1 mr-2 text-xs"></i>
<span>Stable sorting algorithm</span>
</li>
<li class="flex items-start">
<i class="fas fa-chevron-right text-emerald-500 mt-1 mr-2 text-xs"></i>
<span>Low overhead - only O(1) additional memory</span>
</li>
</ul>
</div>
<div class="bg-amber-50 rounded-lg p-5">
<h3 class="font-bold text-gray-800 flex items-center mb-3">
<i class="fas fa-exclamation-triangle mr-2 text-amber-600"></i> Limitations
</h3>
<ul class="space-y-2 text-gray-700">
<li class="flex items-start">
<i class="fas fa-chevron-right text-amber-500 mt-1 mr-2 text-xs"></i>
<span>Inefficient for large lists (O(n²) worst-case)</span>
</li>
<li class="flex items-start">
<i class="fas fa-chevron-right text-amber-500 mt-1 mr-2 text-xs"></i>
<span>Performance degrades with large unsorted data</span>
</li>
<li class="flex items-start">
<i class="fas fa-chevron-right text-amber-500 mt-1 mr-2 text-xs"></i>
<span>Less efficient than algorithms like quicksort for larger datasets</span>
</li>
</ul>
</div>
</div>
</div>
</div>
<!-- Algorithm Demonstration Steps -->
<div class="bg-white rounded-xl shadow-md overflow-hidden card-hover">
<div class="p-6 border-b border-gray-200 bg-gradient-to-r from-secondary to-emerald-400">
<h2 class="text-2xl font-bold text-white">Step-by-Step Process</h2>
</div>
<div class="p-6">
<div id="stepContainer" class="space-y-4">
<div class="algorithm-step bg-white border border-gray-200 rounded-lg p-4">
<div class="flex items-center mb-2">
<div class="bg-green-500 text-white rounded-full h-8 w-8 flex items-center justify-center mr-3 flex-shrink-0">1</div>
<h3 class="font-bold text-gray-800">Start with the second element</h3>
</div>
<p class="text-gray-600 ml-11 mb-3">
Consider array[1] as the key (current element to be sorted)
</p>
<div class="flex justify-center">
<div class="flex items-end h-32">
<div class="array-bar flex flex-col items-center mx-1 w-12 bg-blue-500" style="height: 80px;">
<span class="text-white font-bold mt-1">8</span>
<span class="text-white text-xs">[0]</span>
</div>
<div class="array-bar flex flex-col items-center mx-1 w-12 bg-red-500" style="height: 50px;">
<span class="text-white font-bold mt-1">5</span>
<span class="text-white text-xs">[1]</span>
<span class="text-white text-xs mt-1">(Key)</span>
</div>
<div class="array-bar flex flex-col items-center mx-1 w-12 bg-gray-400" style="height: 70px;">
<span class="text-white font-bold mt-1">7</span>
<span class="text-white text-xs">[2]</span>
</div>
<div class="array-bar flex flex-col items-center mx-1 w-12 bg-gray-400" style="height: 40px;">
<span class="text-white font-bold mt-1">3</span>
<span class="text-white text-xs">[3]</span>
</div>
</div>
</div>
</div>
<div class="algorithm-step bg-white border border-gray-200 rounded-lg p-4">
<div class="flex items-center mb-2">
<div class="bg-green-500 text-white rounded-full h-8 w-8 flex items-center justify-center mr-3 flex-shrink-0">2</div>
<h3 class="font-bold text-gray-800">Compare with left element</h3>
</div>
<p class="text-gray-600 ml-11 mb-3">
Compare key (5) with element to its left (8). Since 5 < 8, we need to shift 8 to the right.
</p>
<div class="flex justify-center">
<div class="flex items-end h-32">
<div class="array-bar flex flex-col items-center mx-1 w-12 bg-yellow-500" style="height: 80px;">
<span class="text-white font-bold mt-1">8</span>
<span class="text-white text-xs">[0]</span>
<span class="text-white text-xs mt-1">Comparing</span>
</div>
<div class="array-bar flex flex-col items-center mx-1 w-12 bg-red-500" style="height: 50px;">
<span class="text-white font-bold mt-1">5</span>
<span class="text-white text-xs">[1]</span>
<span class="text-white text-xs mt-1">(Key)</span>
</div>
<div class="array-bar flex flex-col items-center mx-1 w-12 bg-gray-400" style="height: 70px;">
<span class="text-white font-bold mt-1">7</span>
<span class="text-white text-xs">[2]</span>
</div>
<div class="array-bar flex flex-col items-center mx-1 w-12 bg-gray-400" style="height: 40px;">
<span class="text-white font-bold mt-1">3</span>
<span class="text-white text-xs">[3]</span>
</div>
</div>
</div>
</div>
<div class="algorithm-step bg-white border border-gray-200 rounded-lg p-4">
<div class="flex items-center mb-2">
<div class="bg-green-500 text-white rounded-full h-8 w-8 flex items-center justify-center mr-3 flex-shrink-0">3</div>
<h3 class="font-bold text-gray-800">Shift and Insert</h3>
</div>
<p class="text-gray-600 ml-11 mb-3">
Shift 8 to the right and insert key (5) in its correct position.
</p>
<div class="flex justify-center">
<div class="flex items-end h-32">
<div class="array-bar flex flex-col items-center mx-1 w-12 bg-green-500" style="height: 50px;">
<span class="text-white font-bold mt-1">5</span>
<span class="text-white text-xs">[0]</span>
<span class="text-white text-xs mt-1">Sorted</span>
</div>
<div class="array-bar flex flex-col items-center mx-1 w-12 bg-green-500" style="height: 80px;">
<span class="text-white font-bold mt-1">8</span>
<span class="text-white text-xs">[1]</span>
<span class="text-white text-xs mt-1">Sorted</span>
</div>
<div class="array-bar flex flex-col items-center mx-1 w-12 bg-gray-400" style="height: 70px;">
<span class="text-white font-bold mt-1">7</span>
<span class="text-white text-xs">[2]</span>
</div>
<div class="array-bar flex flex-col items-center mx-1 w-12 bg-gray-400" style="height: 40px;">
<span class="text-white font-bold mt-1">3</span>
<span class="text-white text-xs">[3]</span>
</div>
</div>
</div>
</div>
</div>
<div class="mt-8 bg-blue-50 rounded-lg p-5">
<h3 class="font-bold text-gray-800 flex items-center mb-3">
<i class="fas fa-calculator mr-2 text-blue-600"></i> Complexity Analysis
</h3>
<div class="grid grid-cols-1 md:grid-cols-3 gap-4">
<div class="text-center p-4 bg-white rounded-lg">
<div class="text-lg font-semibold">Best Case</div>
<div class="text-2xl font-bold text-green-600">O(n)</div>
<p class="text-gray-600 mt-2">When array is already sorted</p>
</div>
<div class="text-center p-4 bg-white rounded-lg">
<div class="text-lg font-semibold">Average Case</div>
<div class="text-2xl font-bold text-amber-600">O(n²)</div>
<p class="text-gray-600 mt-2">Typical performance</p>
</div>
<div class="text-center p-4 bg-white rounded-lg">
<div class="text-lg font-semibold">Worst Case</div>
<div class="text-2xl font-bold text-red-600">O(n²)</div>
<p class="text-gray-600 mt-2">Reverse sorted array</p>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Visualization Panel -->
<div>
<div class="bg-white rounded-xl shadow-md overflow-hidden sticky top-8 card-hover">
<div class="p-6 border-b border-gray-200 bg-gradient-to-r from-highlight to-amber-400">
<h2 class="text-2xl font-bold text-white">Interactive Visualization</h2>
</div>
<div class="p-6">
<div id="visualization" class="mb-6">
<div class="flex flex-col items-center justify-center">
<!-- Key Display -->
<div id="key-display" class="mb-6 bg-amber-100 rounded-lg p-4 text-center">
<span class="text-gray-600 font-semibold">Current Key:</span>
<span id="current-key" class="ml-2 bg-red-500 text-white font-bold px-3 py-1 rounded-lg">5</span>
</div>
<!-- Array Visualization -->
<div class="h-64 w-full flex items-end justify-center px-4" id="array-container">
<div class="flex items-end">
<div class="array-bar flex flex-col items-center mx-1 w-12 bg-green-500" style="height: 80px;">
<span class="text-white font-bold mt-1">5</span>
<span class="text-white text-xs">[0]</span>
<span class="text-white text-xs mt-1">Sorted</span>
</div>
<div class="array-bar flex flex-col items-center mx-1 w-12 bg-green-500" style="height: 50px;">
<span class="text-white font-bold mt-1">8</span>
<span class="text-white text-xs">[1]</span>
<span class="text-white text-xs mt-1">Sorted</span>
</div>
<div class="array-bar flex flex-col items-center mx-1 w-12 bg-red-500" style="height: 70px;">
<span class="text-white font-bold mt-1">7</span>
<span class="text-white text-xs">[2]</span>
<span class="text-white text-xs mt-1">(Key)</span>
</div>
<div class="array-bar flex flex-col items-center mx-1 w-12 bg-gray-400" style="height: 40px;">
<span class="text-white font-bold mt-1">3</span>
<span class="text-white text-xs">[3]</span>
</div>
<div class="array-bar flex flex-col items-center mx-1 w-12 bg-gray-400" style="height: 100px;">
<span class="text-white font-bold mt-1">10</span>
<span class="text-white text-xs">[4]</span>
</div>
<div class="array-bar flex flex-col items-center mx-1 w-12 bg-gray-400" style="height: 60px;">
<span class="text-white font-bold mt-1">6</span>
<span class="text-white text-xs">[5]</span>
</div>
</div>
</div>
<!-- Status Information -->
<div id="status-info" class="mt-4 text-center px-4 py-3 bg-blue-100 rounded-lg w-full max-w-md">
<p class="font-medium text-blue-800">Comparing 7 and 8. Shift required.</p>
</div>
</div>
</div>
<!-- Controls -->
<div class="mt-6">
<div class="flex flex-wrap justify-center gap-3">
<button class="px-4 py-2 bg-blue-500 hover:bg-blue-600 text-white rounded-lg flex items-center">
<i class="fas fa-play mr-2"></i> Start
</button>
<button class="px-4 py-2 bg-yellow-500 hover:bg-yellow-600 text-white rounded-lg flex items-center">
<i class="fas fa-step-forward mr-2"></i> Next Step
</button>
<button class="px-4 py-2 bg-green-500 hover:bg-green-600 text-white rounded-lg flex items-center">
<i class="fas fa-random mr-2"></i> New Array
</button>
<button class="px-4 py-2 bg-purple-500 hover:bg-purple-600 text-white rounded-lg flex items-center">
<i class="fas fa-backward mr-2"></i> Reset
</button>
</div>
<div class="mt-6 bg-gray-100 rounded-lg p-4">
<h4 class="font-bold text-gray-800 mb-2 flex items-center">
<i class="fas fa-tachometer-alt mr-2 text-gray-600"></i> Adjust Speed
</h4>
<input type="range" min="1" max="10" value="5" class="w-full h-2 bg-gray-300 rounded-lg appearance-none cursor-pointer">
<div class="flex justify-between mt-1 text-sm text-gray-600">
<span>Slow</span>
<span>Medium</span>
<span>Fast</span>
</div>
</div>
</div>
<div class="mt-8 bg-gray-50 rounded-lg p-5">
<h3 class="font-bold text-gray-800 mb-3 flex items-center">
<i class="fas fa-lightbulb mr-2 text-yellow-500"></i> Key Insight
</h3>
<p class="text-gray-600">
Insertion sort's efficiency comes from its simplicity and adaptability.
While not optimal for large datasets, it excels when the data is nearly sorted
or when processing small datasets with low overhead.
</p>
<div class="mt-4 flex items-center">
<i class="fas fa-code mr-2 text-gray-500"></i>
<span class="text-sm bg-gray-200 px-2 py-1 rounded font-mono">arr[j + 1] = arr[j] // Shift element right</span>
</div>
</div>
</div>
</div>
<!-- Real-world Applications -->
<div class="mt-8 bg-white rounded-xl shadow-md overflow-hidden card-hover">
<div class="p-6 border-b border-gray-200 bg-gradient-to-r from-purple-500 to-indigo-400">
<h2 class="text-2xl font-bold text-white">Real-world Applications</h2>
</div>
<div class="p-6">
<ul class="space-y-4">
<li class="flex items-start">
<div class="bg-indigo-100 p-3 rounded-full mr-4">
<i class="fas fa-gamepad text-indigo-600"></i>
</div>
<div>
<h3 class="font-bold text-gray-800">Game Leaderboards</h3>
<p class="text-gray-600">Online games use insertion sort for player rankings where scores are constantly updated incrementally.</p>
</div>
</li>
<li class="flex items-start">
<div class="bg-indigo-100 p-3 rounded-full mr-4">
<i class="fas fa-database text-indigo-600"></i>
</div>
<div>
<h3 class="font-bold text-gray-800">Database Optimizations</h3>
<p class="text-gray-600">Databases use insertion sort for small datasets or when data is almost sorted.</p>
</div>
</li>
<li class="flex items-start">
<div class="bg-indigo-100 p-3 rounded-full mr-4">
<i class="fas fa-shopping-cart text-indigo-600"></i>
</div>
<div>
<h3 class="font-bold text-gray-800">E-commerce Filtering</h3>
<p class="text-gray-600">E-commerce sites use it for real-time sorting of products when filters change incrementally.</p>
</div>
</li>
<li class="flex items-start">
<div class="bg-indigo-100 p-3 rounded-full mr-4">
<i class="fas fa-network-wired text-indigo-600"></i>
</div>
<div>
<h3 class="font-bold text-gray-800">Network Routing</h3>
<p class="text-gray-600">Network devices sort routing tables efficiently when there are minor changes.</p>
</div>
</li>
</ul>
</div>
</div>
</div>
</div>
<!-- Footer -->
<footer class="mt-12 pt-6 border-t border-gray-200 text-center text-gray-600">
<p>Interactive Insertion Sort Visualizer & Algorithm Explanation</p>
<p class="mt-2">Explore how insertion sort efficiently builds a sorted array one element at a time.</p>
</footer>
</div>
<script>
// Sample array for the visualization
const sampleArray = [8, 5, 7, 3, 10, 6];
// State for the visualization
const state = {
array: [...sampleArray],
currentKeyIndex: 2, // Index of the current key element
comparingIndex: 1, // Index being compared with the key
isPlaying: false,
speed: 5,
sortedIndices: [0, 1]
};
// Initialize the visualization (would be enhanced in a complete implementation)
function initVisualization() {
// In a full implementation, this would handle rendering the array
// based on the current state, and updating the UI accordingly
console.log("Visualization initialized");
}
// Event listeners for buttons (simplified for this example)
document.addEventListener('DOMContentLoaded', function() {
initVisualization();
document.querySelectorAll('button').forEach(button => {
button.addEventListener('click', function() {
this.classList.add('animate-pulse');
setTimeout(() => {
this.classList.remove('animate-pulse');
}, 300);
});
});
// For the algorithm explanation steps
document.querySelectorAll('.algorithm-step').forEach(step => {
step.addEventListener('click', function() {
document.querySelectorAll('.algorithm-step').forEach(s => {
s.classList.remove('border-blue-500', 'bg-blue-50');
});
this.classList.add('border-blue-500', 'bg-blue-50');
});
});
});
</script>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=aigpt4robo/awesomeinsertionsortvisulizer" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html> |