Quick sorting (Ruby)

Source: Internet
Author: User

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-8On
Top of that file. That changes the default encoding of all string/Regexp literals in that fileutf-8.

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

Reference: Introduction to algorithms, github.com/kanwei

Common Quick Sort versions:

#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.newindex = 0while (index < 12)  arrayInt[index] = rand(100)  #produce 12 random number  index += 1endputs "The original array is:" + arrayInt.to_sdef QuickSort(arrayInt, first, last)  if first < last      middle = Partition(arrayInt, first, last)    QuickSort(arrayInt, first, middle - 1)    QuickSort(arrayInt, middle + 1, last)       end  enddef 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 + 1endQuickSort(arrayInt, 0, arrayInt.length-1)puts "The sorted array is: " + arrayInt.to_s

Randomization version of Quick Sort:

#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.newindex = 0while (index < 12)  arrayInt[index] = rand(100)  #produce 12 random number  index += 1endputs "The original array is:" + arrayInt.to_sdef RandomizedQuickSort(arrayInt, first, last)  if first < last      middle = RandomizedPartition(arrayInt, first, last)    RandomizedQuickSort(arrayInt, first, middle - 1)    RandomizedQuickSort(arrayInt, middle + 1, last)       end  enddef RandomizedPartition(arrayInt, first, last)  i = rand(last - first + 1) + first   arrayInt[i], arrayInt[last] = arrayInt[last], arrayInt[i]  return Partition(arrayInt, first, last)  enddef 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 + 1endRandomizedQuickSort(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:

#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.newindex = 0while (index < 12)  arrayInt[index] = rand(100)  #produce 12 random number  index += 1endputs "The original array is:" + arrayInt.to_sdef 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.