快速排序(Ruby)
来源:互联网 发布:asp.net crm源码 编辑:程序博客网 时间:2024/04/30 21:41
刚学Ruby,正巧算法老师鼓励用不熟悉的语言来写算法,我就用Ruby吧~~
话说Ruby可真是超厉害,好多凭直觉的方法都可以用。。。。。无限膜拜中。。。。
期间我遇到了invalid multibyte char (US-ASCII)的错误,解决办法是在开头加一个#encoding:utf-8
这个错误在stackoverflow上有人问到过,某人给出的回答是
Write # encoding: utf-8
on top of that file. That changes the default encoding of all string/regexp literals in that file utf-8
.
参考链接:http://stackoverflow.com/questions/3678172/ruby-1-9-invalid-multibyte-char-us-ascii
参考资料:算法导论,github.com/kanwei
快速排序的普通版本:
#encoding: utf-8#author: xu jin, 4100213#date: Oct 20, 2012#RandomizedQuickSort#to sort an array by using QuickSort#example:#The original array is:[10, 35, 25, 67, 69, 52, 24, 40, 69, 76, 6, 49]#The sorted array is: [6, 10, 24, 25, 35, 40, 49, 52, 67, 69, 69, 76]arrayInt = Array.newindex = 0while (index < 12) arrayInt[index] = rand(100) #produce 12 random number index += 1endputs "The original array is:" + arrayInt.to_sdef QuickSort(arrayInt, first, last) if first < last middle = Partition(arrayInt, first, last) QuickSort(arrayInt, first, middle - 1) QuickSort(arrayInt, middle + 1, last) end enddef Partition(arrayInt, first, last) x = arrayInt[last] i = first - 1 for j in first .. (last - 1) if arrayInt[j] <= x i += 1 arrayInt[i], arrayInt[j] = arrayInt[j], arrayInt[i] #exchange end end arrayInt[i + 1], arrayInt[last] = arrayInt[last], arrayInt[i + 1] return i + 1endQuickSort(arrayInt, 0, arrayInt.length-1)puts "The sorted array is: " + arrayInt.to_s
快速排序的随机化版本:
#encoding: utf-8#author: xu jin, 4100213#date: Oct 20, 2012#RandomizedQuickSort#to sort an array by using randomized QuickSort#example: #The original array is:[14, 47, 46, 49, 82, 76, 92, 22, 44, 81, 59, 61]#The sorted array is: [14, 22, 44, 46, 47, 49, 59, 61, 76, 81, 82, 92]arrayInt = Array.newindex = 0while (index < 12) arrayInt[index] = rand(100) #produce 12 random number index += 1endputs "The original array is:" + arrayInt.to_sdef RandomizedQuickSort(arrayInt, first, last) if first < last middle = RandomizedPartition(arrayInt, first, last) RandomizedQuickSort(arrayInt, first, middle - 1) RandomizedQuickSort(arrayInt, middle + 1, last) end enddef RandomizedPartition(arrayInt, first, last) i = rand(last - first + 1) + first arrayInt[i], arrayInt[last] = arrayInt[last], arrayInt[i] return Partition(arrayInt, first, last) enddef Partition(arrayInt, first, last) x = arrayInt[last] i = first - 1 for j in first .. (last - 1) if arrayInt[j] <= x i += 1 arrayInt[i], arrayInt[j] = arrayInt[j], arrayInt[i] #exchange end end arrayInt[i + 1], arrayInt[last] = arrayInt[last], arrayInt[i + 1] return i + 1endRandomizedQuickSort(arrayInt, 0, arrayInt.length-1)puts "The sorted array is: " + arrayInt.to_s
快速排序的利用了Ruby的语法糖的随机化版本:
#encoding: utf-8#author: xu jin, 4100213#date: Oct 20, 2012#RandomizedQuickSort#to sort an array by using randomized QuickSort#example: #The original array is:[14, 47, 46, 49, 82, 76, 92, 22, 44, 81, 59, 61]#The sorted array is: [14, 22, 44, 46, 47, 49, 59, 61, 76, 81, 82, 92]arrayInt = Array.newindex = 0while (index < 12) arrayInt[index] = rand(100) #produce 12 random number index += 1endputs "The original array is:" + arrayInt.to_sdef RandomizedQuickSort(a) i = rand(a.length) a[i], a[a.length - 1] = a[a.length - 1], a[i] (x=a.pop) ? RandomizedQuickSort(a.select{|i| i <= x}) + [x] + RandomizedQuickSort(a.select{|i| i > x}) : [] end puts "The sorted array is: " + RandomizedQuickSort(arrayInt).to_s
- 快速排序(Ruby)
- ruby 快速排序、合并排序
- ruby快速参考(未完成)
- Ruby排序
- Ruby快速入门(三):控制语句
- Ruby快速入门(2)Array
- Ruby快速入门(30分钟)
- Ruby快速入门(三):控制语句
- Spring 2.0+ JRuby 快速入门二(Ruby快速入门)
- Ruby学习之路——编程实践【3】 排序——快速排序
- 排序总结(快速排序)
- 排序(之快速排序)
- 快速排序(java排序)
- wikioi1076 排序(快速排序)
- 排序(5)快速排序
- c++排序(快速排序)
- 排序(二)快速排序
- 排序(3)--快速排序
- poj 1285 确定比赛名次
- 笔试题 面试题
- wpf鼠标捕获与控件交互——UIElement.CaptureMouse
- 《深入浅出WPF》笔记——绘画与动画
- 刚入职场应该注意什么(转载)
- 快速排序(Ruby)
- gcc编译动态链接库
- RAID常用级别比较
- Java StringBuffer类基本用法详解
- Java算法(队列操作实例)
- MFC编程中,组合框下拉列表不能弹出
- nyoj91 阶乘之和
- 一些常用C语言库函数实现
- ZOJ 3261 Connections in Galaxy War(并查集)