游客:
注册
|
登录
|
会员
|
搜索
|
统计
|
帮助
无忧脚本
»
JavaScript & VBScript & DHTML 脚本技术讨论版
» 常见排序算法 JS 版
‹‹ 上一主题
|
下一主题 ››
35
1/2
1
2
››
投票
交易
悬赏
活动
打印
|
推荐
|
订阅
|
收藏
标题:
[原创]
常见排序算法 JS 版
microsopht
爬虫
UID 46889
精华
1
积分 113
帖子 55
威望 75
阅读权限 20
注册 2006-2-11
状态 离线
#1
大
中
小
使用道具
发表于 2006-2-21 20:14
资料
个人空间
短消息
加为好友
常见排序算法 JS 版
JS在这方面的效率比 C++、Java 还是要差很多地……
<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>
提示:您可以先修改部分代码再运行
doomchan
大恐龙
UID 12722
精华 0
积分 677
帖子 416
威望 415
阅读权限 50
注册 2004-6-9
来自 广东佛山
状态 离线
#2
大
中
小
使用道具
发表于 2006-2-21 22:43
资料
个人空间
短消息
加为好友
很实用,收了
raid1
小恐龙
UID 40052
精华 0
积分 343
帖子 201
威望 203
阅读权限 30
注册 2005-10-24
状态 离线
#3
大
中
小
使用道具
发表于 2006-2-22 10:47
资料
个人空间
短消息
加为好友
QUOTE:
引用内容由 [i]microsopht[/i] 发表于 2006-2-21 20:14
JS在这方面的效率比 C++、Java 还是要差很多地……
不是语言的问题
无殇
霸王龙
UID 41131
精华
1
积分 2153
帖子 1367
威望 1347
阅读权限 70
注册 2005-11-13
来自 无聊中……
状态 离线
#4
大
中
小
使用道具
发表于 2006-2-22 13:26
资料
个人空间
短消息
加为好友
晕!用谢尔和冒泡效率相差这么多啊!差50倍啊!这是什么算法,太强了吧!
94ok
霸王龙
UID 11533
精华
1
积分 1258
帖子 750
威望 792
阅读权限 70
注册 2004-5-16
状态 离线
#5
大
中
小
使用道具
发表于 2006-3-5 21:41
资料
个人空间
短消息
加为好友
QUOTE:
引用内容由 [i]无殇[/i] 发表于 2006-2-22 01:26 PM
晕!用谢尔和冒泡效率相差这么多啊!差50倍啊!这是什么算法,太强了吧!
相对来说,希尔排序比冒泡排序效率本来就要高些,所以用时差50倍我觉得很正常~!
lybykw
爬虫
UID 4683
精华 0
积分 198
帖子 103
威望 102
阅读权限 20
注册 2003-7-30
来自 成都
状态 离线
#6
大
中
小
使用道具
发表于 2006-9-8 10:11
资料
个人空间
主页
短消息
加为好友
学到一招。
中国网站资源站
中国演艺人才网
Rimifon
(无名)
超级版主
新手上路
UID 11749
精华
3
积分 4993
帖子 2836
威望 2805
阅读权限 150
注册 2004-5-22
来自 湖南长沙
状态 离线
#7
大
中
小
使用道具
发表于 2006-9-8 11:21
资料
个人空间
主页
短消息
加为好友
一招都没学到,留着先……
風雲工作室
=========
广告位招租(做在老百姓眼皮底下的广告)
wllllll
大恐龙
UID 35854
精华 0
积分 739
帖子 402
威望 411
阅读权限 50
注册 2005-8-24
状态 离线
#8
大
中
小
使用道具
发表于 2006-9-8 22:48
资料
个人空间
短消息
加为好友
很让我郁闷 能运行 与运行好的区别。
freeren
小恐龙
UID 40708
精华 0
积分 231
帖子 106
威望 124
阅读权限 30
注册 2005-11-9
状态 离线
#9
大
中
小
使用道具
发表于 2006-9-25 14:36
资料
个人空间
短消息
加为好友
谢谢,先收了再看,很有价值啊!
cntui
爬虫
UID 12040
精华 0
积分 139
帖子 61
威望 76
阅读权限 20
注册 2004-5-28
状态 离线
#10
大
中
小
使用道具
发表于 2007-1-7 01:18
资料
个人空间
短消息
加为好友
正在找这个呢
高薪招聘ajax工程师(北京), msn:cntui@msn.com, mobile: 13161851280
秦皇也爱JS
霸王龙
网恋? 妄念!
UID 67036
精华 0
积分 2287
帖子 1283
威望 1215
阅读权限 70
注册 2007-3-27
来自 湖北武汉
状态 离线
#11
大
中
小
使用道具
发表于 2007-4-3 08:50
资料
个人空间
短消息
加为好友
太强了 各种排序都全了
"我要印"印刷商务平台
和我聊聊
自从一见桃花后,直至如今更不疑!
eisn
小恐龙
UID 15602
精华 0
积分 475
帖子 200
威望 203
阅读权限 30
注册 2004-8-15
状态 离线
#12
大
中
小
使用道具
发表于 2007-4-17 02:49
资料
个人空间
短消息
加为好友
收藏。以后再研究。
JXQ
大恐龙
UID 55510
精华 0
积分 991
帖子 466
威望 247
阅读权限 50
注册 2006-8-17
来自 四川-眉山
状态 离线
#13
大
中
小
使用道具
发表于 2007-4-17 06:56
资料
个人空间
短消息
加为好友
发呆中。。。
auntion
小恐龙
UID 58736
精华 0
积分 216
帖子 101
威望 101
阅读权限 30
注册 2006-11-3
状态 离线
#14
大
中
小
使用道具
发表于 2007-4-17 11:26
资料
个人空间
短消息
加为好友
呵呵,正在找呢.
henry
爬虫
UID 78294
精华 0
积分 25
帖子 10
威望 10
阅读权限 20
注册 2007-11-3
状态 离线
#15
大
中
小
使用道具
发表于 2007-11-6 20:37
资料
个人空间
短消息
加为好友
就凭这一贴我加入51JS,果然有不错的朋友。
wwp3321
爬虫
UID 78574
精华 0
积分 119
帖子 60
威望 47
阅读权限 20
注册 2007-11-8
状态 离线
#16
大
中
小
使用道具
发表于 2007-12-13 00:53
资料
个人空间
短消息
加为好友
收藏,研究
winter
超级版主
软虫
UID 65747
精华
10
积分 5651
帖子 2676
威望 2676
阅读权限 150
注册 2007-2-27
状态 离线
#17
大
中
小
使用道具
发表于 2007-12-13 01:12
资料
个人空间
短消息
加为好友
哈哈 怎么看起来都这么短呢 印象中几个排序都挺长来着
我的blog
ioyichen
小恐龙
UID 77906
精华 0
积分 223
帖子 40
威望 38
阅读权限 30
注册 2007-10-27
状态 离线
#18
大
中
小
使用道具
发表于 2007-12-13 10:28
资料
个人空间
短消息
加为好友
呵呵,以前学算法的时候,写过这个,路过顶帖留名
winter
超级版主
软虫
UID 65747
精华
10
积分 5651
帖子 2676
威望 2676
阅读权限 150
注册 2007-2-27
状态 离线
#19
大
中
小
使用道具
发表于 2007-12-13 14:11
资料
个人空间
短消息
加为好友
俺好像是数据结构学的排序
我的blog
lxny
爬虫
UID 10408
精华 0
积分 46
帖子 15
威望 33
阅读权限 20
注册 2004-4-10
状态 离线
#20
大
中
小
使用道具
发表于 2007-12-13 14:19
资料
个人空间
短消息
加为好友
有个详细的注释就更好了
35
1/2
1
2
››
投票
交易
悬赏
活动
无忧脚本
无忧脚本技术讨论区
> 原创文章 & 讨论汇总版
> Html & XHtml & CSS 网页制作讨论版
> JavaScript & VBScript & DHTML 脚本技术讨论版
> XML & XSL & XPath & VML 网页技术讨论版
> ASP & Access & SQL Server 后台编程讨论版
> PHP & MySQL 后台编程讨论版
> JSP & Java & J2SE 后台编程讨论版
> .Net 相关技术讨论版
> Web 服务器技术
> Flex & Flash 技术讨论版
> Web UI & 图形技术讨论版
无忧脚本资源服务区
> 经典代码、教程资源库
> 参考手册、常用软件资源库
> 无忧合租服务器讨论版
无忧脚本休闲区
> 休闲留言板
> 招聘求职、网站推荐、广告信息版
> 无忧站务管理版
> 垃圾帖回收站
控制面板首页
编辑个人资料
积分交易
公众用户组
好友列表
升级个人空间
基本概况
流量统计
客户软件
发帖量记录
论坛排行
主题排行
发帖排行
积分排行
在线时间
管理团队
当前时区 GMT+8, 现在时间是 2008-8-8 07:26
苏ICP备05080427号
Powered by
Discuz!
5.5.0
© 2001-2007
51JS.COM
Processed in 0.117565 second(s), 7 queries , Gzip enabled
TOP
清除 Cookies
-
联系我们
-
无忧脚本
-
Archiver
-
WAP