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