最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當(dāng)前位置: 首頁 - 科技 - 知識(shí)百科 - 正文

Javascript實(shí)現(xiàn)的常用算法(如冒泡、快速、鴿巢、奇偶等)_javascript技巧

來源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 21:22:17
文檔

Javascript實(shí)現(xiàn)的常用算法(如冒泡、快速、鴿巢、奇偶等)_javascript技巧

Javascript實(shí)現(xiàn)的常用算法(如冒泡、快速、鴿巢、奇偶等)_javascript技巧: 代碼如下:l = [6, 2, 4, 1, 98, 34, 5, 9, 23, 8, 10, 32] //冒泡排序/*function bubbleSort(arr) { newarr = arr.slice() if (newarr.length for (var i = 0; i for (var j = 0; j if (newarr[
推薦度:
導(dǎo)讀Javascript實(shí)現(xiàn)的常用算法(如冒泡、快速、鴿巢、奇偶等)_javascript技巧: 代碼如下:l = [6, 2, 4, 1, 98, 34, 5, 9, 23, 8, 10, 32] //冒泡排序/*function bubbleSort(arr) { newarr = arr.slice() if (newarr.length for (var i = 0; i for (var j = 0; j if (newarr[

代碼如下:
l = [6, 2, 4, 1, 98, 34, 5, 9, 23, 8, 10, 32]


//冒泡排序
/*function bubbleSort(arr) {
newarr = arr.slice()
if (newarr.length <= 2) return newarr
for (var i = 0; i < newarr.length - 1; i++) {
for (var j = 0; j < newarr.length - i - 1; j++) {
if (newarr[j] > newarr[j + 1]) {
newarr[j] = [newarr[j + 1], newarr[j + 1] = newarr[j]][0]

}
}
}
return newarr
}

console.log(l)
result = bubbleSort(l)
console.log(result)*/


//快速排序
/*function quickSort(arr) {
if (arr.length <= 1) return arr;
var left = [],
right = [],
middle = [];
var mid = arr[Math.floor(arr.length / 2)]
for (var i = 0; i < arr.length; i++) {
if (arr[i] < mid) {
left.push(arr[i]);
} else if (arr[i] > mid) {
right.push(arr[i]);
} else {
middle.push(mid);
}
}
return [].concat(quickSort(left), middle, quickSort(right))

}

var results = quickSort(l)
console.log(results)


*/

//選擇排序
/*function selectSort(arr) {
var min = 9999,
minindex = 0;
var left = [],
right = arr.slice();
if (arr.length <= 1) return arr;
for (var i = 0; i < arr.length; i++) {
if (arr[i] <= min) {
min = arr[i];
minindex = i;
}
}
left.push(min);
right.splice(minindex, 1);
return [].concat(left, selectSort(right))
}


var results = selectSort(l)
console.log(results)*/


//插入排序
/*function insertSort(arr) {
var tmp;
for (var i = 1; i < arr.length; i++) {
tmp = arr[i];
for (var j = i; j > 0; j--) {
if (arr[j - 1] > tmp) {
arr[j] = arr[j - 1];
} else {
break;
}
}
arr[j] = tmp;
}
return arr;
}

var results = insertSort(l)
console.log(results)*/

//木桶排序
/*function bucketSort(arr){
var bucket = [];
var newarr = [];
for (var i = 0; i < arr.length; i++) {
bucket[arr[i]] = arr[i];
}
for (var i = 0; i < bucket.length; i++) {
if (bucket[i] !== undefined){
newarr.push(bucket[i])
}
}
return newarr;
}

var results = bucketSort(l)
console.log(results)*/

//鴿巢排序,非負(fù)整數(shù)
/*function pigeonholeSort(arr){
var tempArr = [];
for(var i=0,l=arr.length;i tempArr[arr[i]] = (tempArr[arr[i]]+1)||1 ;
}

var result = [],count;
for(var k=0;k count = tempArr[k];
if(count){
for(var i=0;i result.push(k);
}
}
}

return result;
}

var results = pigeonholeSort(l)
console.log(results)*/

