Ruby Quicksort function

class QuickSort def self.sort!(keys) quick(keys,0,keys.size-1) end private def self.quick(keys, left, right) if left < right pivot = partition(keys, left, right) quick(keys, left, pivot-1) quick(keys, pivot+1, right) end keys end def self.partition(keys, left, right) x = keys[right] i = left-1 for j in left..right-1 if keys[j] <= x i += 1 keys[i], keys[j] = keys[j], keys[i] end end keys[i+1], keys[right] = keys[right], keys[i+1] i+1 end end
This is an example of a Ruby Quicksort function made in hand using two others functions:
- quick
- partition

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.