Fast sorting, two implementations

Source: Internet
Author: User

When I'm idle, I just want to hear about the introduction to algorithms from foreigners in Netease's classroom. Now I have achieved a quick sorting. I have used two methods to achieve this. One is what foreigners talk about,

When looking for a partitin partition, traverse from the left + 1 to the right and find the position of the partition after traversing it. The other is to start from both the left and right sides and finally converge to the position of the partition.

The following program is written in Ruby:

#--------------------------------------------------------------------
# Anthor: normallife
# Date: 30/3/2013
#--------------------------------------------------------------------

Require 'fileutils'

$ Outputfile = file.open('result.txt ', 'W + ')

Class quicksortmethod

# Iterate from the left + 1 to the right to find the position
Def quicksortmethod. partitionsecond arraybesorted, ibegin, iend
If (ibegin <iend)
IB = ibegin
Ie = ibegin + 1
Bytes = arraybesorted [IB]
While IE <= iend
If arraybesorted [ie] <bytes
IB = IB + 1
Temp = arraybesorted [IB]
Arraybesorted [IB] = arraybesorted [ie]
Arraybesorted [ie] = temp
End
Ie = ie + 1
End
Temp = arraybesorted [IB]
Arraybesorted [IB] = arraybesorted [ibegin]
Arraybesorted [ibegin] = temp
Return IB
End
Return 0
End

# Iterate from the left and right to meet at the specified position
Def quicksortmethod. partitionfirst arraybesorted, ibegin, iend
IB = ibegin
Ie = iend
Dependencies = arraybesorted [ibegin]
While (ib <IE)
While (Bytes> arraybesorted [IB])
IB = IB + 1
End
While (Bytes <arraybesorted [ie])
Ie = ie-1
End

Temp = arraybesorted [IB]
Arraybesorted [IB] = arraybesorted [ie]
Arraybesorted [ie] = temp
End


# Temp = arraybesorted [IB]
# Arraybesorted [IB] = arraybesorted [ie]
# Arraybesorted [ie] = temp

# Temp = temperature
# Keys = arraybesorted [ie]
# Arraybesorted [ie] = temp
 

Return IE
End

Def quicksortmethod. traditionalquicksort arraybesorted, ibegin, iend
$ Outputfile. Puts "quicksort method begins !! "
Arraybesorted. Each {| v |
$ Outputfile. Puts v. to_s
}
If arraybesorted. Length = 0
Puts "arraybesorted is empty array !! "
Return false
End

If (ibegin <iend)
Ipivot = quicksortmethod. partitionsecond (arraybesorted, ibegin, iend)
$ Outputfile. Puts "after one partition, the parameter is number: # {ipivot }"
Quicksortmethod. traditionalquicksort (arraybesorted, IBM, iPivot-1)
Quicksortmethod. traditionalquicksort (arraybesorted, ipivot + 1, iend)
End

Return true
End

End

Begin
Arrayinquicksort = array. [] (5, 2, 3, 8, 9, 1, 7)
Quicksortmethod. traditionalquicksort (arrayinquicksort, 0, 6)
P arrayinquicksort
$ Outputfile. Close ()
End

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.