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

 更新时间:2015年05月22日 11:02:25   投稿:junjie  
这篇文章主要介绍了Ruby实现的3种快速排序算法,本文给出了快速排序的普通版本、快速排序的随机化版本、快速排序的利用了Ruby的语法糖的随机化版本三个版本,需要的朋友可以参考下

刚学Ruby,正巧算法老师鼓励用不熟悉的语言来写算法,我就用Ruby吧~~
话说Ruby可真是超厉害,好多凭直觉的方法都可以用。。。。。无限膜拜中。。。。

期间我遇到了invalid multibyte char (US-ASCII)的错误,解决办法是在开头加一个#encoding:utf-8
这个错误在stackoverflow上有人问到过,某人给出的回答是
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

快速排序的普通版本:


复制代码 代码如下:

#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

快速排序的随机化版本:

复制代码 代码如下:

#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的语法糖的随机化版本:


复制代码 代码如下:

#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

相关文章

  • PHP实现的一个保存远程文件到本地的函数分享

    PHP实现的一个保存远程文件到本地的函数分享

    这篇文章主要介绍了PHP实现的一个保存远程文件到本地的函数分享,本文直接给出实现代码和使用方法,需要的朋友可以参考下
    2014-11-11
  • Ruby中使用多线程队列(Queue)实现下载博客文章保存到本地文件

    Ruby中使用多线程队列(Queue)实现下载博客文章保存到本地文件

    这篇文章主要介绍了Ruby中使用多线程队列(Queue)实现下载博客文章保存到本地文件,本文给出了实现代码、并对代码的核心部分做了讲解,同时给出了运行效果图,需要的朋友可以参考下
    2015-01-01
  • Ruby中Hash哈希结构的基本操作方法小结

    Ruby中Hash哈希结构的基本操作方法小结

    Hash是一种键值对应的数据结构,Ruby中直接带有Hash类来对其提供支持,这里我们整理了Ruby中Hash哈希结构的基本操作方法小结,首先来回顾一下Hash的基本知识:
    2016-06-06
  • 对Ruby on Rails进行高效的单元测试的教程

    对Ruby on Rails进行高效的单元测试的教程

    这篇文章主要介绍了在Ruby on Rails中进行高效的单元测试的教程,使用到了Ruby的RSpec和Factory Girl框架,需要的朋友可以参考下
    2015-04-04
  • 写一个漂亮Rakefile的方法

    写一个漂亮Rakefile的方法

    这篇文章主要介绍了写一个漂亮Rakefile的方法,文中主要就是写一个类,继承自 Tasklib,然后在这个类的初始化函数里用 task 或者 file 来定义实际完成任务的子 task 即可,需要的朋友可以参考下
    2014-06-06
  • Ruby基础知识之类

    Ruby基础知识之类

    这篇文章主要介绍了Ruby基础知识之类,本文讲解了类的创建、访问器、类变量、常量、类方法、方法的访问性、工厂方法、模块module等内容,需要的朋友可以参考下
    2015-04-04
  • Windows下ruby语言安装教程

    Windows下ruby语言安装教程

    这篇文章主要介绍了Windows下ruby语言安装教程,本文使用rubyinstaller提供的安装包安装,并给出图文说明,非常简单,需要的朋友可以参考下
    2015-02-02
  • 使用Ruby re模块创建复杂的正则表达式

    使用Ruby re模块创建复杂的正则表达式

    复杂的正则表达式很难构建,甚至很难阅读。Ruby的Re模块可以帮助你利用简单的表达式构建复杂的正则表达式
    2014-03-03
  • Ruby迭代器的7种技巧分享

    Ruby迭代器的7种技巧分享

    这篇文章主要介绍了Ruby迭代器的7种技巧分享,Ruby中的迭代器非常人性化,本文既是讲解了7个技巧也是讲解了7种迭代器,需要的朋友可以参考下
    2015-01-01
  • Ruby多线程库(Thread)使用方法详解

    Ruby多线程库(Thread)使用方法详解

    这篇文章主要介绍了Ruby多线程库(Thread)使用方法详解,需要的朋友可以参考下
    2022-04-04

最新评论