From 46962e371fd79c2528a4eb9f353441319a8b9705 Mon Sep 17 00:00:00 2001 From: michellejanosi Date: Sun, 1 Oct 2017 10:20:29 -0400 Subject: [PATCH] Add quicksort algorithm --- quicksort.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 quicksort.rb diff --git a/quicksort.rb b/quicksort.rb new file mode 100644 index 0000000..d3b7456 --- /dev/null +++ b/quicksort.rb @@ -0,0 +1,15 @@ +def quicksort + return [] if empty? + + # chose a random pivot value + pivot = delete_at(rand(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(:>)) + + # recursively calling the quicksort method on itself + return *left.quicksort, pivot, *right.quicksort +end + +arr = [34, 2, 1, 5, 3] +p arr.quicksort