我正在尝试在ruby中实现快速排序,但是在第一个pivot分区之后陷入了如何递归调用的问题。请帮助我了解如何继续,并告诉我目前我的编码风格是否良好。
class QuickSort
$array= Array.new()
$count=0
def add(val) #adding values to sort
i=0
while val != '000'.to_i
$array[i]= val.to_i
i=i+1
val = gets.to_i
end
end
def firstsort_aka_divide(val1,val2,val3) #first partition
$count = $count+1
@pivot = val1
@left = val2
@right =val3
while @left!=@right do # first divide/ partition logic
if $array[@right] > $array[@pivot] then
@right= @right-1
elsif $array[@right] < $array[@pivot] then
@var = $array[@right]
$array[@right] = $array[@pivot]
$array[@pivot] = @var
@pivot = @right
@left = @left+1
end
if $array[@left] < $array[@pivot]
@left= @left+1
elsif $array[@left] > $array[@pivot]
@var = $array[@left]
$array[@left] = $array[@pivot]
$array[@pivot] = @var
@pivot =@left
end
end
puts "\n" # printing after the first partition i.e divide
print " Array for for divide ---> #{$array}"
puts "\n"
puts " pivot,left,right after first divide --> #{@pivot},#{@left},#{@right}"
firstsort_aka_divide() # Have to call left side of partition recursively -- need help
firstsort_aka_divide() # Have to call right side of partition recursively -- need help
end
end
ob= QuickSort.new
puts " Enter the numbers you want to sort. \n Press '000' once you are done entering the values"
val = gets.to_i
ob.add(val)
puts " Sorting your list ..."
sleep(2)
ob.firstsort_aka_divide(0,0,($array.size-1)) # base condition for partitioning
答案 0 :(得分:11)
这是一个(非常)天真的快速排序实现,基于Wikipedia's simple-quicksort pseudocode:
def quicksort(array) #takes an array of integers as an argument
您需要一个基本案例,否则您的递归调用永远不会终止
if array.length <= 1
return array
现在选择一个支点:
else
pivot = array.sample
array.delete_at(array.index(pivot)) # remove the pivot
#puts "Picked pivot of: #{pivot}"
less = []
greater = []
循环遍历数组,将项目与pivot进行比较,并将它们收集到less
和greater
数组中。
array.each do |x|
if x <= pivot
less << x
else
greater << x
end
end
现在,递归调用quicksort()
和less
数组上的greater
。
sorted_array = []
sorted_array << self.quicksort(less)
sorted_array << pivot
sorted_array << self.quicksort(greater)
返回sorted_array
,您就完成了。
# using Array.flatten to remove subarrays
sorted_array.flatten!
您可以使用
进行测试qs = QuickSort.new
puts qs.quicksort([1, 2, 3, 4, 5]) == [1, 2, 3, 4, 5] # true
puts qs.quicksort([5]) == [5] # true
puts qs.quicksort([5, -5, 11, 0, 3]) == [-5, 0, 3, 5, 11] # true
puts qs.quicksort([5, -5, 11, 0, 3]) == [5, -5, 11, 0, 3] # false
答案 1 :(得分:11)
这就是我在Ruby中实现快速排序的方法:
def quicksort(*ary)
return [] if ary.empty?
pivot = ary.delete_at(rand(ary.size))
left, right = ary.partition(&pivot.method(:>))
return *quicksort(*left), pivot, *quicksort(*right)
end
实际上,我可能会将它改为Array
的实例方法:
class Array
def quicksort
return [] if empty?
pivot = delete_at(rand(size))
left, right = partition(&pivot.method(:>))
return *left.quicksort, pivot, *right.quicksort
end
end
答案 2 :(得分:3)
here is another way to implement quicksort -- as a newbie I think it's easier to understand -- hope it helps someone :) in this implementation the pivot is always the last element in the array -- I'm following the Khan Academy course and that's where I got the inspiration from
def quick_sort(array, beg_index, end_index)
if beg_index < end_index
pivot_index = partition(array, beg_index, end_index)
quick_sort(array, beg_index, pivot_index -1)
quick_sort(array, pivot_index + 1, end_index)
end
array
end
#returns an index of where the pivot ends up
def partition(array, beg_index, end_index)
#current_index starts the subarray with larger numbers than the pivot
current_index = beg_index
i = beg_index
while i < end_index do
if array[i] <= array[end_index]
swap(array, i, current_index)
current_index += 1
end
i += 1
end
#after this swap all of the elements before the pivot will be smaller and
#after the pivot larger
swap(array, end_index, current_index)
current_index
end
def swap(array, first_element, second_element)
temp = array[first_element]
array[first_element] = array[second_element]
array[second_element] = temp
end
puts quick_sort([2,3,1,5],0,3).inspect #will return [1, 2, 3, 5]
答案 3 :(得分:-1)
这是我的快速排序版本
class QuickSort
def partition(arr, low ,high)
pivot = arr[high]
i = low
for j in low...high
if arr[j] < pivot
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
i = i+1
end
end
temp = arr[i]
arr[i] = arr[high]
arr[high] = temp
return i
end
def sort(arr, low, high)
if (low < high)
pi = partition(arr, low, high)
sort(arr, low, pi-1)
sort(arr, pi+1, high)
end
arr
end
end
arr = [10, 5, 80, 30, 100, 140, 90, 40, 50, 70, 72, 150, 63]
n = arr.length
qs = QuickSort.new
arr = qs.sort(arr, 0, n-1)
puts "Quick Sorted Array ==>#{arr}"
#Quick Sorted Array ==>[5, 10, 30, 40, 50, 63, 70, 72, 80, 90, 100, 140, 150]