Ruby實(shí)現(xiàn)的3種快速排序算法
剛學(xué)Ruby,正巧算法老師鼓勵(lì)用不熟悉的語(yǔ)言來(lái)寫(xiě)算法,我就用Ruby吧~~
話說(shuō)Ruby可真是超厲害,好多憑直覺(jué)的方法都可以用。。。。。無(wú)限膜拜中。。。。
期間我遇到了invalid multibyte char (US-ASCII)的錯(cuò)誤,解決辦法是在開(kāi)頭加一個(gè)#encoding:utf-8
這個(gè)錯(cuò)誤在stackoverflow上有人問(wèn)到過(guò),某人給出的回答是
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
快速排序的隨機(jī)化版本:
#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的語(yǔ)法糖的隨機(jī)化版本:
#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) ? RandomizedQuickSort(a.select{|i| i <= x}) + [x] + RandomizedQuickSort(a.select{|i| i > x}) : []
end
puts "The sorted array is: " + RandomizedQuickSort(arrayInt).to_s
相關(guān)文章
Ruby on Rails中Rack中間件的基礎(chǔ)學(xué)習(xí)教程
Rack是一個(gè)連接Ruby程序與服務(wù)器程序之間的中間件,甚至可以說(shuō)Rails也是在Rack的基礎(chǔ)上建立起來(lái)的,這里我們就來(lái)為大家?guī)?lái)Ruby on Rails中Rack中間件的基礎(chǔ)學(xué)習(xí)教程2016-06-06Ruby中使用SWIG編寫(xiě)ruby擴(kuò)展模塊實(shí)例
這篇文章主要介紹了Ruby中使用SWIG編寫(xiě)ruby擴(kuò)展模塊實(shí)例,SWIG是一種簡(jiǎn)化腳本語(yǔ)言與C/C++接口的開(kāi)發(fā)工具,需要的朋友可以參考下2014-09-09詳解Ruby中正則表達(dá)式對(duì)字符串的匹配和替換操作
這篇文章主要介紹了Ruby中正則表達(dá)式對(duì)字符串的匹配和替換操作,包括對(duì)結(jié)果分組和一些特殊全局變量的介紹,需要的朋友可以參考下2016-04-04Ruby使用GDBM操作DBM數(shù)據(jù)存儲(chǔ)方法實(shí)例詳解
這篇文章主要介紹了Ruby使用GDBM操作DBM數(shù)據(jù)存儲(chǔ)方法實(shí)例詳解,需要的朋友可以參考下2022-04-04Windows下Ruby+Watir自動(dòng)化測(cè)試的環(huán)境搭建及數(shù)據(jù)讀取
這篇文章主要介紹了Windows下Ruby+Watir自動(dòng)化測(cè)試的環(huán)境搭建及數(shù)據(jù)讀取,Watir是一個(gè)使用Ruby實(shí)現(xiàn)的開(kāi)源Web自動(dòng)化測(cè)試框架,需要的朋友可以參考下2016-03-03舉例理解Ruby on Rails的頁(yè)面緩存機(jī)制
這篇文章主要介紹了舉例理解Ruby on Rails的頁(yè)面緩存機(jī)制,本文來(lái)自于IBM官方網(wǎng)站技術(shù)文檔,需要的朋友可以參考下2015-04-04