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

所属分类: 脚本专栏 / ruby专题 阅读数: 1477
收藏 0 赞 0 分享

刚学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

更多精彩内容其他人还在看

简要解读Ruby面向对象编程中的作用域

作用域在面向对象编程中是一个十分重要的概念,程序构建时必须要理解清楚类和方法以及对象的作用范围,接下来就为大家简要解读Ruby面向对象编程中的作用域
收藏 0 赞 0 分享

详解Ruby中的instance_eval方法及其与class_eval的对比

Ruby的eval族方法将字符串作为代码来执行,instance_eval方法便是其中之一,下面就来详解Ruby中的instance_eval方法及其与class_eval的对比
收藏 0 赞 0 分享

Ruby程序中正则表达式的基本使用教程

和Python与Perl一样,Ruby对正则表达式的支持也是相当好的,这里送出整理的Ruby程序中正则表达式的基本使用教程,需要的朋友可以参考下
收藏 0 赞 0 分享

Ruby on Rails所构建的应用程序基本目录结构总结

Ruby on Rails是Ruby世界中一家独大的Web开发框架,要掌握Rails程序的构建,对其目录结构的了解十分必要,下面就来看一下Ruby on Rails所构建的应用程序基本目录结构总结
收藏 0 赞 0 分享

Ruby中的gem包管理的使用及gem源搭建教程

RubyGems是Ruby世界中的包管理工具,gem命令使用起来就如同Linux中的apt与yum一样,也可以构建自己的gem源,下面就带大家一起来学习Ruby中的gem包管理的使用及gem源搭建教程
收藏 0 赞 0 分享

Linux下Redis数据库的安装方法与自动启动脚本分享

这篇文章主要介绍了Linux下Redis数据库的安装方法与自动启动脚本分享,自动启动脚本分别针对CentOS和Ubuntu系统来给出了编写示例,需要的朋友可以参考下
收藏 0 赞 0 分享

Ruby与Ruby on Rails框架环境搭建的简明教程

这篇文章主要介绍了Ruby与Ruby on Rails框架环境搭建的简明教程,包括RubyGems的升级与OpenSSL的支持等配置,需要的朋友可以参考下
收藏 0 赞 0 分享

Ruby编写HTML脚本替换小程序的实例分享

这篇文章主要介绍了Ruby编写HTML脚本替换小程序的实例分享,单纯使用Ruby中的字符串替换方法而没有涉及更复杂的正则表达式,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Ruby中的代码块对象Proc

在Ruby中一个代码块block不是对象,但可以用Proc来替代其作为对象进行操作,接下来我们就来详解Ruby中的代码块对象Proc
收藏 0 赞 0 分享

Ruby中的Proc类及Proc的类方法Proc.new的使用解析

用Proc类可以用Proc.new来创建一个Proc类,进而来操作块,这里我们就来进行Ruby中的Proc类及Proc的类方法Proc.new的使用解析.
收藏 0 赞 0 分享
查看更多