佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > Ruby實(shí)現(xiàn)的3種快速排序算法

Ruby實(shí)現(xiàn)的3種快速排序算法

熱門(mén)標(biāo)簽:語(yǔ)音系統(tǒng) 企業(yè)做大做強(qiáng) 電話(huà)運(yùn)營(yíng)中心 呼叫中心市場(chǎng)需求 百度AI接口 硅谷的囚徒呼叫中心 客戶(hù)服務(wù) Win7旗艦版

剛學(xué)Ruby,正巧算法老師鼓勵(lì)用不熟悉的語(yǔ)言來(lái)寫(xiě)算法,我就用Ruby吧~~
話(huà)說(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

快速排序的普通版本:


復(fù)制代碼 代碼如下:

#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ī)化版本:

復(fù)制代碼 代碼如下:

#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ī)化版本:


復(fù)制代碼 代碼如下:

#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實(shí)現(xiàn)的各種排序算法
  • ruby實(shí)現(xiàn)的插入排序和冒泡排序算法
  • Ruby實(shí)現(xiàn)的矩陣連乘算法
  • Ruby實(shí)現(xiàn)二分搜索(二分查找)算法的簡(jiǎn)單示例
  • Ruby實(shí)現(xiàn)的合并排序算法
  • Ruby實(shí)現(xiàn)的最優(yōu)二叉查找樹(shù)算法
  • Ruby實(shí)現(xiàn)的圖片濾鏡算法代碼

標(biāo)簽:長(zhǎng)沙 濟(jì)南 崇左 安康 海南 山西 山西 喀什

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Ruby實(shí)現(xiàn)的3種快速排序算法》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話(huà)咨詢(xún)

    • 400-1100-266
    会昌县| 闵行区| 丰台区| 富民县| 开江县| 新龙县| 营山县| 沭阳县| 清徐县| 眉山市| 吴江市| 五指山市| 泗洪县| 曲麻莱县| 和龙市| 特克斯县| 山东省| 绥阳县| 罗城| 霍邱县| 伽师县| 岫岩| 积石山| 建平县| 广饶县| 东阿县| 东莞市| 宜都市| 永德县| 泌阳县| 安吉县| 平乡县| 镇沅| 泸西县| 蒙城县| 阳高县| 丹凤县| 阿合奇县| 林周县| 海晏县| 济南市|