JavaScript ~ 排序算法(选择排序)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link type="text/css" rel="stylesheet" href="style/flex.css"> <style> body{ } #sort{ width: 1200px; height: auto; background: #FFFFFF; color: #606060; position: relative; } #title{ width: 100%; height: 60px; background: #9dc4d4; color: #FFFFFF; font-size: 28px; } #nav{ width: 100%; height: 60px; background: #ff6600; } .nav_item{ width: 70px; height: 50px; line-height: 50px; cursor: pointer; } #show-sort{ background: #EEEEEE; width: 1200px; height: 300px; } .sort-item{ margin-left: 15px; width: 30px; } #reset{ position: fixed; top: 30px; right: 30px; height: 42px; line-height: 42px; width: 120px; background: #2492ff; border-radius: 5px; text-align: center; color: #FFFFFF; cursor: pointer; } </style> </head> <body class="r-n-c-fs"> <div id="sort" class="c-n-fs-fs"> <div id="reset" onclick="SortCount(20)">产生随机数据</div> <div id="title" class="r-n-c-c"> 排序算法 </div> <div id="nav" class="r-n-c-c"> <div class="nav_item" onclick="SortOne()">冒泡排序</div> <div class="nav_item" onclick="SortTwo()">选择排序</div> <div class="nav_item">快速排序</div> <div class="nav_item">希尔排序</div> <div class="nav_item">归并排序</div> <div class="nav_item">基数排序</div> <div class="nav_item">堆排序</div> </div> <div id="show-sort" class="r-n-fs-fe"></div> </div> </body> <script> let countList = []; let docFrag = document.createDocumentFragment(); let domEle = document.getElementById("show-sort"); domEle.style.width = "1200px"; let renderWidth = domEle.style.width; SortCount(25); /** * 冒泡排序是一种简单的排序算法。它重复地走访过要排序的数列,一次比较两个元素, * 如果它们的顺序错误就把它们交换过来。走访数列的工作是重复地进行直到没有再需要交换, * 也就是说该数列已经排序完成。这个算法的名字由来是因为越小的元素会经由交换慢慢“浮”到数列的顶端。 * */ function SortOne(){ /** * 遍历目标元素的所有子元素,将所有子元素移动到createDocumentFragment对象中 * */ for (let e of Array.from(domEle.children)){ // console.log(e); docFrag.appendChild(e); } let fragContent = Array.from(docFrag.children); /** * 在createDocumentFragment对象的children属性中取出所有子元素 * 比较相邻的元素。如果第一个比第二个大,就交换它们两个; 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对,这样在最后的元素应该会是最大的数; 针对所有的元素重复以上的步骤,除了最后一个; 重复步骤1~3,直到排序完成。 * */ /* * 循环元素个数减一次(第一轮比较的总次数) * */ for(let i = 0;i < countList.length-1;i++){ /* * 每一轮比较都会将最大的数提升到最后的位置,所以每进行一轮比较,这一轮比较的最大值都会冒泡到最后位置 * 下一轮比较的总次数就不用考虑上一轮比较的最后一个值,所以循环一轮比较次数减一更加优化。 * */ for (let j = 1;j < countList.length-i;j++) { if (countList[j-1]>countList[j]){ [countList[j],countList[j-1]] = [countList[j-1],countList[j]]; [fragContent[j],fragContent[j-1]] = [fragContent[j-1],fragContent[j]] } } } for (let e of fragContent){ domEle.appendChild(e); } console.log(countList); console.log(fragContent); } /** * 选择排序(Selection-sort)是一种简单直观的排序算法。 * 它的工作原理:首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置, * 然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。 * 以此类推,直到所有元素均排序完毕。 * */ function SortTwo() { /** * 遍历目标元素的所有子元素,将所有子元素移动到createDocumentFragment对象中 * */ for (let e of Array.from(domEle.children)){ // console.log(e); docFrag.appendChild(e); } let fragContent = Array.from(docFrag.children); /** * 在createDocumentFragment对象的children属性中取出所有子元素 * 比较相邻的元素。如果第一个比第二个大,就交换它们两个; 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对,这样在最后的元素应该会是最大的数; 针对所有的元素重复以上的步骤,除了最后一个; 重复步骤1~3,直到排序完成。 * */ /* * 循环元素个数减一次(第一轮比较的总次数) * */ let min = 0; console.log(countList); for(let i = 0;i < countList.length-1;i++){ /* * 每一轮比较都会将最小的数提升到最前的位置,所以每进行一轮比较,这一轮比较的最小值都会被选择到最后前位置 * 下一轮比较的总次数就不用考虑上一轮比较的最前一个值,所以循环一轮比较开始基数加一。 * */ for (let j = i+1;j < countList.length;j++) { if (countList[j] < countList[i]) { [countList[i],countList[j]] = [countList[j],countList[i]]; [fragContent[i],fragContent[j]] = [fragContent[j],fragContent[i]]; } } } for (let e of fragContent){ domEle.appendChild(e); } } function selectionSort(arr) { var len = arr.length; var minIndex, temp; for (var i = 0; i < len - 1; i++) { minIndex = i; for (var j = i + 1; j < len; j++) { if (arr[j] < arr[minIndex]) { // 寻找最小的数 minIndex = j; // 将最小数的索引保存 } } temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; } return arr; } function SortCount(numberCount) { let baseItem = document.getElementsByClassName("sort-item"); let baseCount = baseItem.length; let count = baseCount + numberCount; let rWidth = parseInt(renderWidth); let modelWidth = (rWidth-(rWidth/count)*0.3)/count; if (baseCount!==0){ for (let item of baseItem){ item.style.width = modelWidth*0.7+"px"; item.style.marginLeft = modelWidth*0.3+"px"; } } for (let i = 0;i<numberCount;i++){ let tempEle = document.createElement("div"); let size = numberCreate(300); tempEle.className = "sort-item"; tempEle.dataset.size = size+""; tempEle.style.width = modelWidth*0.7+"px"; tempEle.style.height = size+"px"; tempEle.style.marginLeft = modelWidth*0.3+"px"; tempEle.style.backgroundColor = colorCreate(); domEle.appendChild(tempEle); countList.push(size); } } function colorCreate() { let color = ""; let str_item = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"]; for (let i = 0;i<6;i++){ color += str_item[Math.floor(Math.random()*16)]; } return "#"+color; } function numberCreate(limitTop) { return Math.floor(Math.random() * limitTop); } </script> </html>

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
PHP 使用 OSS上传文件
一、安装阿里云 oss sdk 1、在网站根目录执行下面命令,安装oss sdk。 composer require aliyuncs/oss-sdk-php 安装后,会在 网站根目录/vendor 下找到一个名为 aliyuncs 的文件夹。 注:阿里云 oss - sdk 文档(可不用关注) "官方 oss - sdk 文档" 二、使用 1、获取 OSS AccessKeyId、AccessKeySecret 如果没有这两个数据可以参考 如何获取 OSS AccessKeyId、AccessKeySecret 。 2、简易上传 Html 处理 upload.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>小卜丢个人站 - oss图片上传</title> </head> <body> <form action="oss-image/upload-image.json" meth...
- 下一篇
浅谈JS的闭包
最近正逢过十一,有了大块的时间,可以给自己充充电。于是便开始了《你不知道的JavaScript 上卷》之旅。最开始的几章描述的是JS的相关编译原理,作用域,以及声明提升的相关知识。这些内容虽然很重要,但是不是本文的重点。本文的重点是作用域的闭包,为什么呢?因为到现在为止,对这个概念还是云里雾里,所以在这里做下记录。 闭包的定义 当函数可以记住并访问所在的词法作用域时,就产生了闭包,即使函数在当前词法作用域之外执行。 这么说,是不是还是有点不懂。那么我们可以换个说法:无论通过何种方法将内部函数传递到词法作用域之外,他都会持有对原始定义作用域的引用,无论在何处执行这个函数,都会使用闭包。 这么讲还不是很懂?没关系,那么我们可以看一下菜鸟教程对闭包的讲解: 闭包是一种保护私有变量的机制,在函数执行时形成私有的作用域,保护里面的私有变量不受外界干扰。直观的说就是形成一个不销毁的栈环境。 还是不太懂? 没关系,让我们来看一下mozilla的定义: 闭包是由函数以及创建该函数的词法环境组合而成。这个环境包含了这个闭包创建时所能访问的所有局部变量 因为原生的JavaScript是不支持私有变量的,所...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- Hadoop3单机部署,实现最简伪集群
- CentOS8编译安装MySQL8.0.19
- Docker安装Oracle12C,快速搭建Oracle学习环境
- Eclipse初始化配置,告别卡顿、闪退、编译时间过长
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- CentOS6,CentOS7官方镜像安装Oracle11G