|
1 | 1 | # Best case: Omega(nlog(n))
|
2 | 2 | # Worst case: O(nlog(n))
|
3 | 3 |
|
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 |
5 | 11 |
|
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 |
11 | 15 |
|
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 |
15 | 25 |
|
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 |
20 | 38 | end
|
21 |
| - end |
22 |
| - swap(arr, left, partition_index-1) |
23 |
| - partition_index-1 |
24 |
| -end |
25 | 39 |
|
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 |
29 | 49 |
|
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) |
34 | 52 | end
|
35 |
| - end |
36 |
| - swap(arr, right, partition_index) |
37 |
| - partition_index |
38 |
| -end |
39 | 53 |
|
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 |
49 | 54 |
|
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 |
56 | 59 |
|
| 60 | + left_array = [] |
| 61 | + right_array = [] |
| 62 | + pivot = arr[0] |
57 | 63 |
|
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) |
62 | 73 |
|
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 |
66 | 79 |
|
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 |
82 | 82 |
|
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 |
89 | 88 | end
|
90 | 89 | 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