js常用排序?qū)崿F(xiàn)代碼
更新時(shí)間:2010年12月28日 21:28:44 作者:
js常用排序,整理了,常用的數(shù)組互換。
復(fù)制代碼 代碼如下:
<script>
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("個(gè)數(shù)和最大值必須是一個(gè)整數(shù)");
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");
}
</script>
<body onload=generate()>
<table style="width:100%;height:100%;font-size:12px;font-family:宋體">
<tr>
<td align=right>
<textarea id=txtInput readonly style="width:100px;height:100%"></textarea>
</td>
<td width=150 align=center>
隨機(jī)數(shù)個(gè)數(shù)<input id=txtCount value=500 style="width:50px"><br><br>
最大隨機(jī)數(shù)<input id=txtMax value=1000 style="width:50px"><br><br>
<button onclick=generate()>重新生成</button><br><br><br><br>
耗時(shí)(毫秒):<label id=lblTime></label><br><br><br><br>
<button onclick=demo("bubble")>冒泡排序</button><br><br>
<button onclick=demo("selection")>選擇排序</button><br><br>
<button onclick=demo("insertion")>插入排序</button><br><br>
<button onclick=demo("shell")>謝爾排序</button><br><br>
<button onclick=demo("quick")>快速排序(遞歸)</button><br><br>
<button onclick=demo("stackQuick")>快速排序(堆棧)</button><br><br>
<button onclick=demo("merge")>歸并排序</button><br><br>
<button onclick=demo("heap")>堆排序</button><br><br>
</td>
<td align=left>
<textarea id=txtOutput readonly style="width:100px;height:100%"></textarea>
</td>
</tr>
</table>
</body>
相關(guān)文章
js 實(shí)現(xiàn)菜單左右滾動(dòng)顯示示例介紹
菜單左右滾動(dòng)顯示的實(shí)現(xiàn)方法有很多,在本文將為大家介紹下如何使用js實(shí)現(xiàn),需要的朋友可以參考下,希望對(duì)大家有所幫助2013-11-11
用js實(shí)現(xiàn)圖片旋轉(zhuǎn)的兩種方案
這篇文章主要給大家介紹了關(guān)于用js實(shí)現(xiàn)圖片旋轉(zhuǎn)的兩種方案, 旋轉(zhuǎn)的效果就是根據(jù)鼠標(biāo)的的移動(dòng)距離來顯示不同的圖片,形成視覺差,仿佛就是在正真的旋轉(zhuǎn),需要的朋友可以參考下2023-07-07
如何判斷出一個(gè)js對(duì)象是否一個(gè)dom對(duì)象
如何判斷出一個(gè)js對(duì)象是否一個(gè)dom對(duì)象呢?下面小編就為大家?guī)硪黄袛喑鲆粋€(gè)js對(duì)象是否一個(gè)dom對(duì)象的方法。希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2016-11-11
javascript在IE下trim函數(shù)無法使用的解決方法
這篇文章主要介紹了javascript在IE下trim函數(shù)無法使用的解決方法,分別敘述了javascript以及jQuery下的解決方案,對(duì)于WEB前端javascript設(shè)計(jì)人員進(jìn)行瀏覽器兼容性調(diào)試有不錯(cuò)的借鑒價(jià)值,需要的朋友可以參考下2014-09-09
Js遍歷鍵值對(duì)形式對(duì)象或Map形式的方法
下面小編就為大家?guī)硪黄狫s遍歷鍵值對(duì)形式對(duì)象或Map形式的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08
npm安裝依賴時(shí)出現(xiàn)Peer Dependencies沖突報(bào)錯(cuò)解決分析
這篇文章主要為大家介紹了npm安裝依賴時(shí)出現(xiàn)Peer Dependencies沖突報(bào)錯(cuò)解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
JavaScript滑動(dòng)驗(yàn)證功能實(shí)現(xiàn)詳細(xì)代碼
這篇文章主要給大家介紹了關(guān)于JavaScript滑動(dòng)驗(yàn)證功能實(shí)現(xiàn)的相關(guān)資料,滑動(dòng)拼圖驗(yàn)證功能是一種常見的網(wǎng)站人機(jī)驗(yàn)證方法,旨在防止惡意機(jī)器人或自動(dòng)化程序?qū)W(wǎng)站進(jìn)行惡意攻擊,需要的朋友可以參考下2024-06-06
js實(shí)現(xiàn)跨域的4種實(shí)用方法原理分析
這篇文章主要分析了js實(shí)現(xiàn)跨域的4種實(shí)用方法原理,主要是使用jsonp跨域,使用window.name來進(jìn)行跨域,對(duì)這方面感興趣的朋友可以參考一下2015-10-10

