js常用排序实现代码


Posted in Javascript onDecember 28, 2010
<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("个数和最大值必须是一个整数"); 
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> 
随机数个数<input id=txtCount value=500 style="width:50px"><br><br> 
最大随机数<input id=txtMax value=1000 style="width:50px"><br><br> 
<button onclick=generate()>重新生成</button><br><br><br><br> 
耗时(毫秒):<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>
Javascript 相关文章推荐
js过滤特殊字符输入适合输入、粘贴、拖拽多种情况
Mar 22 Javascript
项目中常用的JS方法整理
Jan 30 Javascript
js实现图片从左往右渐变切换效果的方法
Feb 06 Javascript
JSON字符串转换JSONObject和JSONArray的方法
Jun 03 Javascript
javascript基础知识
Jun 07 Javascript
AngularJS 单元测试(一)详解
Sep 21 Javascript
JS模拟实现ECMAScript5新增的数组方法
Mar 20 Javascript
手机端转换rem适应
Apr 01 Javascript
小发现之浅谈location.search与location.hash的问题
Jun 23 Javascript
使用vuepress搭建静态博客的示例代码
Feb 14 Javascript
详解Vue-Router源码分析路由实现原理
May 15 Javascript
JavaScript 中的无穷数(Infinity)详解
Feb 13 Javascript
深入理解Javascript闭包 新手版
Dec 28 #Javascript
prettify 代码高亮着色器google出品
Dec 28 #Javascript
Pro JavaScript Techniques学习笔记
Dec 28 #Javascript
使用JavaScript检测Firefox浏览器是否启用了Firebug的代码
Dec 28 #Javascript
JavaScript动态调整TextArea高度的代码
Dec 28 #Javascript
JavaScript性能陷阱小结(附实例说明)
Dec 28 #Javascript
javascript写的日历类(基于pj)
Dec 28 #Javascript
You might like
php切割页面div内容的实现代码分享
2012/07/31 PHP
PHP代码保护--Zend Guard的使用详解
2013/06/03 PHP
超强多功能php绿色集成环境详解
2017/01/25 PHP
PHP5.5基于mysqli连接MySQL数据库和读取数据操作实例详解
2019/02/16 PHP
laravel框架与其他框架的详细对比
2019/10/23 PHP
JS启动应用程序的一个简单例子
2008/05/11 Javascript
使用JQuery进行跨域请求
2010/01/25 Javascript
js 自动播放的实例代码
2013/11/19 Javascript
判断iframe里的页面是否加载完成
2014/06/06 Javascript
JavaScript中for循环的使用详解
2015/06/03 Javascript
实例讲解javascript注册事件处理函数
2016/01/09 Javascript
AngularJS基础 ng-class-odd 指令示例
2016/08/01 Javascript
微信小程序 聊天室简单实现
2017/04/19 Javascript
node.js中TCP Socket多进程间的消息推送示例详解
2018/07/10 Javascript
详解wepy开发小程序踩过的坑(小结)
2019/05/22 Javascript
vue选项卡切换登录方式小案例
2019/09/27 Javascript
原生js+css调节音量滑块
2020/01/15 Javascript
vuex实现购物车功能
2020/06/28 Javascript
Python深入学习之闭包
2014/08/31 Python
python 统计一个列表当中的每一个元素出现了多少次的方法
2018/11/14 Python
django 快速启动数据库客户端程序的方法示例
2019/08/16 Python
初次部署django+gunicorn+nginx的方法步骤
2019/09/11 Python
解决pycharm中的run和debug失效无法点击运行
2020/06/09 Python
python实现xlwt xlrd 指定条件给excel行添加颜色
2020/07/14 Python
HTML5+CSS3实例 :canvas 模拟实现电子彩票刮刮乐代码
2016/12/30 HTML / CSS
HTML5在微信内置浏览器下右上角菜单的调整字体导致页面显示错乱的问题
2021/01/19 HTML / CSS
印尼极简主义和实惠的在线家具店:Fabelio
2019/03/27 全球购物
培训主管的岗位职责
2013/11/23 职场文书
小班上学期评语
2014/05/05 职场文书
社区服务活动总结
2014/05/07 职场文书
大学毕业生求职自荐书
2014/06/05 职场文书
计算机实训报告范文
2014/11/05 职场文书
灵魂歌王观后感
2015/06/17 职场文书
好员工观后感
2015/06/17 职场文书
分享Python获取本机IP地址的几种方法
2022/03/17 Python
MySQL约束(创建表时的各种条件说明)
2022/06/21 MySQL