当前位置: 首页 > news >正文

用html、css和js来实现冒泡排序

效果图如下

代码如下

<meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>冒泡排序动画</title><style>body {display: flex;flex-direction: column;justify-content: center;align-items: center;background-color: #1c1c1c;color: white;font-family: Arial, sans-serif;height: 100vh;margin: 0;}h1 {margin-bottom: 100px;}.container {display: flex;justify-content: center;align-items: center;gap: 20px;}.ball {width: 70px;height: 70px;background-color: black;border-radius: 50%;display: flex;justify-content: center;align-items: center;color: white;font-size: 18px;transition: transform 1.5s ease, box-shadow 1.5s ease;}.comparing {box-shadow: 0px 0px 20px 5px yellow;transform: translateY(-20px); /* Move upward */}.swapping {box-shadow: 0px 0px 20px 5px red;transform: translateY(20px); /* Move downward for swap effect */}.sorted {background-color: green;}.description {margin-top: 60px;font-size: 25px;color:yellow;}</style><h1>冒泡排序动画</h1>
<div class="container" id="balls-container"></div>
<div class="description" id="description">Starting Bubble Sort...</div><script>const array = [5, 3, 8, 4, 2, 6];  // Initial arrayconst container = document.getElementById("balls-container");const description = document.getElementById("description");// Function to create the circular balls representing the arrayfunction createBalls() {container.innerHTML = '';array.forEach((value, index) => {const ball = document.createElement("div");ball.classList.add("ball");ball.textContent = value;ball.setAttribute("id", `ball-${index}`);container.appendChild(ball);});}// Function to swap elements in the DOMfunction swapElements(idx1, idx2) {const ball1 = document.getElementById(`ball-${idx1}`);const ball2 = document.getElementById(`ball-${idx2}`);const tempText = ball1.textContent;ball1.textContent = ball2.textContent;ball2.textContent = tempText;}// Bubble Sort Algorithm with Animationasync function bubbleSort() {let n = array.length;for (let i = 0; i < n; i++) {for (let j = 0; j < n - i - 1; j++) {// Highlight comparing elementsconst ball1 = document.getElementById(`ball-${j}`);const ball2 = document.getElementById(`ball-${j + 1}`);ball1.classList.add("comparing");ball2.classList.add("comparing");description.textContent = `比较: ${array[j]} 和 ${array[j + 1]}`;await new Promise(resolve => setTimeout(resolve, 3000));  // Slower animation// Compare and swap if necessaryif (array[j] > array[j + 1]) {description.textContent = `交换: ${array[j]} 和 ${array[j + 1]}`;[array[j], array[j + 1]] = [array[j + 1], array[j]];swapElements(j, j + 1);ball1.classList.add("swapping");ball2.classList.add("swapping");await new Promise(resolve => setTimeout(resolve, 2000));}// Remove comparison and swapping highlightsball1.classList.remove("comparing", "swapping");ball2.classList.remove("comparing", "swapping");}// Mark the last sorted elementdocument.getElementById(`ball-${n - i - 1}`).classList.add("sorted");}description.textContent = "Array is sorted!";}// Initialize and start the animationcreateBalls();bubbleSort();
</script>


http://www.mrgr.cn/news/50178.html

相关文章:

  • FPGA驱动HDMI 初级篇
  • 10月15日 -- 11月15日 ,参与《人工智能导论》学习打卡赢B站大会员
  • 饭局上做到这5点,让你轻松和大家打成一片相谈甚欢!
  • Thread类的基本用用法
  • SpringCloud-OpenFeign-服务接口调用
  • Java数据结构--顺序表
  • nemo-guardrails简单应用
  • 二叉平衡树(AVL树)Java语言实现
  • 家庭事务管理系统|基于java和vue的家庭事务管理系统设计与实现(源码+数据库+文档)
  • Diffusion model原理:李宏毅篇(1)
  • threejs-法线向量
  • 周易解读:推荐教材
  • 【C语言刷力扣】2206.将数组划分成相等数对
  • YOLOv11改进策略【Conv和Transformer】| ACmix 卷积和自注意力的结合,充分发挥两者优势
  • linux------缓冲区与C库的原理
  • cmake模板-支持编译动态/静态文件
  • Dmitri Shuralyov的全职开源之旅
  • c语言中使用(>> )运算替代(/ %)运算实现优化
  • 【MAUI】步进器
  • Avarra:革新企业销售培训的未来