//歸并排序
/*function mergeSort(arr) {
if (arr.length <= 1) return arr;
var mid = Math.floor(arr.length / 2);
var left = MergeSort(arr.slice(0, mid));
var right = MergeSort(arr.slice(mid));
var result = [];

while (left.length && right.length) {
if (left[left.length - 1] <= right[0]) {
result = result.concat(left);
left = [];
} else if (right[right.length - 1] < left[0]) {
result = result.concat(right);
right = [];
} else {
if (right[0] < left[0]) {
result.push(right.shift());
} else {
result.push(left.shift());
}
}
}
result = result.concat(left, right);

return result;
}*/

/*function mergeSort(arr) {
var merge = function(left, right) {
var result = [];
while (left.length > 0 && right.length > 0) {
if (left[0] < right[0]) {
result.push(left.shift());
} else {
result.push(right.shift());
}
}
return result.concat(left).concat(right);
}

if (arr.length == 1) return arr;
var middle = Math.floor(arr.length / 2),
left = arr.slice(0, middle),
right = arr.slice(middle);
return merge(mergeSort(left), mergeSort(right));
}

var results = mergeSort(l)
console.log(results)

*/


//堆排序

/*function heapSort(arr) {
var findRoot = function(arr, p, length) {
p = p || 0;
length = length || arr.length;
var self = arguments.callee;
var l = p * 2 + 1;
var r = (p + 1) * 2;
var left, right;

if (l < length) left = self(arr, l, length);
if (r < length) right = self(arr, r, length);
if (left > arr[p]) arr[p] = [left, arr[l] = arr[p]][0];
if (right > arr[p]) arr[p] = [right, arr[r] = arr[p]][0];

return arr[p];
};


for (var i = arr.length; i > 0; i--) {
findRoot(arr, 0, i);
arr[i - 1] = [arr[0], arr[0] = arr[i - 1]][0];
}
return arr;
}

var results = heapSort(l)
console.log(results)*/

//奇偶排列

/*function oddEvenSort(arr) {
var swaped = true,
k = 0;
while (swaped) {
if (k > 0) swaped = false;

for (var i = k; i < arr.length - 1; i += 2) {
if (arr[i]>arr[i+1]) {
arr[i] = [ arr[i+1], arr[i+1]=arr[i] ][0];
swaped = true;
}
}
k = [1, 0][k]
}
return arr;
}

var results = oddEvenSort(l)
console.log(results)*/

function oddEvenSort(arr) {
var swaped = true;
while (swaped) {
swaped = false;
for (var i = 0; i < arr.length; i += 2) {
if (arr[i] > arr[i + 1]) {
arr[i] = [arr[i + 1], arr[i + 1] = arr[i]][0];
swaped = true;
}
}
for (var i = 1; i < arr.length; i += 2) {
if (arr[i] > arr[i + 1]) {
arr[i] = [arr[i + 1], arr[i + 1] = arr[i]][0];
swaped = true;
}
}
}
return arr;
}

var results = oddEvenSort(l)
console.log(results)

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

Javascript實(shí)現(xiàn)的常用算法(如冒泡、快速、鴿巢、奇偶等)_javascript技巧

Javascript實(shí)現(xiàn)的常用算法(如冒泡、快速、鴿巢、奇偶等)_javascript技巧: 代碼如下:l = [6, 2, 4, 1, 98, 34, 5, 9, 23, 8, 10, 32] //冒泡排序/*function bubbleSort(arr) { newarr = arr.slice() if (newarr.length for (var i = 0; i for (var j = 0; j if (newarr[
推薦度:
標(biāo)簽: 快速 實(shí)現(xiàn) 冒泡
  • 熱門焦點(diǎn)

最新推薦

猜你喜歡

熱門推薦

專題
Top