fastsort 0.1.3 → 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: 9c7b13967306e965a337cf00b9b8debca59787d6ee788af65b3242e606507ae2
4
- data.tar.gz: 04a5570f6b32a7eea2f18e22e174ef53591e7968ff9b4fe1bc5222148c1f070d
3
+ metadata.gz: dc6da324d5e82ea3fc7e86f3c1867c4bfb71fffde7f40c61adc91993bf814793
4
+ data.tar.gz: f029598b6363d8d10f6b0b561cf03d3570d4e7d8d69dcf340802dda72b89a2a7
5
5
  SHA512:
6
- metadata.gz: 961c2cc4b1a10d4dbd97408546d02294ffa884a784da0655cc96a4cb74848ff50aa757c71fe3e18ec102e706bce77bd99c14e8d1d34a31af5a2180aae57ec291
7
- data.tar.gz: 17ffc5adb2895eb51c53f45512771111be4a9c4f50892fdf63d61e7ed8489b7427d12ef7386ebb46d3b104674ed1ea5894f4a0708038f688e7019b6ea742e767
6
+ metadata.gz: 1277ee60c4cfed9ef92694f114fa8384546d45f6cfafaac3ea736a1d8d36fd9782228f969257c70a80c9d7a56afbb4442b540e12d653f2d992f2ada6f9f76487
7
+ data.tar.gz: 690a0f5c5668af181d8fd469fdc7c6a89810e7ee9c4161b9f5fba32537a5a6bcdd2e0f774fe94b6731fe5b658fd2a3e7b8d847a8b3c63bd44cf55d37a94d0a0e
data/README.md CHANGED
@@ -31,14 +31,17 @@ arr = [
31
31
  sorter001 = Fastsort::Quicksort.new(arr)
32
32
  sorter002 = Fastsort::Mergesort.new(arr)
33
33
  sorter003 = Fastsort::Sonicsort.new(arr)
34
+ sorter004 = Fastsort::SelectionSort.new(arr)
34
35
 
35
36
  sorter001.sort_array
36
37
  sorter002.sort_array
37
- sorter003.sort_array
38
+ sorter003.sort_quick_and_merge
39
+ sorter004.sort_array
38
40
 
39
41
  puts "Sorted Array: #{sorter001.array}"
40
42
  puts "Sorted Array: #{sorter002.array}"
41
43
  puts "Sorted Array: #{sorter003.array}"
44
+ puts "Sorted Array: #{sorter004.array}"
42
45
 
43
46
 
44
47
 
Binary file
Binary file
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Fastsort
4
- VERSION = "0.1.3"
4
+ VERSION = "0.1.5"
5
5
  end
data/lib/fastsort.rb CHANGED
@@ -86,12 +86,12 @@ module Fastsort
86
86
  attr_accessor :array
87
87
 
88
88
  def initialize(array)
89
- raise ArgumentError, 'Array cannot be nil' if array.nil?
89
+ raise ArgumentError, "Array cannot be nil" if array.nil?
90
90
  @array = array
91
91
  end
92
92
 
93
93
  def sort_quick_and_merge
94
- raise ArgumentError, 'Array cannot be empty' if array.empty?
94
+ raise ArgumentError, "Array cannot be empty" if array.empty?
95
95
  quicksort(0, array.length - 1)
96
96
  merge_sort(0, array.length - 1)
97
97
  self
@@ -112,7 +112,7 @@ module Fastsort
112
112
  pivot_idx = to_idx
113
113
  pivot_value = array[pivot_idx]
114
114
 
115
- raise ArgumentError, 'Invalid pivot value' if pivot_value.nil?
115
+ raise ArgumentError, "Invalid pivot value" if pivot_value.nil?
116
116
 
117
117
  pointer_b_idx = from_idx
118
118
 
@@ -131,7 +131,6 @@ module Fastsort
131
131
  pointer_b_idx
132
132
  end
133
133
 
134
-
135
134
  def merge_sort(from_idx, to_idx)
136
135
  return if from_idx >= to_idx
137
136
 
@@ -162,4 +161,61 @@ module Fastsort
162
161
  end
163
162
  end
164
163
 
164
+ class SelectionSort
165
+ attr_accessor :array
166
+
167
+ def initialize(array)
168
+ raise ArgumentError, "Array cannot be nil" if array.nil?
169
+ @array = array
170
+ end
171
+
172
+ def sort_array
173
+ raise ArgumentError, "Array cannot be empty" if array.empty?
174
+ selection_sort(array)
175
+ end
176
+
177
+ def selection_sort(arr)
178
+ arr.each_index do |i|
179
+ min_idx = i
180
+ (i..arr.length - 1).each do |j|
181
+ min_idx = j if arr[j] < arr[min_idx]
182
+ end
183
+ swap_values(i, min_idx)
184
+ end
185
+ end
186
+
187
+ def swap_values(let_idx_a, let_idx_b)
188
+ array[let_idx_a], array[let_idx_b] = array[let_idx_b], array[let_idx_a]
189
+ end
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
165
221
  end
data/main.rb CHANGED
@@ -32,16 +32,23 @@ arr = [
32
32
  280, 488, 411, 363, 436, 41, 356, 399, 304, 229, 254, 430, 259, 203, 201,
33
33
  115, 350, 370, 124, 382, 421, 498, 139, 345, 438, 69, 431, 27, 317, 215,
34
34
  47, 67, 251, 272, 144, 333, 220, 168, 465, 328, 130, 170, 294, 167, 52,
35
- 409, 117, 487, 138, 461, 489]
35
+ 409, 117, 487, 138, 461, 489,
36
+ ]
36
37
 
37
38
  sorter_001 = Fastsort::Quicksort.new(arr)
38
39
  sorter_002 = Fastsort::Mergesort.new(arr)
39
40
  sorter_003 = Fastsort::Sonicsort.new(arr)
41
+ sorter_004 = Fastsort::SelectionSort.new(arr)
42
+ sorter_005 = Fastsort::BubbleSort.new(arr)
40
43
 
41
44
  sorter_001.sort_array
42
45
  sorter_002.sort_array
43
46
  sorter_003.sort_quick_and_merge
47
+ sorter_004.sort_array
48
+ sorter_005.sort_array
44
49
 
45
- puts "Sorted Array Quicksort: #{sorter_001.array}"
46
- puts "Sorted Array Mergesort: #{sorter_002.array}"
47
- puts "Sorted Array Sonicsort: #{sorter_003.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,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fastsort
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dants0
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-12-29 00:00:00.000000000 Z
11
+ date: 2024-01-03 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Designed to efficiently organize the elements of an array in a specific
14
14
  order. Sorting is a fundamental operation in computer science and data processing,
@@ -24,6 +24,8 @@ files:
24
24
  - fastsort-0.1.0.gem
25
25
  - fastsort-0.1.1.gem
26
26
  - fastsort-0.1.2.gem
27
+ - fastsort-0.1.3.gem
28
+ - fastsort-0.1.4.gem
27
29
  - fastsort.gemspec
28
30
  - lib/fastsort.rb
29
31
  - lib/fastsort/version.rb