希尔排序动画

 1 <!doctype html>
 2 <html lang="en">
 3 <head>
 4   <meta charset="UTF-8">
 5   <meta name="viewport"
 6         content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
 7   <meta http-equiv="X-UA-Compatible" content="ie=edge">
 8   <title>Document</title>
 9   <style>
10     #root {
11       display: flex;
12       align-items: baseline;
13       position: relative;
14       margin: 0 auto;
15     }
16     #root > div {
17       flex: 1;
18       flex-wrap: nowrap;
19       min-width: 20px;
20       border-radius: 3px;
21       border: 1px solid gray;
22       position: absolute;
23       bottom: -600px;
24       cursor: pointer;
25       text-align: center;
26       transition: all .8s;
27     }
28     .active {
29       background-color: orange;
30     }
31     .moving {
32       background-color: #4fbef3;
33     }
34     .default {
35       background-color: #bbbbbb;
36     }
37   </style>
38 </head>
39 <body>
40 <div id="root"></div>
41 <script>
42   var baseArr = [7,32,18,12,8,33,25,22,13,14,11,6,19,3,10,49,31,48,21,26,37,51,15,36,41];
43   // 绘制棒状图
44   function draw(root, arr) {
45     let fragment = document.createDocumentFragment();
46     arr.map((item, index) => {
47       let dom = document.createElement('div');
48       dom.style.height = item * 10 + 'px';
49       dom.style.left = (index * 25) + 'px';
50       dom.setAttribute('id', index);
51       dom.setAttribute('class', 'default');
52       dom.innerText = item;
53       fragment.appendChild(dom)
54     });
55     root.innerHTML = '';
56     root.appendChild(fragment);
57   }
58   function myShell(arr) {
59     let N = arr.length;
60     let h = 1;
61     while (h < N / 3) {
62       h = h * 3 + 1
63     }
64     let time = 1;
65     while (h >= 1) {
66       for (let i = h; i < N; i++) {
67         for (let j = i; j >= h && arr[j] < arr[j - h]; j -= h) {
68           (function(h,j) {
69             setTimeout(() => {
70               let prvDom = document.getElementById(`${j - h}`);
71               let nextDom = document.getElementById(`${j}`);
72               prvDom.setAttribute('class', 'moving');
73               nextDom.setAttribute('class', 'moving');
74               [nextDom.style.left, prvDom.style.left] = [prvDom.style.left, nextDom.style.left];
75               setTimeout(() => {
76                 prvDom.setAttribute('class', 'default');
77                 nextDom.setAttribute('class', 'default');
78               }, 800);
79               prvDom.setAttribute('id', `${j}`);
80               nextDom.setAttribute(`id`,`${j - h}`);
81             }, 1000 * time);
82           })(h,j);
83           [arr[j], arr[ j - h ]] = [arr[j - h], arr[j]];
84           time++;
85         }
86       }
87       h = Math.floor(h / 3);
88     }
89     console.log(arr)
90   }
91   const rootDom = document.getElementById('root');
92   draw(rootDom, baseArr);
93   setTimeout(() => {
94     myShell(baseArr);
95   }, 1000)
96 </script>
97 </body>
98 </html>

最近看了看基本的排序算法,简单的实现一个了希尔排序动画。

希尔排序是插入排序的一种优化排序。插入排序适用于小规模数组或部分有序数组的排序,当需要插入的元素距离很远的时候,那么就会需要移动很多步。希尔排序的思想是使指定间距的元素是有序的,这样移动的时候元素基本就不会出现一步一步从头移动到尾的情况。

本人技术有限,哪里有问题欢迎指正

转载于:https://www.cnblogs.com/SharkChilli/p/10860298.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值