forked from VAR-solutions/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.rb
More file actions
33 lines (29 loc) · 908 Bytes
/
quick_sort.rb
File metadata and controls
33 lines (29 loc) · 908 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def partition(array, low, high)
pivot = array[high] # Selecting the last element of the array as the pivot element
idx = low - 1 # Rightful index of the pivot element
for counter in low..high
if array[counter] < pivot
idx = idx + 1
array[counter], array[idx] = array[idx], array[counter]
end
end
array[idx+1], array[high] = array[high], array[idx+1]
return idx + 1
end
def quick_sort(array, low, high)
if low < high
idx = partition(array, low, high) # The partition index
quick_sort(array, low, idx-1)
quick_sort(array, idx+1, high)
end
end
# Dummy data for testing
test_array = [7, 6, 5, 4, 3, 2, 1]
# Display unsorted array
puts "Unsorted Array:"
puts test_array.join(' ')
# Perform quick sort
quick_sort(test_array, 0, 6)
# Display the sorted results
puts "Post QuickSort"
puts test_array.join(' ')