Node.js 排序處理 (3)

基本介紹

教學目標

透過 Node.js 實作資料結構與演算法中的插入排序法。

前置作業

  1. 完成 Node.js 套件安裝與設置。

使用教學

撰寫程式

algorithm.js

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
var data = [];
var number = 500
for (var n = 0; n < number; n++) {
data[n] = Math.floor(Math.random() * (number + 1))
}

var shell_sort_start = new Date().getTime();
var shell_sort_result = shellSort(data.slice());
var shell_sort_stop = new Date().getTime();
var shell_sort_elapsed = shell_sort_stop - shell_sort_start;

console.log("希爾排序法");
console.log("原始資料: " + data );
console.log("排序結果: " + shell_sort_result );
console.log("花費時間: " + shell_sort_elapsed + " 毫秒" );

function shellSort(data) {
var gap = data.length / 2;
while (gap >= 1) {
for (var i = gap; i < data.length; i++) {
for (var j = i; j >= gap && data[j] < data[j-gap]; j -= gap) {
var temp = data[j];
data[j] = data[j-gap];
data[j-gap] = temp;
}
}
gap = parseInt(gap / 2);
}
return data;
}

執行程式

1
$ node algorithm.js

執行結果

1
2
3
4
希爾排序法
原始資料: 333,61,75,320,193,500,161,197,459,318,43,145,367,57,380,332,5,69,53,143,340,332,4,96,313,12,204,148,490,354,177,292,335,449,87,412,380,291,474,167,431,76,388,349,17,481,52,216,272,486,290,178,262,383,495,38,76,389,232,486,249,156,346,172,500,63,180,85,122,370,138,59,115,243,76,100,366,470,123,182,153,322,58,66,149,238,363,118,361,48,44,49,128,192,400,156,460,406,333,232,495,5,197,436,472,321,104,317,268,207,378,97,137,322,298,207,242,258,229,179,143,61,263,137,127,275,90,426,374,18,43,33,444,121,264,119,313,322,334,194,435,328,369,230,54,345,40,297,260,314,461,30,26,417,235,398,18,378,10,282,225,143,450,239,170,135,47,483,163,117,336,392,194,370,288,259,357,36,253,266,95,22,403,399,218,404,375,136,141,289,132,234,291,24,248,424,344,332,72,78,353,333,386,476,23,460,41,274,67,445,206,318,126,24,133,394,96,364,38,25,247,454,387,335,7,232,370,395,75,168,328,49,77,295,75,473,225,399,346,313,235,398,474,486,350,133,413,45,140,361,281,17,115,466,53,157,349,314,197,406,5,52,304,17,195,403,153,115,38,24,172,473,286,344,9,63,309,244,90,431,228,470,31,210,211,231,225,408,346,209,325,483,367,247,486,229,382,256,209,483,335,362,223,199,373,444,437,71,118,222,424,414,457,15,413,325,439,166,163,179,376,135,388,221,54,64,416,247,91,136,393,131,417,105,145,288,149,353,170,99,209,395,386,268,202,284,372,64,293,499,483,194,158,173,484,5,242,196,372,487,51,475,205,274,416,348,155,399,133,249,318,288,240,85,28,131,481,484,490,271,405,253,40,348,229,412,77,406,236,403,212,268,322,220,85,380,416,335,59,227,420,137,190,420,142,285,138,368,140,269,339,22,495,297,69,285,358,374,467,110,147,393,458,61,332,181,256,56,277,48,291,329,384,382,352,361,119,466,112,167,123,237,251,296,228,412,260,154,208,133,50,381,395,352,443,356,450,341,339,466,332,48,262,434,174,308,454,211,136,255,353,343,347,74,17,0,8,194,181,25,384,144,344,308,339,336,229,72,334,133,475,132,237,151,268,126,497,141,8,89
排序結果: 0,4,5,5,5,5,7,8,8,9,10,12,15,17,17,17,17,18,18,22,22,23,24,24,24,25,25,26,28,30,31,33,36,38,38,38,40,40,41,43,43,44,45,47,48,48,48,49,49,50,51,52,52,53,53,54,54,56,57,58,59,59,61,61,61,63,63,64,64,66,67,69,69,71,72,72,74,75,75,75,76,76,76,77,77,78,85,85,85,87,89,90,90,91,95,96,96,97,99,100,104,105,110,112,115,115,115,117,118,118,119,119,121,122,123,123,126,126,127,128,131,131,132,132,133,133,133,133,133,135,135,136,136,136,137,137,137,138,138,140,140,141,141,142,143,143,143,144,145,145,147,148,149,149,151,153,153,154,155,156,156,157,158,161,163,163,166,167,167,168,170,170,172,172,173,174,177,178,179,179,180,181,181,182,190,192,193,194,194,194,194,195,196,197,197,197,199,202,204,205,206,207,207,208,209,209,209,210,211,211,212,216,218,220,221,222,223,225,225,225,227,228,228,229,229,229,229,230,231,232,232,232,234,235,235,236,237,237,238,239,240,242,242,243,244,247,247,247,248,249,249,251,253,253,255,256,256,258,259,260,260,262,262,263,264,266,268,268,268,268,269,271,272,274,274,275,277,281,282,284,285,285,286,288,288,288,289,290,291,291,291,292,293,295,296,297,297,298,304,308,308,309,313,313,313,314,314,317,318,318,318,320,321,322,322,322,322,325,325,328,328,329,332,332,332,332,332,333,333,333,334,334,335,335,335,335,336,336,339,339,339,340,341,343,344,344,344,345,346,346,346,347,348,348,349,349,350,352,352,353,353,353,354,356,357,358,361,361,361,362,363,364,366,367,367,368,369,370,370,370,372,372,373,374,374,375,376,378,378,380,380,380,381,382,382,383,384,384,386,386,387,388,388,389,392,393,393,394,395,395,395,398,398,399,399,399,400,403,403,403,404,405,406,406,406,408,412,412,412,413,413,414,416,416,416,417,417,420,420,424,424,426,431,431,434,435,436,437,439,443,444,444,445,449,450,450,454,454,457,458,459,460,460,461,466,466,466,467,470,470,472,473,473,474,474,475,475,476,481,481,483,483,483,483,484,484,486,486,486,486,487,490,490,495,495,495,497,499,500,500
花費時間: 1 毫秒

相關資源