摘要:冒泡排序可謂是最經典的排序算法了,它是基于比較的排序算法,其優點是實現簡單,排序數量較小時性能較好。也可以實現大數放在前面,小數放在后面,如果前面的數據比后面的小,就交換兩個的位置。要實現上述規則需要用到兩層循環。
冒泡排序可謂是最經典的排序算法了,它是基于比較的排序算法,其優點是實現簡單,排序數量較小時性能較好。
算法原理相鄰的數據進行兩兩比較,小數放在前面,大數放在后面,如果前面的數據比后面的數據大,就交換這兩個數的位置。也可以實現大數放在前面,小數放在后面,如果前面的數據比后面的小,就交換兩個的位置。要實現上述規則需要用到兩層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);
上面代碼排序之后結果
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/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)...
閱讀 1915·2021-11-25 09:43
閱讀 1415·2021-11-22 14:56
閱讀 3285·2021-11-22 09:34
閱讀 2019·2021-11-15 11:37
閱讀 2269·2021-09-01 10:46
閱讀 1405·2019-08-30 15:44
閱讀 2300·2019-08-30 13:15
閱讀 2402·2019-08-29 13:07