fastsort 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '08018e1e9579c8454b8cda1bfa90f63f979ffb37e948fdfc011835df2b9d1eea'
4
- data.tar.gz: '09836fc38630639c43fa2bb7c475c34def609bb1e3d8dfeab799eb95035fedc3'
3
+ metadata.gz: dc6da324d5e82ea3fc7e86f3c1867c4bfb71fffde7f40c61adc91993bf814793
4
+ data.tar.gz: f029598b6363d8d10f6b0b561cf03d3570d4e7d8d69dcf340802dda72b89a2a7
5
5
  SHA512:
6
- metadata.gz: 6e5d81f92f004e9add3cc6d4e14ee6e15d832a8fc5977b610304c82a4c3de3577e5540467d30328a71ed34b153bc8f2d8467578bbcff3999e4ef6787720e9f19
7
- data.tar.gz: 2f35f038b12503ab1a4e415cabb44e0f886601ef85877010ddc3863b1f62f3c73017b3c89e2c06d876a61312a09c69d6c59d8bae7c78a326b7891ba651ad7471
6
+ metadata.gz: 1277ee60c4cfed9ef92694f114fa8384546d45f6cfafaac3ea736a1d8d36fd9782228f969257c70a80c9d7a56afbb4442b540e12d653f2d992f2ada6f9f76487
7
+ data.tar.gz: 690a0f5c5668af181d8fd469fdc7c6a89810e7ee9c4161b9f5fba32537a5a6bcdd2e0f774fe94b6731fe5b658fd2a3e7b8d847a8b3c63bd44cf55d37a94d0a0e
Binary file
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Fastsort
4
- VERSION = "0.1.4"
4
+ VERSION = "0.1.5"
5
5
  end
data/lib/fastsort.rb CHANGED
@@ -171,17 +171,16 @@ module Fastsort
171
171
 
172
172
  def sort_array
173
173
  raise ArgumentError, "Array cannot be empty" if array.empty?
174
+ selection_sort(array)
174
175
  end
175
176
 
176
177
  def selection_sort(arr)
177
- for i in arr.length
178
+ arr.each_index do |i|
178
179
  min_idx = i
179
- for j in i..arr.length - 1
180
- if arr[j] < arr[min_idx]
181
- min_idx = j
182
- end
180
+ (i..arr.length - 1).each do |j|
181
+ min_idx = j if arr[j] < arr[min_idx]
183
182
  end
184
- swap_values(arr[i], arr[min_i])
183
+ swap_values(i, min_idx)
185
184
  end
186
185
  end
187
186
 
@@ -189,4 +188,34 @@ module Fastsort
189
188
  array[let_idx_a], array[let_idx_b] = array[let_idx_b], array[let_idx_a]
190
189
  end
191
190
  end
191
+
192
+ class BubbleSort
193
+ attr_accessor :array
194
+
195
+ def initialize(array)
196
+ raise ArgumentError, "Array cannot be nil" if array.nil?
197
+ @array = array
198
+ end
199
+
200
+ def sort_array
201
+ raise ArgumentError, "Array cannot be empty" if array.empty?
202
+ bubble_sort(array)
203
+ end
204
+
205
+ def bubble_sort(arr)
206
+ n = arr.length
207
+ loop do
208
+ swapped = false
209
+
210
+ (n - 1).times do |i|
211
+ if arr[i] > arr[i + 1]
212
+ arr[i], arr[i + 1] = arr[i + 1], arr[i]
213
+ swapped = true
214
+ end
215
+ end
216
+
217
+ break unless swapped
218
+ end
219
+ end
220
+ end
192
221
  end
data/main.rb CHANGED
@@ -39,13 +39,16 @@ sorter_001 = Fastsort::Quicksort.new(arr)
39
39
  sorter_002 = Fastsort::Mergesort.new(arr)
40
40
  sorter_003 = Fastsort::Sonicsort.new(arr)
41
41
  sorter_004 = Fastsort::SelectionSort.new(arr)
42
+ sorter_005 = Fastsort::BubbleSort.new(arr)
42
43
 
43
44
  sorter_001.sort_array
44
45
  sorter_002.sort_array
45
46
  sorter_003.sort_quick_and_merge
46
47
  sorter_004.sort_array
48
+ sorter_005.sort_array
47
49
 
48
- puts "Sorted Array Quicksort: #{sorter_001.array}"
49
- puts "Sorted Array Mergesort: #{sorter_002.array}"
50
- puts "Sorted Array Sonicsort: #{sorter_003.array}"
51
- puts "Sorted Array SelectionSort: #{sorter_004.array}"
50
+ # puts "Sorted Array Quicksort: #{sorter_001.array}"
51
+ # puts "Sorted Array Mergesort: #{sorter_002.array}"
52
+ # puts "Sorted Array Sonicsort: #{sorter_003.array}"
53
+ # puts "Sorted Array SelectionSort: #{sorter_004.array}"
54
+ puts "Sorted Array SelectionSort: #{sorter_005.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.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dants0
@@ -25,6 +25,7 @@ files:
25
25
  - fastsort-0.1.1.gem
26
26
  - fastsort-0.1.2.gem
27
27
  - fastsort-0.1.3.gem
28
+ - fastsort-0.1.4.gem
28
29
  - fastsort.gemspec
29
30
  - lib/fastsort.rb
30
31
  - lib/fastsort/version.rb