`
hideto
  • 浏览: 2678991 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

各种排序的Ruby实现

    博客分类:
  • Ruby
阅读更多
Θ(n^2)

1, Bubble sort
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


2, Selection sort
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


3, Insertion sort
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, Shell sort
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


Θ(n*logn)

1, Merge sort
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


2, Heap sort
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


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


Θ(n)

1, Counting sort
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


2, Radix sort
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


3, Bucket sort
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]
分享到:
评论
2 楼 q1241312 2012-10-20  
厉害,快速排序用循环实现了,不知道时间复杂度是多少。
1 楼 gemstone 2008-11-27  
很不错, 这里也有个实现。


require 'containers/heap' # for heapsort
 
=begin rdoc
This module implements sorting algorithms. Documentation is provided for each algorithm.
=end
module Algorithms::Sort
  # Bubble sort: A very naive sort that keeps swapping elements until the container is sorted.
  # Requirements: Needs to be able to compare elements with <=>, and the [] []= methods should
  # be implemented for the container.
  # Time Complexity: О(n^2)
  # Space Complexity: О(n) total, O(1) auxiliary
  # Stable: Yes
  #
  # Algorithms::Sort.bubble_sort [5, 4, 3, 1, 2] => [1, 2, 3, 4, 5]
  def self.bubble_sort(container)
    loop do
      swapped = false
      (container.size-1).times do |i|
        if (container[i] <=> container[i+1]) == 1
          container[i], container[i+1] = container[i+1], container[i] # Swap
          swapped = true
        end
      end
      break unless swapped
    end
    container
  end
  
  # Comb sort: A variation on bubble sort that dramatically improves performance.
  # Source: http://yagni.com/combsort/
  # Requirements: Needs to be able to compare elements with <=>, and the [] []= methods should
  # be implemented for the container.
  # Time Complexity: О(n^2)
  # Space Complexity: О(n) total, O(1) auxiliary
  # Stable: Yes
  #
  # Algorithms::Sort.comb_sort [5, 4, 3, 1, 2] => [1, 2, 3, 4, 5]
  def self.comb_sort(container)
    container
    gap = container.size
    loop do
      gap = gap * 10/13
      gap = 11 if gap == 9 || gap == 10
      gap = 1 if gap < 1
      swapped = false
      (container.size - gap).times do |i|
        if (container[i] <=> container[i + gap]) == 1
          container[i], container[i+gap] = container[i+gap], container[i] # Swap
          swapped = true
        end
      end
      break if !swapped && gap == 1
    end
    container
  end
  
  # Selection sort: A naive sort that goes through the container and selects the smallest element,
  # putting it at the beginning. Repeat until the end is reached.
  # Requirements: Needs to be able to compare elements with <=>, and the [] []= methods should
  # be implemented for the container.
  # Time Complexity: О(n^2)
  # Space Complexity: О(n) total, O(1) auxiliary
  # Stable: Yes
  #
  # Algorithms::Sort.selection_sort [5, 4, 3, 1, 2] => [1, 2, 3, 4, 5]
  def self.selection_sort(container)
    0.upto(container.size-1) do |i|
      min = i
      (i+1).upto(container.size-1) do |j|
        min = j if (container[j] <=> container[min]) == -1
      end
      container[i], container[min] = container[min], container[i] # Swap
    end
    container
  end
  
  # Heap sort: Uses a heap (implemented by the Containers module) to sort the collection.
  # Requirements: Needs to be able to compare elements with <=>
  # Time Complexity: О(n^2)
  # Space Complexity: О(n) total, O(1) auxiliary
  # Stable: Yes
  #
  # Algorithms::Sort.heapsort [5, 4, 3, 1, 2] => [1, 2, 3, 4, 5]
  def self.heapsort(container)
    heap = Containers::Heap.new(container)
    ary = []
    ary << heap.pop until heap.empty?
    ary
  end
  
  # Insertion sort: Elements are inserted sequentially into the right position.
  # Requirements: Needs to be able to compare elements with <=>, and the [] []= methods should
  # be implemented for the container.
  # Time Complexity: О(n^2)
  # Space Complexity: О(n) total, O(1) auxiliary
  # Stable: Yes
  #
  # Algorithms::Sort.insertion_sort [5, 4, 3, 1, 2] => [1, 2, 3, 4, 5]
  def self.insertion_sort(container)
    return container if container.size < 2
    (1..container.size-1).each do |i|
      value = container[i]
      j = i-1
      while j >= 0 and container[j] > value do
        container[j+1] = container[j]
        j = j-1
      end
      container[j+1] = value
    end
    container
  end
  
  # Shell sort: Similar approach as insertion sort but slightly better.
  # Requirements: Needs to be able to compare elements with <=>, and the [] []= methods should
  # be implemented for the container.
  # Time Complexity: О(n^2)
  # Space Complexity: О(n) total, O(1) auxiliary
  # Stable: Yes
  #
  # Algorithms::Sort.shell_sort [5, 4, 3, 1, 2] => [1, 2, 3, 4, 5]
  def self.shell_sort(container)
    increment = container.size/2
    while increment > 0 do
      (increment..container.size-1).each do |i|
        temp = container[i]
        j = i
        while j >= increment && container[j - increment] > temp do
          container[j] = container[j-increment]
          j -= increment
        end
        container[j] = temp
      end
      increment = (increment == 2 ? 1 : (increment / 2.2).round)
    end
    container
  end
  
  # Quicksort: A divide-and-conquer sort that recursively partitions a container until it is sorted.
  # Requirements: Container should implement #pop and include the Enumerable module.
  # Time Complexity: О(n log n) average, O(n^2) worst-case
  # Space Complexity: О(n) auxiliary
  # Stable: No
  #
  # Algorithms::Sort.quicksort [5, 4, 3, 1, 2] => [1, 2, 3, 4, 5]
  # def self.quicksort(container)
  # return [] if container.empty?
  #
  # x, *xs = container
  #
  # quicksort(xs.select { |i| i < x }) + [x] + quicksort(xs.select { |i| i >= x })
  # end
  
  def self.partition(data, left, right)
    pivot = data[front]
    left += 1
 
    while left <= right do
      if data[frontUnknown] < pivot
        back += 1
        data[frontUnknown], data[back] = data[back], data[frontUnknown] # Swap
      end
 
      frontUnknown += 1
    end
 
    data[front], data[back] = data[back], data[front] # Swap
    back
  end
 
 
  # def self.quicksort(container, left = 0, right = container.size - 1)
  # if left < right
  # middle = partition(container, left, right)
  # quicksort(container, left, middle - 1)
  # quicksort(container, middle + 1, right)
  # end
  # end
  
  def self.quicksort(container)
    bottom, top = [], []
    top[0] = 0
    bottom[0] = container.size
    i = 0
    while i >= 0 do
      l = top[i]
      r = bottom[i] - 1;
      if l < r
        pivot = container[l]
        while l < r do
          r -= 1 while (container[r] >= pivot && l < r)
          if (l < r)
            container[l] = container[r]
            l += 1
          end
          l += 1 while (container[l] <= pivot && l < r)
          if (l < r)
            container[r] = container[l]
            r -= 1
          end
        end
        container[l] = pivot
        top[i+1] = l + 1
        bottom[i+1] = bottom[i]
        bottom[i] = l
        i += 1
      else
        i -= 1
      end
    end
    container
  end
 
  # Mergesort: A stable divide-and-conquer sort that sorts small chunks of the container and then merges them together.
  # Returns an array of the sorted elements.
  # Requirements: Container should implement []
  # Time Complexity: О(n log n) average and worst-case
  # Space Complexity: О(n) auxiliary
  # Stable: Yes
  #
  # Algorithms::Sort.mergesort [5, 4, 3, 1, 2] => [1, 2, 3, 4, 5]
  def self.mergesort(container)
    return container if container.size <= 1
    mid = container.size / 2
    left = container[0...mid]
    right = container[mid...container.size]
    merge(mergesort(left), mergesort(right))
  end
 
  def self.merge(left, right)
    sorted = []
    until left.empty? or right.empty?
      left.first <= right.first ? sorted << left.shift : sorted << right.shift
    end
    sorted + left + right
  end
 
end



Source: http://github.com/kanwei/algorithms/tree/master

相关推荐

    ruby-使用ruby实现的排序算法-sorting.zip

    本资源"ruby-使用ruby实现的排序算法-sorting.zip"聚焦于如何使用Ruby实现不同的排序算法,这对于Ruby开发者来说是一项重要的技能。下面将详细讨论Ruby中的排序算法及其原理。 1. 内置排序方法 `sort` Ruby提供了...

    ruby-使用ruby实现的算法之冒泡排序.zip

    本资料包“ruby-使用ruby实现的算法之冒泡排序.zip”专注于讲解如何使用Ruby来实现经典的冒泡排序算法,这对于理解排序算法以及提升Ruby编程技能非常有帮助。 冒泡排序是一种基础且直观的排序算法,它通过重复遍历...

    Ruby实现的各种排序算法

    ### Ruby 实现的各种排序算法详解 #### 一、概述 在计算机科学中,排序算法是基本且重要的数据处理手段之一,被广泛应用于多种场景。不同的排序算法有着各自的特点与应用场景,选择合适的排序算法能有效提高程序...

    Ruby实现的3种快速排序算法

    ### Ruby实现的三种快速排序算法 #### 一、引言 快速排序是一种高效的排序算法,由英国计算机科学家托尼·霍尔(Tony Hoare)于1960年提出。其核心思想是通过一趟排序将待排记录分隔成独立的两部分,其中一部分的...

    Ruby-Referral一个命令行工具用于查找过滤和排序Ruby代码的定义和引用

    它支持搜索代码库中的各种Ruby标识符,如类、模块、方法、变量等,这些都是构成Ruby程序的基本元素。 类是Ruby中的核心概念,它定义了对象的结构和行为。Referral可以帮助开发者快速定位到类的定义,理解其属性和...

    Ruby-twittercldrrbICU的Ruby实现

    它包含了各种数据,如日期/时间格式、数字格式、货币格式、排序规则、拼写检查等,以及用于处理这些任务的API。ICU在许多语言环境中都是标准的本地化工具,包括Java、C++和.NET等。 Ruby-twittercldrrb库将ICU的...

    一些图形算法的Ruby实现_Ruby_下载.zip

    在这个"一些图形算法的Ruby实现_Ruby_下载.zip"压缩包中,我们可以期待找到一些用Ruby编写的图形算法示例代码,帮助我们理解和应用这些概念。 1. 图的基本概念: - 图是由节点(或顶点)和边构成的数据结构,可以...

    ruby排序算法

    Ruby语言提供了多种实现排序的算法,这些算法各有优缺点,适用于不同的场景。以下将详细阐述标题和描述中提到的几种常见排序算法: 1. 冒泡排序(Bubble Sort) 冒泡排序是一种简单的排序算法,通过不断地交换相邻...

    Ruby冒泡排序的案例

    #### 二、Ruby中的冒泡排序实现 在给定的代码片段中,定义了一个`bubble_sort`方法来实现冒泡排序算法。下面将对该方法进行详细的分析和解释。 ```ruby def bubble_sort(array) n = array.length swapped = true ...

    Ruby语言写一个冒泡排序算法.pdf

    #### 知识点三:Ruby实现冒泡排序 ```ruby def bubble_sort(arr) len = arr.length for i in 0..len-2 swapped = false for j in 0..len-i-2 if arr[j] &gt; arr[j+1] # 交换 arr[j]和 arr[j+1] arr[j], arr[j+...

    基于Ruby实现pagerank算法.zip

    基于Ruby实现Pagerank算法,可以让我们更深入地理解该算法并将其应用于实际项目中。 首先,我们需要了解Pagerank的基本原理。在互联网上,每个网页都可以看作是一个节点,而链接则作为节点之间的边。Pagerank计算时...

    data_structures:各种数据结构的 Ruby 实现

    #Data Structures 这个仓库充满了我对各种数据结构的实现。 虽然这是一个 ruby​​ 存储库,但我尽量不以 Ruby 方式实现这些结构,而是更规范的实现。 ##Array 使用内部数组并提供以下方法&lt;&lt; [] []= 推流行...

    新手Ruby编程语言实现的冒泡排序算法入门demo

    冒泡排序是一种简单的排序算法,它重复地走访过要排序的数列,一次比较两个元素,如果它们的顺序错误就把它们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。 部分代码示例...

    Ruby实现的合并排序算法

    在这个Ruby实现中,我们首先看如何通过代码理解合并排序的工作原理。 ### 合并排序算法概述 合并排序的核心思想是将一个大的数组分为两个较小的数组,对这两个数组分别进行排序,然后将两个已排序的数组合并成一个...

    Ruby实现插入排序算法及进阶的二路插入排序代码示例

    以下是一个二路插入排序的Ruby实现示例: ```ruby def two_way_sort(data) first, final = 0, 0 temp = [data[0]] result = [] data[1..-1].each_with_index do |item, i| if item &gt;= temp[final] final += ...

    ruby写的几个小算法

    在给定的压缩包中,`sort.rb` 和 `search.rb` 文件分别包含了实现这些算法的Ruby代码。 **排序算法**: 排序是指将一组数据按照特定顺序排列的过程。在`sort.rb`文件中,可能包含了不同的排序算法,如冒泡排序、...

    Ruby-使用ordinaregem轻松排序您的Gemfile

    `ordinaregem`是一个实用的工具,专门为Ruby开发者设计,用于自动化`Gemfile`的排序。它遵循一定的规则,如按字母顺序排列Gem,使得整个文件看起来更加整洁,也便于团队协作和代码审查。通过使用`ordinaregem`,...

    Programming-Ruby-1.9源代码

    - 集合操作如查找、排序、合并、过滤等,源代码会提供各种实例,帮助理解Ruby的数组和哈希操作的强大。 6. **文件和I/O** - 文件读写操作,如`File.open`,以及流处理,源代码将展示如何进行文件操作。 - 标准...

Global site tag (gtag.js) - Google Analytics