摘要:快速排序快速排序原始數(shù)組二分查找冒泡排序冒泡排序耗時(shí)冒泡排序耗時(shí)改進(jìn)后的冒泡排序耗時(shí)改進(jìn)后的冒泡排序耗時(shí)排序前冒泡排序后改進(jìn)的冒泡排序后選擇排序選擇排序耗時(shí)選擇排序耗時(shí)排序前排序后插入排序插入排序耗時(shí)插入排序耗時(shí)排序前排序后
快速排序
function quickSort(ary, isDesc) { var len = ary.length; if (len < 3) { return ary; } var baseIndex = Math.floor(len / 2), base = ary[baseIndex]; var smallAry = [], largeAry = []; for (var i = len - 1, cur; i > -1; i--) { cur = ary[i]; if (i == baseIndex) { continue; } if (isDesc) { cur < base ? (largeAry[largeAry.length] = cur) : (smallAry[smallAry.length] = cur); } else { cur >= base ? (largeAry[largeAry.length] = cur) : (smallAry[smallAry.length] = cur); } } smallAry[smallAry.length] = base; return quickSort(smallAry, isDesc).concat(quickSort(largeAry, isDesc)); } function halfSearch(ary, num) { var len = ary.length, middle = Math.floor(len / 2), midNum = ary[middle]; if (len == 0) { return null; } else if (num === midNum) { return midNum; } else if (midNum > num ) { return halfSearch(ary.slice(0, middle), num); } else { return halfSearch(ary.slice(middle + 1), num); } } var testAry = [9, 2, 3, 4, 1, 0, 8, 4, 2]; var sortedAry = quickSort(testAry); console.log(sortedAry, "快速排序"); console.log(testAry, "原始數(shù)組"); console.log(halfSearch(sortedAry, 3), "二分查找");冒泡排序
var common = require("./common"); function bubbleSort(ary){ console.time("冒泡排序耗時(shí)"); var len = ary.length; for(var i = 0; i < len; i++){ for(var j = 0; j < len-1-i; j++){ if(ary[j] > ary[j+1]){ var tmp = ary[j]; ary[j] = ary[j+1]; ary[j+1] = tmp; } } } console.timeEnd("冒泡排序耗時(shí)"); return ary; } function bubbleSort2(ary){ console.time("改進(jìn)后的冒泡排序耗時(shí)"); var i = ary.length -1; while(i > 0){ var pos; for(var j = 0; j < i; j++){ if(ary[j] > ary[j+1]){ pos = j; var tmp = ary[j]; ary[j] = ary[j+1]; ary[j+1] = tmp; } i = pos; } } console.timeEnd("改進(jìn)后的冒泡排序耗時(shí)"); return ary; } var ary = common.createAry(200); console.log("排序前", ary.toString()); var result = bubbleSort(ary); console.log(result.toString(), "冒泡排序后"); console.log(tmp.toString()); result = bubbleSort2(tmp); console.log(result.toString(), "改進(jìn)的冒泡排序后");選擇排序
var common = require("./common"); function chooseSort(ary){ console.time("選擇排序耗時(shí)"); var len = ary.length, tmp, minIndex; for(var i = 0; i < len; i++){ minIndex = i; for(j = i+1; j < len - i; j++){ if(ary[j] < ary[minIndex]){ minIndex = j; } } tmp = ary[i]; ary[i] = ary[minIndex]; ary[minIndex] = tmp; } console.timeEnd("選擇排序耗時(shí)"); return ary; } var ary = common.createAry(20); console.log("排序前", ary); var sortedAry = chooseSort(ary); console.log("排序后", sortedAry);插入排序
var common = require("./common"); function insertSort(ary) { console.time("插入排序耗時(shí)"); var len = ary.length; for (var i = 1; i < len; i++) { var key = ary[i], j = i - 1; while (j >= 0 && key < ary[j]) { ary[j + 1] = ary[j]; j--; } ary[j + 1] = key; } console.timeEnd("插入排序耗時(shí)"); return ary; } var ary = common.createAry(20); console.log("排序前", ary); var sortedAry = insertSort(ary); console.log("排序后", sortedAry);common.js
exports.createAry = function(number){ var ary = []; for(let i = 0; i < number; i++){ ary.push(Math.floor(Math.random()*100)); } return ary; }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/91838.html
摘要:所以平均來說,插入排序的時(shí)間復(fù)雜度是。顯然,次方級(jí)別的時(shí)間復(fù)雜度代表著插入排序不適合數(shù)據(jù)特別多的情況,一般來說插入排序適合小數(shù)據(jù)量的排序。 更新了幾個(gè)知識(shí)點(diǎn)~歡迎一起交流呀~ 一、排序 冒泡排序(復(fù)雜度O(n^2)) //冒泡排序 function bubbleSort(arr) { for(var i = 0, len = arr.length; i < len - 1; +...
摘要:匯總數(shù)據(jù)結(jié)構(gòu)算法篇算法與數(shù)據(jù)結(jié)構(gòu)我接觸過的前端數(shù)據(jù)結(jié)構(gòu)與算法十大經(jīng)典排序算法總結(jié)描述 showImg(https://segmentfault.com/img/bVbn0N2?w=458&h=275); 2018匯總數(shù)據(jù)結(jié)構(gòu)算法篇JavaScript 算法與數(shù)據(jù)結(jié)構(gòu)我接觸過的前端數(shù)據(jù)結(jié)構(gòu)與算法十大經(jīng)典排序算法總結(jié)(JavaScript描述)
JavaScript在創(chuàng)建變量(數(shù)組、字符串、對(duì)象等)是自動(dòng)進(jìn)行了分配內(nèi)存,而且當(dāng)它沒有被使用的狀態(tài)下,會(huì)自動(dòng)的釋放分配的內(nèi)容;其實(shí)這樣基層語言,如C語言,他們提供了內(nèi)存管理的接口,比如malloc()用于分配所需的內(nèi)存空間、free()釋放之前所分配的內(nèi)存空間?! ♂尫艃?nèi)存的過程稱為垃圾回收,例如avaScript這類高級(jí)語言可以提供了內(nèi)存自動(dòng)分配和自動(dòng)回收,其實(shí)這個(gè)自動(dòng)儲(chǔ)存不會(huì)占用太多空間...
摘要:新生代的對(duì)象為存活時(shí)間較短的對(duì)象,老生代中的對(duì)象為存活時(shí)間較長(zhǎng)或常駐內(nèi)存的對(duì)象。分別對(duì)新生代和老生代使用不同的垃圾回收算法來提升垃圾回收的效率。如果指向老生代我們就不必考慮它了。 這篇文章的所有內(nèi)容均來自 樸靈的《深入淺出Node.js》及A tour of V8:Garbage Collection,后者還有中文翻譯版V8 之旅: 垃圾回收器,我在這里只是做了個(gè)記錄和結(jié)合 垃圾回收...
閱讀 1195·2021-10-11 10:59
閱讀 1975·2021-09-29 09:44
閱讀 863·2021-09-01 10:32
閱讀 1437·2019-08-30 14:21
閱讀 1880·2019-08-29 15:39
閱讀 2986·2019-08-29 13:45
閱讀 3542·2019-08-29 13:27
閱讀 2015·2019-08-29 12:27