Sorting algorithms/Quicksort: Difference between revisions
Content added Content deleted
Line 3,571: | Line 3,571: | ||
def quick_sort |
def quick_sort |
||
return self if length <= 1 |
return self if length <= 1 |
||
pivot = |
pivot = sample |
||
find_all { |i| i < pivot }.quick_sort + |
find_all { |i| i < pivot }.quick_sort + |
||
find_all { |i| i == pivot } + |
find_all { |i| i == pivot } + |
||
Line 3,583: | Line 3,583: | ||
pivot = self[0] |
pivot = self[0] |
||
less, greatereq = self[1..-1].partition { |x| x < pivot } |
less, greatereq = self[1..-1].partition { |x| x < pivot } |
||
less.quick_sort + |
less.quick_sort + [pivot] + greatereq.quick_sort |
||
[pivot] + |
|||
⚫ | |||
end |
end |
||
end</lang> |
end</lang> |
||
or |
|||
<lang ruby>class Array |
|||
⚫ | |||
return self if length <= 1 |
|||
pivot = sample |
|||
group = group_by{ |x| x <=> pivot } |
|||
group.default = [] |
|||
group[-1].quick_sort + group[0] + group[1].quick_sort |
|||
end |
|||
end</lang> |
|||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
<lang runbasic>' ------------------------------- |
<lang runbasic>' ------------------------------- |