Skip to content

Commit 6b6215f

Browse files
committed
Fix quick sort code.
1 parent df67120 commit 6b6215f

File tree

1 file changed

+70
-76
lines changed

1 file changed

+70
-76
lines changed

lib/Algorithm-Sorting/quick_sort.rb

Lines changed: 70 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,89 @@
11
# Best case: Omega(nlog(n))
22
# Worst case: O(nlog(n))
33

4-
values = [5,2,4,8,7,1,3,6]
4+
class QuickSort
5+
class << self
6+
def swap(arr, first_index, second_index)
7+
temp = arr[first_index]
8+
arr[first_index] = arr[second_index]
9+
arr[second_index] = temp
10+
end
511

6-
def swap(arr, first_index, second_index)
7-
temp = arr[first_index]
8-
arr[first_index] = arr[second_index]
9-
arr[second_index] = temp
10-
end
12+
def l_pivot_partition(arr, left, right)
13+
pivot_elem = arr[left]
14+
partition_index = left+1
1115

12-
def l_pivot_partition(arr, left, right)
13-
pivot_elem = arr[left]
14-
partition_index = left+1
16+
(left..right).each do |i|
17+
if(arr[i] < pivot_elem)
18+
swap(arr, i, partition_index)
19+
partition_index += 1
20+
end
21+
end
22+
swap(arr, left, partition_index-1)
23+
partition_index-1
24+
end
1525

16-
(left..right).each do |i|
17-
if(arr[i] < pivot_elem)
18-
swap(arr, i, partition_index)
19-
partition_index += 1
26+
def r_pivot_partition(arr, left, right)
27+
pivot_elem = arr[right]
28+
partition_index = left
29+
30+
(left..right).each do |i|
31+
if(arr[i] < pivot_elem)
32+
swap(arr, i, partition_index)
33+
partition_index += 1
34+
end
35+
end
36+
swap(arr, right, partition_index)
37+
partition_index
2038
end
21-
end
22-
swap(arr, left, partition_index-1)
23-
partition_index-1
24-
end
2539

26-
def r_pivot_partition(arr, left, right)
27-
pivot_elem = arr[right]
28-
partition_index = left
40+
def quick_sort(arr, left, right, left_pivot = true)
41+
if(left < right)
42+
partition_index = l_pivot_partition(arr, left, right) if left_pivot
43+
partition_index = r_pivot_partition(arr, left, right) unless left_pivot
44+
quick_sort(arr, left, partition_index-1, left_pivot)
45+
quick_sort(arr, partition_index+1, right, left_pivot)
46+
end
47+
return arr
48+
end
2949

30-
(left..right).each do |i|
31-
if(arr[i] < pivot_elem)
32-
swap(arr, i, partition_index)
33-
partition_index += 1
50+
def recursive(arr)
51+
quick_sort(arr, 0, arr.size-1, true)
3452
end
35-
end
36-
swap(arr, right, partition_index)
37-
partition_index
38-
end
3953

40-
def quick_sort(arr, left, right, left_pivot = true)
41-
if(left < right)
42-
partition_index = l_pivot_partition(arr, left, right) if left_pivot
43-
partition_index = r_pivot_partition(arr, left, right) unless left_pivot
44-
quick_sort(arr, left, partition_index-1, left_pivot)
45-
quick_sort(arr, partition_index+1, right, left_pivot)
46-
end
47-
return arr
48-
end
4954

50-
p values
51-
p "Sort using first element as pivot: "
52-
p quick_sort(values, 0, values.size-1, true)
53-
values = [5,2,4,8,7,1,3,6]
54-
p "Sort using last element as pivot: "
55-
p quick_sort(values, 0, values.size-1, false)
55+
def non_recursive(arr)
56+
if(arr.size < 2)
57+
return
58+
end
5659

60+
left_array = []
61+
right_array = []
62+
pivot = arr[0]
5763

58-
def order_quick_sort(arr)
59-
if(arr.size < 2)
60-
return
61-
end
64+
for i in (1...arr.size) do
65+
if(arr[i] <= pivot)
66+
left_array.push(arr[i])
67+
else
68+
right_array.push(arr[i])
69+
end
70+
end
71+
order_quick_sort(left_array)
72+
order_quick_sort(right_array)
6273

63-
left_array = []
64-
right_array = []
65-
pivot = arr[0]
74+
index = 0
75+
for j in (0...left_array.size) do
76+
arr[index] = left_array[j]
77+
index += 1
78+
end
6679

67-
for i in (1...arr.size) do
68-
if(arr[i] <= pivot)
69-
left_array.push(arr[i])
70-
else
71-
right_array.push(arr[i])
72-
end
73-
end
74-
order_quick_sort(left_array)
75-
order_quick_sort(right_array)
76-
77-
index = 0
78-
for j in (0...left_array.size) do
79-
arr[index] = left_array[j]
80-
index += 1
81-
end
80+
arr[index] = pivot
81+
index += 1
8282

83-
arr[index] = pivot
84-
index += 1
85-
86-
for k in (0...right_array.size) do
87-
arr[index] = right_array[k]
88-
index += 1
83+
for k in (0...right_array.size) do
84+
arr[index] = right_array[k]
85+
index += 1
86+
end
87+
end
8988
end
9089
end
91-
92-
values = [5,2,4,8,7,1,3,6]
93-
p "Sort using first element as pivot and retaining order: "
94-
order_quick_sort(values)
95-
p values

0 commit comments

Comments
 (0)