mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-11-16 19:50:00 +01:00
Fix NoMethodError
in the quicksort
The code was raising a `NoMethodError` with the description: `quicksort.rb:17:in `<main>': private method `quicksort' called for [34, 2, 1, 5, 3]:Array (NoMethodError)` This commit fixes that
This commit is contained in:
parent
aa4dfbeefb
commit
b950d3530d
1 changed files with 6 additions and 6 deletions
|
@ -1,15 +1,15 @@
|
|||
def quicksort
|
||||
return [] if empty?
|
||||
def quicksort(arr)
|
||||
return [] if arr.empty?
|
||||
|
||||
# chose a random pivot value
|
||||
pivot = delete_at(rand(size))
|
||||
pivot = arr.delete_at(rand(arr.size))
|
||||
# partition array into 2 arrays and comparing them to each other and eventually returning
|
||||
# array with the pivot value sorted
|
||||
left, right = partition(&pivot.method(:>))
|
||||
left, right = arr.partition(&pivot.method(:>))
|
||||
|
||||
# recursively calling the quicksort method on itself
|
||||
return *left.quicksort, pivot, *right.quicksort
|
||||
return *quicksort(left), pivot, *quicksort(right)
|
||||
end
|
||||
|
||||
arr = [34, 2, 1, 5, 3]
|
||||
p arr.quicksort
|
||||
p quicksort(arr)
|
||||
|
|
Loading…
Reference in a new issue