3 Kinds of fast sorting algorithms implemented by Ruby

Source: Internet
Author: User
Tags rand sort

This article mainly introduces 3 kinds of fast sorting algorithms implemented by Ruby, this paper gives the general version of Fast sorting, the random version of fast sorting, the random version of the use of Ruby's syntax sugar, and the three versions, which need friends to refer to under

Just learn ruby, it happens algorithm teacher encourage in unfamiliar language to write algorithm, I use Ruby Bar ~ ~

In other words, Ruby is really awesome, and it's a lot of intuitive way to use ... In the infinite worship ....

During the period I encountered a invalid multibyte char (US-ASCII) error, the solution was to add a #encoding:utf-8 at the beginning

The mistake was asked by someone on the StackOverflow, and the answer was

Write # Encoding:utf-8 on the top of that file. That is changes the default encoding of all STRING/REGEXP literals in that file Utf-8.

Reference Link: http://stackoverflow.com/questions/3678172/ruby-1-9-invalid-multibyte-char-us-ascii

Plain version for quick sorting:

The code is as follows:

#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 (m) #produce random number

Index + + 1

End

Puts "The original array is:" + arrayint.to_s

def QuickSort (Arrayint, I, last)

If < last

Middle = Partition (Arrayint, I, last)

QuickSort (Arrayint, middle-1)

QuickSort (Arrayint, Middle + 1, last)

End

End

def Partition (Arrayint, I, last)

x = Arrayint[last]

i = first-1

For J.. (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

Quick-sorted randomized version:

The code is as follows:

#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 (m) #produce random number

Index + 1

End

Puts "The original array is:" + arrayint.to_s

def randomizedquicksort (Arrayint, I, last)

If < last

Middle = randomizedpartition (Arrayint, I, last)

Randomizedquicksort (Arrayint, middle-1)

Randomizedquicksort (Arrayint, Middle + 1, last)

End

End

def randomizedpartition (Arrayint, I, last)

i = rand (Last-first + 1) + A

Arrayint[i], arrayint[last] = Arrayint[last], arrayint[i]

Return Partition (Arrayint, I, last)

End

def Partition (Arrayint, I, last)

x = Arrayint[last]

i = first-1

For J.. (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

The quick sort utilizes a randomized version of Ruby's syntactic sugars:

The code is as follows:

#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 (m) #produce 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.