Node.js 排序處理 (1)

基本介紹

教學目標

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

var bubble_sort_start = new Date().getTime();
var bubble_sort_result = bubbleSort(data.slice());
var bubble_sort_stop = new Date().getTime();
var bubble_sort_elapsed = bubble_sort_stop - bubble_sort_start;

console.log("氣泡排序法");
console.log("原始資料: " + data );
console.log("排序結果: " + bubble_sort_result );
console.log("花費時間: " + bubble_sort_elapsed + " 毫秒" );

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

執行程式

1
$ node algorithm.js

執行結果

1
2
3
4
氣泡排序法
原始資料: 282,53,179,417,249,73,371,282,13,247,461,237,302,110,57,53,193,185,28,226,173,396,224,215,258,116,388,37,48,464,259,357,2,405,415,190,290,27,353,324,120,449,21,88,242,147,247,257,478,124,332,16,155,85,14,127,424,355,369,454,69,145,380,86,22,140,474,390,467,269,430,486,324,224,413,210,9,130,62,176,230,347,495,278,184,92,339,49,123,435,53,58,310,97,353,498,405,105,364,46,308,265,275,365,121,107,391,397,43,335,369,93,116,308,62,57,140,56,463,278,420,483,383,282,421,282,205,278,295,243,160,30,77,44,206,356,442,334,313,140,93,470,230,372,135,305,77,192,221,235,396,426,36,454,193,403,394,364,207,154,97,108,485,476,272,173,275,214,336,194,412,193,91,285,137,422,181,497,301,156,476,420,235,375,403,169,114,303,27,6,456,442,62,484,239,319,119,237,158,27,481,204,76,181,213,158,478,130,48,75,192,467,367,379,246,82,309,178,23,371,151,12,477,288,68,129,215,63,443,192,34,123,233,260,88,150,334,222,181,490,46,10,392,160,370,62,383,222,441,270,103,299,218,251,170,259,458,500,427,232,24,404,436,376,304,74,312,26,24,29,190,90,108,299,324,216,188,488,214,87,52,33,264,85,160,329,170,217,461,36,267,467,195,306,170,198,186,100,425,417,341,171,326,404,415,372,414,172,465,485,349,251,43,190,432,302,286,388,273,460,318,116,381,472,75,416,419,206,8,450,307,316,75,343,372,52,474,419,487,2,493,25,490,148,459,73,73,418,169,225,459,421,326,392,134,350,126,336,126,240,348,107,65,238,291,30,296,124,127,94,200,426,111,57,159,76,82,387,62,107,471,429,207,90,161,376,130,115,416,117,257,119,450,212,7,274,39,423,157,403,176,221,409,16,147,188,483,411,323,349,176,128,357,466,410,310,440,25,253,498,256,356,76,175,380,282,416,91,107,403,428,402,350,76,193,206,479,40,132,58,345,36,318,31,302,279,302,387,216,282,404,66,131,201,411,422,479,192,204,347,238,90,410,465,431,178,437,243,31,335,356,92,397,288,310,93,398,484,57,465,183,265,115,444,293,335,390,380,309,241,105,169,206,429,387,347,23,486,461,251
排序結果: 2,2,6,7,8,9,10,12,13,14,16,16,21,22,23,23,24,24,25,25,26,27,27,27,28,29,30,30,31,31,33,34,36,36,36,37,39,40,43,43,44,46,46,48,48,49,52,52,53,53,53,56,57,57,57,57,58,58,62,62,62,62,62,63,65,66,68,69,73,73,73,74,75,75,75,76,76,76,76,77,77,82,82,85,85,86,87,88,88,90,90,90,91,91,92,92,93,93,93,94,97,97,100,103,105,105,107,107,107,107,108,108,110,111,114,115,115,116,116,116,117,119,119,120,121,123,123,124,124,126,126,127,127,128,129,130,130,130,131,132,134,135,137,140,140,140,145,147,147,148,150,151,154,155,156,157,158,158,159,160,160,160,161,169,169,169,170,170,170,171,172,173,173,175,176,176,176,178,178,179,181,181,181,183,184,185,186,188,188,190,190,190,192,192,192,192,193,193,193,193,194,195,198,200,201,204,204,205,206,206,206,206,207,207,210,212,213,214,214,215,215,216,216,217,218,221,221,222,222,224,224,225,226,230,230,232,233,235,235,237,237,238,238,239,240,241,242,243,243,246,247,247,249,251,251,251,253,256,257,257,258,259,259,260,264,265,265,267,269,270,272,273,274,275,275,278,278,278,279,282,282,282,282,282,282,285,286,288,288,290,291,293,295,296,299,299,301,302,302,302,302,303,304,305,306,307,308,308,309,309,310,310,310,312,313,316,318,318,319,323,324,324,324,326,326,329,332,334,334,335,335,335,336,336,339,341,343,345,347,347,347,348,349,349,350,350,353,353,355,356,356,356,357,357,364,364,365,367,369,369,370,371,371,372,372,372,375,376,376,379,380,380,380,381,383,383,387,387,387,388,388,390,390,391,392,392,394,396,396,397,397,398,402,403,403,403,403,404,404,404,405,405,409,410,410,411,411,412,413,414,415,415,416,416,416,417,417,418,419,419,420,420,421,421,422,422,423,424,425,426,426,427,428,429,429,430,431,432,435,436,437,440,441,442,442,443,444,449,450,450,454,454,456,458,459,459,460,461,461,461,463,464,465,465,465,466,467,467,467,470,471,472,474,474,476,476,477,478,478,479,479,481,483,483,484,484,485,485,486,486,487,488,490,490,493,495,497,498,498,500
花費時間: 3 毫秒

相關資源