濮阳杆衣贸易有限公司

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

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

熱門標(biāo)簽:百度地圖標(biāo)注自定義圖片 白銀外呼paas系統(tǒng) 地圖標(biāo)注賺錢項(xiàng)目注冊(cè) 常德電銷平臺(tái)外呼系統(tǒng)軟件價(jià)格 滴滴外呼系統(tǒng) 徐州網(wǎng)絡(luò)外呼系統(tǒng)哪個(gè)好 高德地圖標(biāo)注客服 電銷機(jī)器人廠商代理 湖州u友防封電銷卡

時(shí)間復(fù)雜度:Θ(n^2)

Bubble sort

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

def bubble_sort(a) 
  (a.size-2).downto(0) do |i| 
    (0..i).each do |j| 
      a[j], a[j+1] = a[j+1], a[j] if a[j] > a[j+1] 
    end 
  end 
  return a 
end

Selection sort

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

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

Insertion sort

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

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 

 Shell sort
 

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

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

時(shí)間復(fù)雜度:Θ(n*logn)

Merge sort

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

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 

Heap sort

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

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 

Quick sort

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

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

時(shí)間復(fù)雜度:Θ(n)

Counting sort

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

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 

Radix sort

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

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 

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

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] 

您可能感興趣的文章:
  • ruby實(shí)現(xiàn)的插入排序和冒泡排序算法
  • Ruby實(shí)現(xiàn)的矩陣連乘算法
  • Ruby實(shí)現(xiàn)二分搜索(二分查找)算法的簡(jiǎn)單示例
  • Ruby實(shí)現(xiàn)的3種快速排序算法
  • Ruby實(shí)現(xiàn)的合并排序算法
  • Ruby實(shí)現(xiàn)的最優(yōu)二叉查找樹算法
  • Ruby實(shí)現(xiàn)的圖片濾鏡算法代碼

標(biāo)簽:梧州 荊門 永州 公主嶺 遼寧 三沙 張家界 普洱

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Ruby實(shí)現(xiàn)的各種排序算法》,本文關(guān)鍵詞  Ruby,實(shí)現(xiàn),的,各種,排序,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Ruby實(shí)現(xiàn)的各種排序算法》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于Ruby實(shí)現(xiàn)的各種排序算法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    布尔津县| 滨海县| 榆树市| 新河县| 平阳县| 扶余县| 靖江市| 江山市| 马山县| 本溪| 禄丰县| 营口市| 鹤庆县| 中方县| 林西县| 康乐县| 潞西市| 鸡泽县| 安阳市| 五指山市| 麟游县| 法库县| 沈阳市| 泗水县| 永德县| 泾源县| 客服| 大荔县| 阿巴嘎旗| 乾安县| 鞍山市| 大兴区| 敦煌市| 凤翔县| 晋城| 宜君县| 云梦县| 大洼县| 苍山县| 白朗县| 本溪市|