国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Ruby > 正文

Ruby實現快速排序算法的代碼

2020-02-24 15:30:12
字體:
來源:轉載
供稿:網友

我們在剛學習ruby的時候老師們都會鼓勵我們用不熟悉的語音來寫算法,那么你知道Ruby實現快速排序算法的代碼是什么嗎?下面我們就跟隨武林小編一起去看看吧。

剛學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) ? 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是一個漫長的過程,需要一步一步腳踏實地,希望小編的整理能夠幫助到大家。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 阳新县| 曲麻莱县| 南康市| 出国| 方城县| 桐梓县| 五家渠市| 靖州| 渭南市| 莱阳市| 芒康县| 乐安县| 东兴市| 兴安盟| 清远市| 峨边| 苍溪县| 绥棱县| 蓝山县| 乳源| 广安市| 永福县| 溧水县| 盖州市| 柘城县| 鄄城县| 太康县| 吉木萨尔县| 江安县| 朔州市| 白水县| 乌拉特前旗| 阿拉善右旗| 聂拉木县| 五莲县| 西畴县| 禄丰县| 陵川县| 泰顺县| 永泰县| 中牟县|