下面把八種經典排序用 JavaScript 實作一次,並掛在 Array.prototype 上方便直接呼叫。每種都附了可互動的測試介面。
Array.prototype.swap = function (i, j) {
var temp = this[i];
this[i] = this[j];
this[j] = temp;
}
Array.prototype.bubbleSort = function () {
for (var i = this.length - 1; i > 0; --i) {
for (var j = 0; j < i; ++j) {
if (this[j] > this[j + 1]) this.swap(j, j + 1);
}
}
}
Array.prototype.selectionSort = function () {
for (var i = 0; i < this.length; ++i) {
var index = i;
for (var j = i + 1; j < this.length; ++j) {
if (this[j] < this[index]) index = j;
}
this.swap(i, index);
}
}
Array.prototype.insertionSort = function () {
for (var i = 1; i < this.length; ++i) {
var j = i, value = this[i];
while (j > 0 && this[j - 1] > value) {
this[j] = this[j - 1];
--j;
}
this[j] = value;
}
}
Array.prototype.shellSort = function () {
for (var step = this.length >> 1; step > 0; step >>= 1) {
for (var i = 0; i < step; ++i) {
for (var j = i + step; j < this.length; j += step) {
var k = j, value = this[j];
while (k >= step && this[k - step] > value) {
this[k] = this[k - step];
k -= step;
}
this[k] = value;
}
}
}
}
Array.prototype.quickSort = function (s, e) {
if (s == null) s = 0;
if (e == null) e = this.length - 1;
if (s >= e) return;
this.swap((s + e) >> 1, e); // 取中間元素為基準
var index = s - 1;
for (var i = s; i <= e; ++i) {
if (this[i] <= this[e]) this.swap(i, ++index);
}
this.quickSort(s, index - 1);
this.quickSort(index + 1, e);
}
Array.prototype.stackQuickSort = function () {
var stack = [0, this.length - 1];
while (stack.length > 0) {
var e = stack.pop(), s = stack.pop();
if (s >= e) continue;
this.swap((s + e) >> 1, e);
var index = s - 1;
for (var i = s; i <= e; ++i) {
if (this[i] <= this[e]) this.swap(i, ++index);
}
stack.push(s, index - 1, index + 1, e);
}
}
Array.prototype.mergeSort = function (s, e, b) {
if (s == null) s = 0;
if (e == null) e = this.length - 1;
if (b == null) b = new Array(this.length); // 共用暫存陣列,避免反覆配置
if (s >= e) return;
var m = (s + e) >> 1;
this.mergeSort(s, m, b);
this.mergeSort(m + 1, e, b);
for (var i = s, j = s, k = m + 1; i <= e; ++i) {
b[i] = this[(k > e || j <= m && this[j] < this[k]) ? j++ : k++];
}
for (var i = s; i <= e; ++i) this[i] = b[i];
}
Array.prototype.heapSort = function () {
// 建堆
for (var i = 1; i < this.length; ++i) {
for (var j = i, k = (j - 1) >> 1; k >= 0; j = k, k = (k - 1) >> 1) {
if (this[k] >= this[j]) break;
this.swap(j, k);
}
}
// 依序取出
for (var i = this.length - 1; i > 0; --i) {
this.swap(0, i);
for (var j = 0, k = (j + 1) << 1; k <= i; j = k, k = (k + 1) << 1) {
if (k == i || this[k] < this[k - 1]) --k;
if (this[k] <= this[j]) break;
this.swap(j, k);
}
}
}
function generate() {
var max = parseInt(txtMax.value), count = parseInt(txtCount.value);
if (isNaN(max) || isNaN(count)) {
alert("個數和最大值必須是一個整數");
return;
}
var array = [];
for (var i = 0; i < count; ++i) {
array.push(Math.round(Math.random() * max));
}
txtInput.value = array.join("\n");
txtOutput.value = "";
}
function demo(type) {
var array = txtInput.value == "" ? [] : txtInput.value.replace().split("\n");
for (var i = 0; i < array.length; ++i) array[i] = parseInt(array[i]);
var t1 = new Date();
eval("array." + type + "Sort()");
var t2 = new Date();
lblTime.innerText = t2.valueOf() - t1.valueOf();
txtOutput.value = array.join("\n");
}
介面用左右兩個 textarea 分別放輸入與輸出,中間是一排按鈕:重新生成、氣泡、選擇、插入、謝爾、快速(遞迴)、快速(堆疊)、歸並、堆排序,並顯示耗時。
demo 中的 eval 應該避免。改用 array[type + "Sort"]() 即可達到同樣效果,不必把字串交給 eval 解析。
直接擴充 Array.prototype 已不建議。它會讓 for...in 出現非預期的鍵,也容易與其他庫衝突。
真實專案請直接用原生 sort。瀏覽器的內建排序是高度最佳化的實作,通常優於任何手寫版本;需要數字排序時記得傳入比較函式 arr.sort(function(a,b){return a-b;}),否則會被當成字串排序。這些手寫實作的價值在於理解演算法本身與時間複雜度的差異。