Three quick sorting algorithms implemented by Ruby
This article mainly introduces three fast sorting algorithms implemented by Ruby, this article provides the normal version of quick sorting, the Randomization version of quick sorting, and the Randomization version of Ruby syntax sugar. For more information, see
I just learned Ruby. It happened that the algorithm teacher encouraged me to write algorithms in unfamiliar languages. I will use Ruby ~~
Ruby is really amazing, and many intuitive methods can be used ..... Infinite worship ....
While I encountered an invalid multibyte char (US-ASCII) error, the solution is to add a # encoding: UTF-8 at the beginning
This error was asked by someone on stackoverflow. The answer Someone gave is:
Write # encoding: UTF-8 on top of that file. That changes the default encoding of all string/regexp literals in that file UTF-8.
Reference: http://stackoverflow.com/questions/3678172/ruby-1-9-invalid-multibyte-char-us-ascii
Common Quick Sort versions:
The Code is as follows:
# Encoding: UTF-8
# Author: xu jin 4100213
# Date: Oct 20,201 2
# 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
Randomization version of Quick Sort:
The Code is as follows:
# Encoding: UTF-8
# Author: xu jin 4100213
# Date: Oct 20,201 2
# 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
The quick sorting utilizes the random version of the Ruby syntax sugar:
The Code is as follows:
# Encoding: UTF-8
# Author: xu jin 4100213
# Date: Oct 20,201 2
# 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 ()
I = rand (a. length)
A [I], a [a. length-1] = a [a. length-1], a [I]
(X = a. pop )? RandomizedQuickSort (a. select {| I <= x}) + [x] + RandomizedQuickSort (a. select {| I> x}): []
End
Puts "The sorted array is:" + RandomizedQuickSort (arrayInt). to_s