刚学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
快速排序的普通版本:
复制代码 代码如下:
#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.new
index = 0
while (index < 12)
arrayInt[index] = rand(100) #produce 12 random number
index += 1
end
puts "The original array is:" + arrayInt.to_s
def QuickSort(arrayInt, first, last)
if first < last
middle = Partition(arrayInt, first, last)
QuickSort(arrayInt, first, middle - 1)
QuickSort(arrayInt, middle + 1, last)
end
end
def 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 + 1
end
QuickSort(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.new
index = 0
while (index < 12)
arrayInt[index] = rand(100) #produce 12 random number
index += 1
end
puts "The original array is:" + arrayInt.to_s
def RandomizedQuickSort(arrayInt, first, last)
if first < last
middle = RandomizedPartition(arrayInt, first, last)
RandomizedQuickSort(arrayInt, first, middle - 1)
RandomizedQuickSort(arrayInt, middle + 1, last)
end
end
def RandomizedPartition(arrayInt, first, last)
i = rand(last - first + 1) + first
arrayInt[i], arrayInt[last] = arrayInt[last], arrayInt[i]
return Partition(arrayInt, first, last)
end
def 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 + 1
end
RandomizedQuickSort(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.new
index = 0
while (index < 12)
arrayInt[index] = rand(100) #produce 12 random number
index += 1
end
puts "The original array is:" + arrayInt.to_s
def RandomizedQuickSort(a)
i = rand(a.length)
a[i], a[a.length - 1] = a[a.length - 1], a[i]
(x=a.pop) "The sorted array is: " + RandomizedQuickSort(arrayInt).to_s
Ruby,快速排序,算法
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]