首页 > 代码库 > 8大排序算法总结 JS 实现
8大排序算法总结 JS 实现
//bubble sort
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | function bubbleSort(arr,comp){ for (var i = 0 ;i < arr.length; i++){ for (var j = 0 ; j < arr.length - i - 1 ; j++){ if (comp(arr[j],arr[j+ 1 ])){ exch(arr,j,j+ 1 ); } } } } function exch(a,i,j){ var tmp = a[i]; a[i] = a[j]; a[j] = tmp; } var input = new Array( 5 , 1 , 4 , 2 , 3 ); bubbleSort(input,function(a,b){ return a > b;}); console.log(input); input = new Array( 5 , 1 , 4 , 2 , 3 ); bubbleSort(input,function(a,b){ return a < b;}); console.log(input); |
//selection sort
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | function selectionSort(arr,comp){ for (var i = 0 ;i < arr.length ; i++){ for (var j = i;j < arr.length ; j++){ if (comp(arr[i],arr[j])) { exch(arr,i,j); } } } } function exch(a,i,j){ var t = a[i]; a[i] = a[j]; a[j] = t; } var input = new Array( 5 , 1 , 4 , 2 , 3 ); selectionSort(input,function(a,b){ return a > b;}); console.log(input); input = new Array( 5 , 1 , 4 , 2 , 3 ); bubbleSort(input,function(a,b){ return a < b;}); console.log(input); |
//insert sort
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | function insertSort(arr,comp){ var result = new Array(); for (;arr.length > 0 ;){ var inserted = false ; for (var j = 0 ;j < result.length; j++){ if (comp(result[j],arr[ 0 ])) { insert(result,j,arr[ 0 ]); inserted = true ; break ;} } if (!inserted){insert(result,result.length,arr[ 0 ]);} arr.splice( 0 , 1 ); } return result; } function insert(arr,i,v){ arr.splice(i, 0 ,v); } var input = new Array( 5 , 1 , 4 , 2 , 3 ); var ret = insertSort(input,function(a,b){ return a > b;}); console.log(ret); var input = new Array( 5 , 1 , 4 , 2 , 3 ); ret = insertSort(input,function(a,b){ return a < b;}); console.log(ret); |
//shell sort
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | function shellSort (a,comp) { for (var h = a.length; h = parseInt(h / 2 );) { for (var i = h; i < a.length; i++) { var k = a[i]; for (var j = i; j >= h && comp(k, a[j - h]); j -= h) a[j] = a[j - h]; a[j] = k; } } return a; } var arr = new Array( 7 , 9 , 2 , 5 , 4 , 1 , 3 ); var r = shellSort(arr,function (a,b){ return a > b;}); console.log(r); |
//merge sort
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | function mergeS(arr,comp){ if (arr.length == 1 ){ return arr;} var mid = arr.length / 2 | 0 ; var leftArr = new Array(); var rightArr = new Array(); for (var i = 0 ;i < mid;i ++){ leftArr.push(arr[i]); } for (var j = mid;j < arr.length; j++){ rightArr.push(arr[j]); } console.log( "before : " + leftArr + " | " + rightArr); var leftRet = mergeS(leftArr,comp); var rightRet = mergeS(rightArr,comp); var r = merge(leftRet,rightRet,comp); return r; } function merge(leftArr,rightArr,comp){ var ret = new Array(); var i = j = 0 ; for (;i < leftArr.length && j < rightArr.length; ){ if (comp(leftArr[i],rightArr[j])){ret.push(leftArr[i]); i ++} else {ret.push(rightArr[j]); j ++} } for (;i < leftArr.length;){ ret.push(leftArr[i]);i++; } for (;j< rightArr.length; ){ ret.push(rightArr[j]);j++; } return ret; } var r = mergeS( new Array( 0 , 6 , 5 , 1 , 2 , 4 , 3 , 9 ),function(a,b){ return a > b;}); console.log(r); |
//quick sort
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | function quickS(arr,lo,hi,comp){ if (lo >= hi){ return ;} var stub = arr[lo]; var i = lo + 1 ; var j = hi; for (;i < j ;){ for (;i < j && !comp(stub,arr[j]);j--); for (;i < j && comp(stub,arr[i]);i++); if (i >= j){ break ;} var t = arr[i]; arr[i] = arr[j]; arr[j] = t; j--; i++; } if (comp(arr[lo],arr[i])){ var t = arr[lo]; arr[lo] = arr[i]; arr[i] = t; } quickS(arr,lo,i- 1 ,comp); quickS(arr,i,hi,comp); } var input = new Array( 22 , 3 , 10 , 66 , 15 , 11 , 2 , 4 , 31 , 9 ); quickS(input, 0 , input.length - 1 ,function(a,b){ return a > b;}); console.log(input); |
//heap sort
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | var ret = new Array(); function heapS(arr,comp){ if (arr.length == 0 ){ return ;} var i = arr.length / 2 | 0 ; for (;i >= 0 ; i--){ if (comp(arr[i], arr[i * 2 ])){exch(arr, i, i* 2 );} if (comp(arr[i], arr[i * 2 + 1 ])) {exch(arr, i, i* 2 + 1 );} } ret.push(arr[ 0 ]); arr.splice( 0 , 1 ); heapS(arr,comp); } function exch(arr,i,j){ var t = arr[i]; arr[i] = arr[j]; arr[j] = t; } heapS( new Array( 16 , 22 , 91 , 0 , 51 , 44 , 23 ),function (a,b){ return a > b;}); console.log(ret);
|
8大排序算法总结 JS 实现
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。