js常用排序实现代码_javascript技巧
<script> <BR>Array.prototype.swap = function(i, j) <BR>{ <BR>var temp = this[i]; <BR>this[i] = this[j]; <BR>this[j] = temp; <BR>} <br><br>Array.prototype.bubbleSort = function() <BR>{ <BR>for (var i = this.length - 1; i > 0; --i) <BR>{ <BR>for (var j = 0; j < i; ++j) <BR>{ <BR>if (this[j] > this[j + 1]) this.swap(j, j + 1); <BR>} <BR>} <BR>} <br><br>Array.prototype.selectionSort = function() <BR>{ <BR>for (var i = 0; i < this.length; ++i) <BR>{ <BR>var index = i; <BR>for (var j = i + 1; j < this.length; ++j) <BR>{ <BR>if (this[j] < this[index]) index = j; <BR>} <BR>this.swap(i, index); <BR>} <BR>} <br><br>Array.prototype.insertionSort = function() <BR>{ <BR>for (var i = 1; i < this.length; ++i) <BR>{ <BR>var j = i, value = this[i]; <BR>while (j > 0 && this[j - 1] > value) <BR>{ <BR>this[j] = this[j - 1]; <BR>--j; <BR>} <BR>this[j] = value; <BR>} <BR>} <br><br>Array.prototype.shellSort = function() <BR>{ <BR>for (var step = this.length >> 1; step > 0; step >>= 1) <BR>{ <BR>for (var i = 0; i < step; ++i) <BR>{ <BR>for (var j = i + step; j < this.length; j += step) <BR>{ <BR>var k = j, value = this[j]; <BR>while (k >= step && this[k - step] > value) <BR>{ <BR>this[k] = this[k - step]; <BR>k -= step; <BR>} <BR>this[k] = value; <BR>} <BR>} <BR>} <BR>} <br><br>Array.prototype.quickSort = function(s, e) <BR>{ <BR>if (s == null) s = 0; <BR>if (e == null) e = this.length - 1; <BR>if (s >= e) return; <BR>this.swap((s + e) >> 1, e); <BR>var index = s - 1; <BR>for (var i = s; i <= e; ++i) <BR>{ <BR>if (this[i] <= this[e]) this.swap(i, ++index); <BR>} <BR>this.quickSort(s, index - 1); <BR>this.quickSort(index + 1, e); <BR>} <br><br>Array.prototype.stackQuickSort = function() <BR>{ <BR>var stack = [0, this.length - 1]; <BR>while (stack.length > 0) <BR>{ <BR>var e = stack.pop(), s = stack.pop(); <BR>if (s >= e) continue; <BR>this.swap((s + e) >> 1, e); <BR>var index = s - 1; <BR>for (var i = s; i <= e; ++i) <BR>{ <BR>if (this[i] <= this[e]) this.swap(i, ++index); <BR>} <BR>stack.push(s, index - 1, index + 1, e); <BR>} <BR>} <br><br>Array.prototype.mergeSort = function(s, e, b) <BR>{ <BR>if (s == null) s = 0; <BR>if (e == null) e = this.length - 1; <BR>if (b == null) b = new Array(this.length); <BR>if (s >= e) return; <BR>var m = (s + e) >> 1; <BR>this.mergeSort(s, m, b); <BR>this.mergeSort(m + 1, e, b); <BR>for (var i = s, j = s, k = m + 1; i <= e; ++i) <BR>{ <BR>b[i] = this[(k > e || j <= m && this[j] < this[k]) ? j++ : k++]; <BR>} <BR>for (var i = s; i <= e; ++i) this[i] = b[i]; <BR>} <br><br>Array.prototype.heapSort = function() <BR>{ <BR>for (var i = 1; i < this.length; ++i) <BR>{ <BR>for (var j = i, k = (j - 1) >> 1; k >= 0; j = k, k = (k - 1) >> 1) <BR>{ <BR>if (this[k] >= this[j]) break; <BR>this.swap(j, k); <BR>} <BR>} <BR>for (var i = this.length - 1; i > 0; --i) <BR>{ <BR>this.swap(0, i); <BR>for (var j = 0, k = (j + 1) << 1; k <= i; j = k, k = (k + 1) << 1) <BR>{ <BR>if (k == i || this[k] < this[k - 1]) --k; <BR>if (this[k] <= this[j]) break; <BR>this.swap(j, k); <BR>} <BR>} <BR>} <br><br>function generate() <BR>{ <BR>var max = parseInt(txtMax.value), count = parseInt(txtCount.value); <BR>if (isNaN(max) || isNaN(count)) <BR>{ <BR>alert("个数和最大值必须是一个整数"); <BR>return; <BR>} <BR>var array = []; <BR>for (var i = 0; i < count; ++i) array.push(Math.round(Math.random() * max)); <BR>txtInput.value = array.join("\n"); <BR>txtOutput.value = ""; <BR>} <br><br>function demo(type) <BR>{ <BR>var array = txtInput.value == "" ? [] : txtInput.value.replace().split("\n"); <BR>for (var i = 0; i < array.length; ++i) array[i] = parseInt(array[i]); <BR>var t1 = new Date(); <BR>eval("array." + type + "Sort()"); <BR>var t2 = new Date(); <BR>lblTime.innerText = t2.valueOf() - t1.valueOf(); <BR>txtOutput.value = array.join("\n"); <BR>} <BR></script>
| 随机数个数 最大随机数 耗时(毫秒): | |

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

本文討論了在瀏覽器中優化JavaScript性能的策略,重點是減少執行時間並最大程度地減少對頁面負載速度的影響。

本文討論了使用瀏覽器開發人員工具的有效JavaScript調試,專注於設置斷點,使用控制台和分析性能。

本文說明瞭如何使用源地圖通過將其映射回原始代碼來調試JAVASCRIPT。它討論了啟用源地圖,設置斷點以及使用Chrome DevTools和WebPack之類的工具。

本文探討了Java收藏框架的有效使用。 它強調根據數據結構,性能需求和線程安全選擇適當的收集(列表,設置,地圖,隊列)。 通過高效優化收集用法

掌握了入門級TypeScript教程後,您應該能夠在支持TypeScript的IDE中編寫自己的代碼,並將其編譯成JavaScript。本教程將深入探討TypeScript中各種數據類型。 JavaScript擁有七種數據類型:Null、Undefined、Boolean、Number、String、Symbol(ES6引入)和Object。 TypeScript在此基礎上定義了更多類型,本教程將詳細介紹所有這些類型。 Null數據類型 與JavaScript一樣,TypeScript中的null

本教程將介紹如何使用 Chart.js 創建餅圖、環形圖和氣泡圖。此前,我們已學習了 Chart.js 的四種圖表類型:折線圖和條形圖(教程二),以及雷達圖和極地區域圖(教程三)。 創建餅圖和環形圖 餅圖和環形圖非常適合展示某個整體被劃分為不同部分的比例。例如,可以使用餅圖展示野生動物園中雄獅、雌獅和幼獅的百分比,或不同候選人在選舉中獲得的投票百分比。 餅圖僅適用於比較單個參數或數據集。需要注意的是,餅圖無法繪製值為零的實體,因為餅圖中扇形的角度取決於數據點的數值大小。這意味著任何占比為零的實體
