论坛首页 编程语言技术论坛

Ruby排序算法收集

浏览 6944 次
精华帖 (9) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-08-25  
1.冒泡排序

百科:http://baike.baidu.com/view/254413.html?wtp=tt
Wiki:http://zh.wikipedia.org/zh-tw/%E5%86%92%E6%B3%A1%E6%8E%92%E5%BA%8F

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]
      end
    end
  end
    arr
end

array = [2.3,1.3,15.02,25.02,45,85.14,56.1,35.2,4.2,15.4]
puts bubble_sort(array)


2.漢諾塔
百科:http://baike.baidu.com/view/191666.html?wtp=tt
Wiki: http://zh.wikipedia.org/zh-tw/%E6%B1%89%E8%AF%BA%E5%A1%94

def move(from,to)
  puts "#{from} move to #{to}"
end

def 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)
  end
end

hanoi(6,"A","B","C")


3.插入排序
百科:http://baike.baidu.com/view/396887.html?wtp=tt
Wiki:http://zh.wikipedia.org/zh-tw/%E6%8F%92%E5%85%A5%E6%8E%92%E5%BA%8F

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.選擇排序
百科:http://baike.baidu.com/view/547263.html?wtp=tt
Wiki:http://zh.wikipedia.org/zh-tw/%E9%80%89%E6%8B%A9%E6%8E%92%E5%BA%8F

def selection_sort(a)
  b = []
  a.size.times do |i|
    min = a.min
    b << min
    a.delete_at(a.index(min))
  end
  return b
end


5.Shell排序
百科:http://baike.baidu.com/view/549624.html?wtp=tt
Wiki:http://zh.wikipedia.org/zh-tw/%E5%B8%8C%E5%B0%94%E6%8E%92%E5%BA%8F

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
      end
    end
  end
  return a
end


6.合并排序
百科:http://baike.baidu.com/view/3668564.html?wtp=tt
Wiki:http://zh.wikipedia.org/zh-tw/%E5%90%88%E4%BD%B5%E6%8E%92%E5%BA%8F

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
    end
  end
  if l.size > 0
    result += l
  end
  if r.size > 0
    result += r
  end
  return result
end

def 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.堆排序
百科:http://baike.baidu.com/view/157305.html?wtp=tt
Wiki:http://zh.wikipedia.org/zh-tw/%E5%A0%86%E6%8E%92%E5%BA%8F

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)
  end
end

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
  end
end

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)
  end
  return a
end


8.快速排序
百科:http://baike.baidu.com/view/115472.html?wtp=tt
Wiki:http://zh.wikipedia.org/zh-tw/%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F

def quick_sort(a)
  (x=a.pop) ? quick_sort(a.select{|i| i <= x}) + [x] + quick_sort(a.select{|i| i > x}) : []
end


9.計數排序
百科:http://baike.baidu.com/view/1209480.html?wtp=tt
Wiki:http://zh.wikipedia.org/zh-tw/%E8%AE%A1%E6%95%B0%E6%8E%92%E5%BA%8F

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{|i| [i+min]*counts[i]}.flatten
end


10.基數排序
百科:http://baike.baidu.com/view/1170573.html?wtp=tt
Wiki:http://zh.wikipedia.org/zh-tw/%E5%9F%BA%E6%95%B0%E6%8E%92%E5%BA%8F

def kth_digit(n, i)
  while(i > 1)
    n = n / 10
    i = i - 1
  end
  n % 10
end

def 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] = []
    end

    a.each do |n|
      kth = kth_digit(n, i)
      tmp[kth] << n
    end
    a = tmp.flatten
  end
  return a
end


11.桶排序
百科:http://baike.baidu.com/view/1784217.html?wtp=tt
Wiki:http://zh.wikipedia.org/zh-tw/%E6%A1%B6%E6%8E%92%E5%BA%8F

def quick_sort(a)
  (x=a.pop) ? quick_sort(a.select{|i| i <= x}) + [x] + quick_sort(a.select{|i| i > x}) : []
end

def first_number(n)
  (n * 10).to_i
end

def bucket_sort(a)
  tmp = []
  (0..9).each do |j|
    tmp[j] = []
  end
  
  a.each do |n|
    k = first_number(n)
    tmp[k] << n
  end

  (0..9).each do |j|
    tmp[j] = quick_sort(tmp[j])
  end

  tmp.flatten
end

a = [0.75, 0.13, 0, 0.44, 0.55, 0.01, 0.98, 0.1234567]
p bucket_sort(a)

# Result: 
[0, 0.01, 0.1234567, 0.13, 0.44, 0.55, 0.75, 0.98]


12.雞尾酒排序
百科:http://baike.baidu.com/view/1981861.html?wtp=tt
Wiki:http://zh.wikipedia.org/zh-tw/%E9%B8%A1%E5%B0%BE%E9%85%92%E6%8E%92%E5%BA%8F

13.奇偶排序
百科:http://baike.baidu.com/view/2096179.html?wtp=tt
Wiki:http://zh.wikipedia.org/zh-tw/%E5%A5%87%E5%81%B6%E6%8E%92%E5%BA%8F


14.鴿巢排序
百科:http://baike.baidu.com/view/2020276.html?wtp=tt
Wiki:http://zh.wikipedia.org/zh-tw/%E9%B8%BD%E5%B7%A2%E6%8E%92%E5%BA%8F
   发表时间:2010-08-26   最后修改:2010-08-26
a,b=b,a


这样的写法听ns说过,相当的奇妙
小飞人,你好猛
1 请登录后投票
   发表时间:2010-08-26  
代碼基本上都是別人寫的...跟我關系不大..
0 请登录后投票
   发表时间:2010-08-26  
你是我滴玫瑰 你是我滴花
0 请登录后投票
   发表时间:2010-09-01  
kaka2008 写道
a,b=b,a


这样的写法听ns说过,相当的奇妙
小飞人,你好猛


我刚学编程的时候,最记得上课时的一个题目就是用 C 语言在不准用临时变量的情况下,实现a,b=b,a,当然a,b是int类型。

那时一直觉得这个很牛叉,后来学了Ruby之学,居然直接在语言级别就有了,令我少了很多乐趣,哈哈。
0 请登录后投票
   发表时间:2010-09-07  
只是用过JaVa的一些排序,学习了
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics