Ruby排序演算法

2021-05-27 10:33:05 字數 3835 閱讀 2431

1.氣泡排序 

百科: 

wiki: 

def bubble_sort(arr)

1.upto(arr.length-1) do |i|

(arr.length-i).times do |j|

if arr[j]>arr[j+1]

arr[j],arr[j+1] = arr[j+1],arr[j]

endend

endarr

endarray = [2.3,1.3,15.02,25.02,45,85.14,56.1,35.2,4.2,15.4]

puts bubble_sort(array)

2.漢諾塔 

百科: 

wiki:  

def move(from,to)

puts "# move to #"

enddef hanoi(n,first,second,third)

if n==1

move(first,third)

else

hanoi(n-1,first,third,second)

move(first,third)

hanoi(n-1,second,first,third)

endend

hanoi(6,"a","b","c")

3.插入排序 

百科: 

wiki: 

def insertion_sort(a)  

a.each_with_index do |el,i|

j = i - 1

while j >= 0

break if a[j] <= el

a[j + 1] = a[j]

j -= 1

end

a[j + 1] = el

end

return a

end

4.選擇排序 

百科: 

wiki: 

def selection_sort(a)

b =

a.size.times do |i|

min = a.min

b << min

a.delete_at(a.index(min))

endreturn b

end

5.shell排序 

百科: 

wiki: 

def shell_sort(a)

gap = a.size

while(gap > 1)

gap = gap / 2

(gap..a.size-1).each do |i|

j = i

while(j > 0)

a[j], a[j-gap] = a[j-gap], a[j] if a[j] <= a[j-gap]

j = j - gap

endend

endreturn a

end

6.合併排序 

百科: 

wiki: 

def merge(l, r)

result =

while l.size > 0 and r.size > 0 do

if l.first < r.first

result << l.shift

else

result << r.shift

endend

if l.size > 0

result += l

endif r.size > 0

result += r

endreturn result

enddef merge_sort(a)

return a if a.size <= 1

middle = a.size / 2

left = merge_sort(a[0, middle])

right = merge_sort(a[middle, a.size - middle])

merge(left, right)

end

7.堆排序 

百科: 

wiki: 

def heapify(a, idx, size)

left_idx = 2 * idx + 1

right_idx = 2 * idx + 2

bigger_idx = idx

bigger_idx = left_idx if left_idx < size && a[left_idx] > a[idx]

bigger_idx = right_idx if right_idx < size && a[right_idx] > a[bigger_idx]

if bigger_idx != idx

a[idx], a[bigger_idx] = a[bigger_idx], a[idx]

heapify(a, bigger_idx, size)

endend

def build_heap(a)

last_parent_idx = a.length / 2 - 1

i = last_parent_idx

while i >= 0

heapify(a, i, a.size)

i = i - 1

endend

def heap_sort(a)

return a if a.size <= 1

size = a.size

build_heap(a)

while size > 0

a[0], a[size-1] = a[size-1], a[0]

size = size - 1

heapify(a, 0, size)

endreturn a

end

8.快速排序 

百科: 

wiki: 

def quick_sort(a)

(x=a.pop) ? quick_sort(a.select) + [x] + quick_sort(a.select) :

end

9.計數排序 

百科: 

wiki: 

def counting_sort(a)

min = a.min

max = a.max

counts = array.new(max-min+1, 0)

a.each do |n|

counts[n-min] += 1

end(0...counts.size).map.flatten

end

10.基數排序 

百科: 

wiki: 

def kth_digit(n, i)

while(i > 1)

n = n / 10

i = i - 1

endn % 10

enddef radix_sort(a)

max = a.max

d = math.log10(max).floor + 1

(1..d).each do |i|

tmp =

(0..9).each do |j|

tmp[j] =

enda.each do |n|

kth = kth_digit(n, i)

tmp[kth] << n

enda = tmp.flatten

endreturn a

end

選擇排序 ruby

先尋找最小的數與array 0 交換,再尋找第二小的數和array 1 交換。array 31,41,59,26,41,58 0.upto array.length 2 do count min 1000 最小值 index 0 start count while startif array sta...

ruby演算法實現

題目一 傳2個引數 1個是整形int 1個是int 陣列 怎麼判斷 這個int 可以在這個陣列裡組合起來 如 已知75,20,15,30,45,55 求後面的陣列的元素中二者之和為75的元素對。def get m a,b half a 2 將a 2加入到陣列,組成陣列d並排序 d b half so...

外部排序ruby實現

value 陣列型別,儲存待比較大小的元素。陣列長度比敗者樹陣列長度大1 loser tree 陣列型別,敗者樹。陣列元素是value陣列的索引 node 待調整的葉子結點在value陣列中的索引 def adjust loser tree value,loser tree,node 求葉子結點的父...