Node.js 排序處理 (2)

基本介紹

教學目標

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

var insertion_sort_start = new Date().getTime();
var insertion_sort_result = insertionSort(data.slice());
var insertion_sort_stop = new Date().getTime();
var insertion_sort_elapsed = insertion_sort_stop - insertion_sort_start;

console.log("插入排序法");
console.log("原始資料: " + data );
console.log("排序結果: " + insertion_sort_result );
console.log("花費時間: " + insertion_sort_elapsed + " 毫秒" );

function insertionSort(data) {
var temp, inner;
for (var outer = 1; outer <= data.length - 1 ; ++outer) {
temp = data[outer];
inner = outer;
while (inner > 0 && data[inner - 1] >= temp) {
data[inner] = data[inner - 1];
--inner;
}
data[inner] = temp;
}
return data;
}

執行程式

1
$ node algorithm.js

執行結果

1
2
3
4
插入排序法
原始資料: 343,479,136,358,299,408,11,453,404,430,41,203,272,259,336,61,109,83,322,433,168,72,481,246,284,52,64,233,85,310,427,357,454,172,472,220,498,329,211,283,308,470,308,209,319,179,96,198,454,426,399,396,418,24,408,79,180,168,79,215,256,388,428,132,75,109,463,258,276,178,317,355,238,314,40,385,425,160,159,322,261,184,419,391,214,359,184,374,48,14,20,402,371,113,347,194,10,91,428,372,215,459,174,61,151,446,454,226,411,390,213,399,159,272,7,132,308,428,428,196,313,189,215,301,110,82,252,478,277,440,218,406,1,308,214,8,37,352,102,410,79,422,213,75,455,381,354,223,249,6,166,269,168,137,322,33,54,338,122,306,410,420,337,172,164,438,464,354,266,451,378,222,208,147,469,309,88,443,229,357,86,330,110,90,288,436,425,168,122,220,227,185,389,243,383,500,44,45,9,13,276,398,14,383,248,201,440,173,350,495,159,492,364,466,229,416,0,52,428,390,444,128,374,297,185,119,136,228,84,117,10,205,111,77,496,125,66,391,29,388,4,111,410,110,449,423,60,467,41,113,299,451,343,322,376,99,46,204,55,452,38,36,57,31,90,6,58,426,405,220,286,181,26,27,362,209,6,256,383,141,449,379,264,100,42,317,401,7,124,152,182,16,324,108,81,31,333,17,322,106,468,163,479,414,19,226,253,468,382,438,58,455,415,92,264,270,391,249,496,177,182,143,33,226,54,142,220,44,242,453,12,219,246,56,72,329,377,431,163,122,213,117,166,58,288,43,224,261,431,20,214,48,100,386,108,31,287,215,35,158,72,294,158,428,164,52,369,96,314,391,354,111,474,348,31,363,204,101,304,493,201,223,95,67,37,478,110,348,316,57,264,444,195,21,248,471,108,6,337,322,379,189,146,185,357,377,491,147,8,327,289,184,375,355,418,422,293,281,89,289,146,267,430,485,426,60,107,361,94,333,319,343,257,177,24,220,243,326,351,280,257,265,136,289,15,67,155,485,234,71,490,137,268,112,131,475,406,274,87,30,379,13,361,275,440,498,6,476,52,369,265,394,196,387,283,309,122,296,48,74,5,336,348,182,499,33,47,291,256,134,130,159,477,382,392,415,37,118,144,341
排序結果: 0,1,4,5,6,6,6,6,6,7,7,8,8,9,10,10,11,12,13,13,14,14,15,16,17,19,20,20,21,24,24,26,27,29,30,31,31,31,31,33,33,33,35,36,37,37,37,38,40,41,41,42,43,44,44,45,46,47,48,48,48,52,52,52,52,54,54,55,56,57,57,58,58,58,60,60,61,61,64,66,67,67,71,72,72,72,74,75,75,77,79,79,79,81,82,83,84,85,86,87,88,89,90,90,91,92,94,95,96,96,99,100,100,101,102,106,107,108,108,108,109,109,110,110,110,110,111,111,111,112,113,113,117,117,118,119,122,122,122,122,124,125,128,130,131,132,132,134,136,136,136,137,137,141,142,143,144,146,146,147,147,151,152,155,158,158,159,159,159,159,160,163,163,164,164,166,166,168,168,168,168,172,172,173,174,177,177,178,179,180,181,182,182,182,184,184,184,185,185,185,189,189,194,195,196,196,198,201,201,203,204,204,205,208,209,209,211,213,213,213,214,214,214,215,215,215,215,218,219,220,220,220,220,220,222,223,223,224,226,226,226,227,228,229,229,233,234,238,242,243,243,246,246,248,248,249,249,252,253,256,256,256,257,257,258,259,261,261,264,264,264,265,265,266,267,268,269,270,272,272,274,275,276,276,277,280,281,283,283,284,286,287,288,288,289,289,289,291,293,294,296,297,299,299,301,304,306,308,308,308,308,309,309,310,313,314,314,316,317,317,319,319,322,322,322,322,322,322,324,326,327,329,329,330,333,333,336,336,337,337,338,341,343,343,343,347,348,348,348,350,351,352,354,354,354,355,355,357,357,357,358,359,361,361,362,363,364,369,369,371,372,374,374,375,376,377,377,378,379,379,379,381,382,382,383,383,383,385,386,387,388,388,389,390,390,391,391,391,391,392,394,396,398,399,399,401,402,404,405,406,406,408,408,410,410,410,411,414,415,415,416,418,418,419,420,422,422,423,425,425,426,426,426,427,428,428,428,428,428,428,430,430,431,431,433,436,438,438,440,440,440,443,444,444,446,449,449,451,451,452,453,453,454,454,454,455,455,459,463,464,466,467,468,468,469,470,471,472,474,475,476,477,478,478,479,479,481,485,485,490,491,492,493,495,496,496,498,498,499,500
花費時間: 2 毫秒

相關資源