摘要:冒泡排序可谓是最经典的排序算法了,它是基于比较的排序算法,其优点是实现简单,排序数量较小时性能较好。也可以实现大数放在前面,小数放在后面,如果前面的数据比后面的小,就交换两个的位置。要实现上述规则需要用到两层循环。
冒泡排序可谓是最经典的排序算法了,它是基于比较的排序算法,其优点是实现简单,排序数量较小时性能较好。
算法原理相邻的数据进行两两比较,小数放在前面,大数放在后面,如果前面的数据比后面的数据大,就交换这两个数的位置。也可以实现大数放在前面,小数放在后面,如果前面的数据比后面的小,就交换两个的位置。要实现上述规则需要用到两层for循环。
冒泡实现数组从小到大排序
var arr=[2,7,1,8,6,4,3,5,9]; function mintomax(par){ var length=par.length; for(var i=0; ipar[j+1]){ var temp=par[j+1]; par[j+1]=par[j]; par[j]=temp; } } } return par; } console.log(mintomax(arr));
冒泡实现数组从大到小排序
var arr=[2,7,1,8,6,4,3,5,9]; function maxtomin(par){ var length=par.length; for(var i=0; i用es6结构赋值的方式重写上面代码
var arr=[2,7,1,8,6,4,3,5,9]; function mintomax(par){ var length=par.length; for(var i=0; ipar[j+1]){ [par[j],par[j+1]]=[par[j+1],par[j]] } } } return par; } console.log(mintomax(arr)); function maxtomin(par){ var length=par.length; for(var i=0; i 按照父子平铺顺序排序
var data = [{ ZDXM_STDCODE: "100101", ZDXM_STDNAME: "", FINA_YYSR: "", FINA_PGZHSY: "", FINA_SJZHSY: "", FINA_PGZHSYL: "", FINA_SJZHSYL: "", FINA_ZHSYLCE: "", FINA_SRJJL: "", FINA_JSSKL: "", FINA_HTE: "", GLZDXM: "1001", }, { ZDXM_STDCODE: "1001", ZDXM_STDNAME: "", FINA_YYSR: "", FINA_PGZHSY: "", FINA_SJZHSY: "", FINA_PGZHSYL: "", FINA_SJZHSYL: "", FINA_ZHSYLCE: "", FINA_SRJJL: "", FINA_JSSKL: "", FINA_HTE: "", GLZDXM: "", }, { ZDXM_STDCODE: "100102", ZDXM_STDNAME: "", FINA_YYSR: "", FINA_PGZHSY: "", FINA_SJZHSY: "", FINA_PGZHSYL: "", FINA_SJZHSYL: "", FINA_ZHSYLCE: "", FINA_SRJJL: "", FINA_JSSKL: "", FINA_HTE: "", GLZDXM: "1001", }, { ZDXM_STDCODE: "100201", ZDXM_STDNAME: "", FINA_YYSR: "", FINA_PGZHSY: "", FINA_SJZHSY: "", FINA_PGZHSYL: "", FINA_SJZHSYL: "", FINA_ZHSYLCE: "", FINA_SRJJL: "", FINA_JSSKL: "", FINA_HTE: "", GLZDXM: "1002", }, { ZDXM_STDCODE: "1002", ZDXM_STDNAME: "", FINA_YYSR: "", FINA_PGZHSY: "", FINA_SJZHSY: "", FINA_PGZHSYL: "", FINA_SJZHSYL: "", FINA_ZHSYLCE: "", FINA_SRJJL: "", FINA_JSSKL: "", FINA_HTE: "", GLZDXM: "", }, { ZDXM_STDCODE: "100202", ZDXM_STDNAME: "", FINA_YYSR: "", FINA_PGZHSY: "", FINA_SJZHSY: "", FINA_PGZHSYL: "", FINA_SJZHSYL: "", FINA_ZHSYLCE: "", FINA_SRJJL: "", FINA_JSSKL: "", FINA_HTE: "", GLZDXM: "1002", }, ] function datatotree(par) { var length = par.length; for (var i = 0; i < length-1; i++) { for (var j = 0; j < length - i - 1; j++) { var str1=par[j].GLZDXM+par[j].ZDXM_STDCODE; var str2=par[j+1].GLZDXM+par[j+1].ZDXM_STDCODE; if(par[j].GLZDXM==null){ str1=par[j].ZDXM_STDCODE; } if(data[j+1].GLZDXM==null){ str2=par[j+1].ZDXM_STDCODE; } if (str1 > str2) { var temp = par[j + 1]; par[j + 1] = par[j] par[j] = temp; } } } return par; } var data = datatotree(data) console.log(data);按照es6结构赋值重写上面代码
var data = [{ ZDXM_STDCODE: "100101", ZDXM_STDNAME: "", FINA_YYSR: "", FINA_PGZHSY: "", FINA_SJZHSY: "", FINA_PGZHSYL: "", FINA_SJZHSYL: "", FINA_ZHSYLCE: "", FINA_SRJJL: "", FINA_JSSKL: "", FINA_HTE: "", GLZDXM: "1001", }, { ZDXM_STDCODE: "1001", ZDXM_STDNAME: "", FINA_YYSR: "", FINA_PGZHSY: "", FINA_SJZHSY: "", FINA_PGZHSYL: "", FINA_SJZHSYL: "", FINA_ZHSYLCE: "", FINA_SRJJL: "", FINA_JSSKL: "", FINA_HTE: "", GLZDXM: "", }, { ZDXM_STDCODE: "100102", ZDXM_STDNAME: "", FINA_YYSR: "", FINA_PGZHSY: "", FINA_SJZHSY: "", FINA_PGZHSYL: "", FINA_SJZHSYL: "", FINA_ZHSYLCE: "", FINA_SRJJL: "", FINA_JSSKL: "", FINA_HTE: "", GLZDXM: "1001", }, { ZDXM_STDCODE: "100201", ZDXM_STDNAME: "", FINA_YYSR: "", FINA_PGZHSY: "", FINA_SJZHSY: "", FINA_PGZHSYL: "", FINA_SJZHSYL: "", FINA_ZHSYLCE: "", FINA_SRJJL: "", FINA_JSSKL: "", FINA_HTE: "", GLZDXM: "1002", }, { ZDXM_STDCODE: "1002", ZDXM_STDNAME: "", FINA_YYSR: "", FINA_PGZHSY: "", FINA_SJZHSY: "", FINA_PGZHSYL: "", FINA_SJZHSYL: "", FINA_ZHSYLCE: "", FINA_SRJJL: "", FINA_JSSKL: "", FINA_HTE: "", GLZDXM: "", }, { ZDXM_STDCODE: "100202", ZDXM_STDNAME: "", FINA_YYSR: "", FINA_PGZHSY: "", FINA_SJZHSY: "", FINA_PGZHSYL: "", FINA_SJZHSYL: "", FINA_ZHSYLCE: "", FINA_SRJJL: "", FINA_JSSKL: "", FINA_HTE: "", GLZDXM: "1002", }, ] function datatotree(par) { var length = par.length; for (var i = 0; i < length-1; i++) { for (var j = 0; j < length - i - 1; j++) { var str1=par[j].GLZDXM+par[j].ZDXM_STDCODE; var str2=par[j+1].GLZDXM+par[j+1].ZDXM_STDCODE; if(par[j].GLZDXM==null){ str1=par[j].ZDXM_STDCODE; } if(data[j+1].GLZDXM==null){ str2=par[j+1].ZDXM_STDCODE; } if (str1 > str2) { [par[j],par[j+1]]=[par[j+1],par[j]]; } } } return par; } var data = datatotree(data) console.log(data);上面代码排序之后结果
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/108155.html
摘要:之所以把冒泡排序选择排序插入排序放在一起比较,是因为它们的平均时间复杂度都为。其中,冒泡排序就是原地排序算法。所以冒泡排序是稳定的排序算法。选择排序思路选择排序算法的实现思路有点类似插入排序,也分已排序区间和未排序区间。 showImg(https://segmentfault.com/img/bVbuvnj?w=900&h=250); 1. 前言 算法为王。 想学好前端,先练好内功,...
摘要:说明上次写了实现冒泡排序,只是简单的说了冒泡排序算法是什么,怎么实现,这次来实现将冒泡排序的过程展现出来。总结上面的两个版本的思路基本一样,用一句话概括就是,记录冒泡排序所有的改变,将这些改变一步一步的显示出来。 说明 上次写了 JavaScript实现冒泡排序 ,只是简单的说了冒泡排序算法是什么,怎么实现,这次来实现将冒泡排序的过程展现出来。 解释 先来个简单的版本,看效果图 sh...
说明 对数组进行 冒泡排序 算是比较简单的,冒泡排序也是容易理解的一种排序算法了,在面试的时候,很可能就会问到。 实现原理 数组中有 n 个数,比较每相邻两个数,如果前者大于后者,就把两个数交换位置;这样一来,第一轮就可以选出一个最大的数放在最后面;那么经过 n-1(数组的 length - 1) 轮,就完成了所有数的排序。 showImg(https://segmentfault.com/img...
摘要:实现快速排序介绍通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。 冒泡排序 介绍 重复遍历要排序的元素列,依次比较两个相邻的元素,前一个元素若比后一个元素大则互换位置。以升序排序为例,最大的元素会在第一次遍历后冒泡到数组的末端。假如数组...
摘要:不过说到排序,最容易想到的就是冒泡排序,选择排序,插入排序了。一是影响太小,而是我们人的效率问题,一分钟能从头写个冒泡选择插入的排序方法,而换成是归并排序呢原文发表在我的博客排序,不只是冒泡,欢迎访问 非常非常推荐大家去读一本gitBook上的书 - 十大经典排序算法 : https://sort.hust.cc/ , 本文的动图和演示代码均是这里面的。 做编程,排序是个必然的需求。前...
摘要:快速排序快速排序原始数组二分查找冒泡排序冒泡排序耗时冒泡排序耗时改进后的冒泡排序耗时改进后的冒泡排序耗时排序前冒泡排序后改进的冒泡排序后选择排序选择排序耗时选择排序耗时排序前排序后插入排序插入排序耗时插入排序耗时排序前排序后 快速排序 function quickSort(ary, isDesc) { var len = ary.length; if (len < 3)...
阅读 1879·2021-11-25 09:43
阅读 1388·2021-11-22 14:56
阅读 3257·2021-11-22 09:34
阅读 1979·2021-11-15 11:37
阅读 2211·2021-09-01 10:46
阅读 1370·2019-08-30 15:44
阅读 2278·2019-08-30 13:15
阅读 2375·2019-08-29 13:07