fastsort 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5138e2c24fc0375ad7ce6ab52e0d2687c92298fd27ce998d6aaf718c2c43a846
4
- data.tar.gz: 640b5566de987806787c1d8f38d1f77d8a6c476f0caf37077989fff8533d159e
3
+ metadata.gz: 9c7b13967306e965a337cf00b9b8debca59787d6ee788af65b3242e606507ae2
4
+ data.tar.gz: 04a5570f6b32a7eea2f18e22e174ef53591e7968ff9b4fe1bc5222148c1f070d
5
5
  SHA512:
6
- metadata.gz: dc0abc389f64ee1fbea1b2e74643e130a87c69f3cdee81a904eaf7adfa0561f530ee43ca79e6a5d8eb40ca5812385a9a190a699b1acf6fdddb94a84b8ff25954
7
- data.tar.gz: 713c5b8999af7a93a7f466c8ea2eb17ec36479f94d91736c1b8900efe22d689e7295720d3a2e992d5458f79539344ae6ad9053421f90529675f702224d08b32c
6
+ metadata.gz: 961c2cc4b1a10d4dbd97408546d02294ffa884a784da0655cc96a4cb74848ff50aa757c71fe3e18ec102e706bce77bd99c14e8d1d34a31af5a2180aae57ec291
7
+ data.tar.gz: 17ffc5adb2895eb51c53f45512771111be4a9c4f50892fdf63d61e7ed8489b7427d12ef7386ebb46d3b104674ed1ea5894f4a0708038f688e7019b6ea742e767
data/README.md CHANGED
@@ -30,14 +30,17 @@ arr = [
30
30
 
31
31
  sorter001 = Fastsort::Quicksort.new(arr)
32
32
  sorter002 = Fastsort::Mergesort.new(arr)
33
+ sorter003 = Fastsort::Sonicsort.new(arr)
33
34
 
34
35
  sorter001.sort_array
36
+ sorter002.sort_array
37
+ sorter003.sort_array
35
38
 
36
39
  puts "Sorted Array: #{sorter001.array}"
37
40
  puts "Sorted Array: #{sorter002.array}"
41
+ puts "Sorted Array: #{sorter003.array}"
42
+
38
43
 
39
- Sorted Array Quicksort: [1, 6, 7, 9, 10, 12, 14, 15, 18, 21, 21, 22, 24, 25, 30, 31, 34, 38, 39, 40, 41, 45, 53, 55, 57, 59, 60, 63, 66, 67, 67, 69, 71, 75, 75, 75, 79, 79, 79, 80, 83, 83, 85, 87, 92, 92, 93, 97, 98, 99]
40
- Sorted Array Mergesort: [1, 6, 7, 9, 10, 12, 14, 15, 18, 21, 21, 22, 24, 25, 30, 31, 34, 38, 39, 40, 41, 45, 53, 55, 57, 59, 60, 63, 66, 67, 67, 69, 71, 75, 75, 75, 79, 79, 79, 80, 83, 83, 85, 87, 92, 92, 93, 97, 98, 99]
41
44
 
42
45
 
43
46
  ```
Binary file
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Fastsort
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.3"
5
5
  end
data/lib/fastsort.rb CHANGED
@@ -92,41 +92,74 @@ module Fastsort
92
92
 
93
93
  def sort_quick_and_merge
94
94
  raise ArgumentError, 'Array cannot be empty' if array.empty?
95
- sort_and_merge(0, array.length - 1)
95
+ quicksort(0, array.length - 1)
96
+ merge_sort(0, array.length - 1)
96
97
  self
97
98
  end
98
99
 
99
100
  private
100
101
 
101
- def sort_and_merge(from_idx, to_idx)
102
+ def quicksort(from_idx, to_idx)
102
103
  return if from_idx >= to_idx
103
104
 
104
- # Use Quicksort before Mergesort
105
105
  pivot_idx = quicksort_partition(from_idx, to_idx)
106
106
 
107
- # Continue with Mergesort on both sides of the pivot
108
- sort_and_merge(from_idx, pivot_idx - 1)
109
- sort_and_merge(pivot_idx + 1, to_idx)
107
+ quicksort(from_idx, pivot_idx - 1)
108
+ quicksort(pivot_idx + 1, to_idx)
110
109
  end
111
110
 
112
111
  def quicksort_partition(from_idx, to_idx)
113
- pivot_idx = array[to_idx]
114
- pointer_a_idx = pointer_b_idx = from_idx
112
+ pivot_idx = to_idx
113
+ pivot_value = array[pivot_idx]
115
114
 
116
- while pointer_a_idx < to_idx
117
- if array[pointer_a_idx] <= pivot_idx
115
+ raise ArgumentError, 'Invalid pivot value' if pivot_value.nil?
116
+
117
+ pointer_b_idx = from_idx
118
+
119
+ ((from_idx...to_idx).to_a).each do |pointer_a_idx|
120
+ current_value = array[pointer_a_idx]
121
+
122
+ next if current_value.nil?
123
+
124
+ if current_value <= pivot_value
118
125
  swap_values(pointer_a_idx, pointer_b_idx)
119
126
  pointer_b_idx += 1
120
127
  end
121
- pointer_a_idx += 1
122
128
  end
123
129
 
124
- swap_values(pointer_b_idx, to_idx)
130
+ swap_values(pointer_b_idx, pivot_idx)
125
131
  pointer_b_idx
126
132
  end
127
133
 
134
+
135
+ def merge_sort(from_idx, to_idx)
136
+ return if from_idx >= to_idx
137
+
138
+ middle_idx = (from_idx + to_idx) / 2
139
+
140
+ merge_sort(from_idx, middle_idx)
141
+ merge_sort(middle_idx + 1, to_idx)
142
+ merge_subarrays(from_idx, middle_idx, to_idx)
143
+ end
144
+
145
+ def merge_subarrays(from_idx, middle_idx, to_idx)
146
+ left = array[from_idx..middle_idx]
147
+ right = array[(middle_idx + 1)..to_idx]
148
+
149
+ result = []
150
+
151
+ until left.empty? || right.empty?
152
+ result << (left.first <= right.first ? left.shift : right.shift)
153
+ end
154
+
155
+ result.concat(left).concat(right)
156
+
157
+ array[from_idx..to_idx] = result
158
+ end
159
+
128
160
  def swap_values(let_idx_a, let_idx_b)
129
161
  array[let_idx_a], array[let_idx_b] = array[let_idx_b], array[let_idx_a]
130
162
  end
131
163
  end
164
+
132
165
  end
data/main.rb CHANGED
@@ -36,10 +36,12 @@ arr = [
36
36
 
37
37
  sorter_001 = Fastsort::Quicksort.new(arr)
38
38
  sorter_002 = Fastsort::Mergesort.new(arr)
39
+ sorter_003 = Fastsort::Sonicsort.new(arr)
39
40
 
40
41
  sorter_001.sort_array
41
42
  sorter_002.sort_array
43
+ sorter_003.sort_quick_and_merge
42
44
 
43
- # puts "Sorted Array: #{sorter_001.array}"
44
45
  puts "Sorted Array Quicksort: #{sorter_001.array}"
45
46
  puts "Sorted Array Mergesort: #{sorter_002.array}"
47
+ puts "Sorted Array Sonicsort: #{sorter_003.array}"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fastsort
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dants0
@@ -23,6 +23,7 @@ files:
23
23
  - Rakefile
24
24
  - fastsort-0.1.0.gem
25
25
  - fastsort-0.1.1.gem
26
+ - fastsort-0.1.2.gem
26
27
  - fastsort.gemspec
27
28
  - lib/fastsort.rb
28
29
  - lib/fastsort/version.rb