fastsort 0.1.4 → 0.1.6

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: 83e398190ca963503253e1d4ec1d0e22527bb349a1504a714b419da2a20b1923
4
+ data.tar.gz: 752909790d11f1af4c2ae5cde6933ba5c1f4a2080c2164a60c7401671df150bd
5
5
  SHA512:
6
- metadata.gz: 6e5d81f92f004e9add3cc6d4e14ee6e15d832a8fc5977b610304c82a4c3de3577e5540467d30328a71ed34b153bc8f2d8467578bbcff3999e4ef6787720e9f19
7
- data.tar.gz: 2f35f038b12503ab1a4e415cabb44e0f886601ef85877010ddc3863b1f62f3c73017b3c89e2c06d876a61312a09c69d6c59d8bae7c78a326b7891ba651ad7471
6
+ metadata.gz: 6cb3d695626d2d909423a2d09d1f5409bcaa8c7559593c82fca5093308642cf7a7d2c045032bc97c5bd89f016739b23f69455902b51b6b93b646e5d757477cd0
7
+ data.tar.gz: bc46f65b76cbf47cecc3e9336d8bb7bc72ac01dcb7f3aa783922be84612b10b6a415fccb8611e0ba349fac46a6c9dfae6e123b2cbb5d6b6285f4ed79082c0fed
data/README.md CHANGED
@@ -28,20 +28,31 @@ arr = [
28
28
  38, 87, 31, 93, 60, 53, 39, 59, 67, 97
29
29
  ]
30
30
 
31
+ bucket_size = 10
32
+
31
33
  sorter001 = Fastsort::Quicksort.new(arr)
32
34
  sorter002 = Fastsort::Mergesort.new(arr)
33
35
  sorter003 = Fastsort::Sonicsort.new(arr)
34
36
  sorter004 = Fastsort::SelectionSort.new(arr)
37
+ sorter004 = Fastsort::BubbleSort.new(arr)
38
+ sorter006 = Fastsort::BucketSort.new(arr, bucket_size)
39
+ sorter007 = Fastsort::InsertionSort.new(arr)
35
40
 
36
41
  sorter001.sort_array
37
42
  sorter002.sort_array
38
43
  sorter003.sort_quick_and_merge
39
44
  sorter004.sort_array
40
-
41
- puts "Sorted Array: #{sorter001.array}"
42
- puts "Sorted Array: #{sorter002.array}"
43
- puts "Sorted Array: #{sorter003.array}"
44
- puts "Sorted Array: #{sorter004.array}"
45
+ sorter005.sort_array
46
+ sorter006.sort_array
47
+ sorter007.sort_array
48
+
49
+ puts "Sorted Array Quicksort: #{sorter_001.array}"
50
+ puts "Sorted Array Mergesort: #{sorter_002.array}"
51
+ puts "Sorted Array Sonicsort: #{sorter_003.array}"
52
+ puts "Sorted Array SelectionSort: #{sorter_004.array}"
53
+ puts "Sorted Array BubbleSort: #{sorter_005.array}"
54
+ puts "Sorted Array BucketSort: #{sorter_006.array}"
55
+ puts "Sorted Array InsertionSort: #{sorter_007.array}"
45
56
 
46
57
 
47
58
 
Binary file
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.6"
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,134 @@ 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
221
+
222
+ class BucketSort
223
+ attr_accessor :array, :bucket_size
224
+
225
+ def initialize(array, bucket_size)
226
+ raise ArgumentError, "Array cannot be nil" if array.nil?
227
+ @bucket_size = bucket_size
228
+ @array = array
229
+ end
230
+
231
+ def sort_array
232
+ raise ArgumentError, "Array cannot be empty" if array.empty?
233
+ bucket_sort(array, bucket_size)
234
+ end
235
+
236
+ def insertion_sort(array)
237
+ i = 0
238
+ while i < array.length - 1
239
+ j = i
240
+ while j >= 0
241
+ if array[j] > array[j + 1]
242
+ temp = array[j]
243
+ array[j] = array[j + 1]
244
+ array[j + 1] = temp
245
+ end
246
+ j -= 1
247
+ end
248
+ i += 1
249
+ end
250
+ return array
251
+ end
252
+
253
+ def bucket_sort(array, bucket_size = 5)
254
+ if array.empty? || array.length == 1
255
+ return array
256
+ end
257
+
258
+ min_value = array[0]
259
+ max_value = array[0]
260
+
261
+ array.each do |item|
262
+ if item < min_value
263
+ min_value = item
264
+ elsif item > max_value
265
+ max_value = item
266
+ end
267
+ end
268
+
269
+ bucket_count = ((max_value - min_value) / bucket_size).floor + 1
270
+ bucket_array = Array.new(bucket_count)
271
+
272
+ (0..bucket_array.length - 1).each do |i|
273
+ bucket_array[i] = []
274
+ end
275
+
276
+ array.each do |item|
277
+ bucket_array[((item - min_value) / bucket_size).floor] << item
278
+ end
279
+
280
+ sorted_array = []
281
+
282
+ bucket_array.each do |bucket|
283
+ new = insertion_sort(bucket)
284
+ sorted_array.concat(new)
285
+ end
286
+
287
+ return sorted_array
288
+ end
289
+ end
290
+
291
+ class InsertionSort
292
+ attr_accessor :array
293
+
294
+ def initialize(array)
295
+ raise ArgumentError, "Array cannot be nil" if array.nil?
296
+ @array = array
297
+ end
298
+
299
+ def sort_array
300
+ raise ArgumentError, "Array cannot be empty" if array.empty?
301
+ insertion_sort(array)
302
+ end
303
+
304
+ def insertion_sort(arr)
305
+ i = 0
306
+ while i < arr.length - 1
307
+ j = i
308
+ while j >= 0
309
+ if arr[j] > arr[j + 1]
310
+ temp = arr[j]
311
+ arr[j] = arr[j + 1]
312
+ arr[j + 1] = temp
313
+ end
314
+ j -= 1
315
+ end
316
+ i += 1
317
+ end
318
+ return arr
319
+ end
320
+ end
192
321
  end
data/main.rb CHANGED
@@ -1,51 +1,29 @@
1
1
  require_relative "./lib/fastsort"
2
2
 
3
- arr = [
4
- 39, 15, 480, 392, 81, 354, 337, 378, 237, 497, 46, 19, 274, 296, 28,
5
- 376, 456, 261, 444, 151, 403, 308, 102, 410, 113, 357, 141, 316, 74, 387,
6
- 137, 235, 184, 212, 85, 217, 445, 79, 439, 147, 385, 121, 3, 482, 311,
7
- 188, 495, 172, 204, 349, 187, 216, 486, 500, 230, 209, 21, 336, 14, 43,
8
- 468, 432, 298, 452, 107, 450, 275, 271, 418, 284, 58, 281, 33, 163, 412,
9
- 200, 464, 415, 247, 87, 214, 397, 270, 48, 268, 301, 417, 285, 484, 467,
10
- 51, 303, 490, 341, 16, 250, 10, 390, 407, 114, 243, 364, 355, 227, 223,
11
- 54, 353, 408, 40, 155, 455, 282, 159, 166, 441, 479, 346, 171, 197, 148,
12
- 34, 383, 101, 248, 287, 286, 414, 88, 278, 347, 180, 305, 105, 210, 477,
13
- 324, 332, 457, 122, 12, 377, 176, 406, 433, 95, 312, 198, 426, 323, 474,
14
- 273, 319, 143, 288, 111, 371, 42, 276, 279, 466, 351, 36, 118, 195, 335,
15
- 225, 226, 420, 59, 338, 396, 320, 361, 199, 156, 116, 401, 183, 469, 134,
16
- 241, 249, 164, 49, 146, 8, 437, 402, 186, 221, 173, 233, 462, 232, 253,
17
- 428, 391, 53, 11, 443, 394, 252, 98, 485, 196, 32, 60, 25, 129, 423,
18
- 297, 472, 66, 367, 110, 307, 80, 429, 30, 470, 321, 290, 368, 475, 37,
19
- 218, 61, 7, 175, 86, 152, 90, 55, 306, 348, 84, 63, 133, 29, 379,
20
- 263, 393, 65, 372, 427, 191, 157, 202, 31, 89, 262, 375, 64, 496, 24,
21
- 300, 234, 389, 131, 140, 365, 236, 395, 295, 72, 494, 435, 499, 483, 388,
22
- 100, 340, 96, 224, 405, 219, 453, 309, 266, 22, 206, 239, 75, 91, 330,
23
- 70, 358, 194, 478, 104, 352, 77, 109, 449, 23, 154, 374, 491, 257, 126,
24
- 422, 26, 119, 62, 192, 292, 493, 460, 265, 313, 471, 386, 179, 447, 343,
25
- 381, 127, 165, 400, 315, 327, 128, 366, 57, 299, 434, 463, 329, 255, 359,
26
- 83, 258, 310, 360, 38, 404, 231, 20, 424, 207, 161, 325, 260, 442, 17,
27
- 244, 246, 103, 451, 242, 318, 106, 44, 419, 6, 4, 35, 213, 18, 293,
28
- 132, 380, 454, 291, 283, 162, 384, 136, 362, 123, 458, 92, 373, 150, 9,
29
- 448, 481, 99, 125, 120, 302, 93, 459, 160, 398, 425, 182, 174, 189, 78,
30
- 145, 135, 193, 149, 228, 256, 1, 344, 68, 5, 178, 177, 208, 326, 473,
31
- 185, 322, 245, 289, 2, 267, 82, 205, 169, 269, 71, 238, 416, 56, 446,
32
- 280, 488, 411, 363, 436, 41, 356, 399, 304, 229, 254, 430, 259, 203, 201,
33
- 115, 350, 370, 124, 382, 421, 498, 139, 345, 438, 69, 431, 27, 317, 215,
34
- 47, 67, 251, 272, 144, 333, 220, 168, 465, 328, 130, 170, 294, 167, 52,
35
- 409, 117, 487, 138, 461, 489,
36
- ]
3
+ arr = [1, 100, 20, 30, 12, 7, 6, 10]
4
+
5
+ bucket_size = 2
37
6
 
38
7
  sorter_001 = Fastsort::Quicksort.new(arr)
39
8
  sorter_002 = Fastsort::Mergesort.new(arr)
40
9
  sorter_003 = Fastsort::Sonicsort.new(arr)
41
10
  sorter_004 = Fastsort::SelectionSort.new(arr)
11
+ sorter_005 = Fastsort::BubbleSort.new(arr)
12
+ sorter_006 = Fastsort::BucketSort.new(arr, bucket_size)
13
+ sorter_007 = Fastsort::InsertionSort.new(arr)
42
14
 
43
- sorter_001.sort_array
44
- sorter_002.sort_array
45
- sorter_003.sort_quick_and_merge
46
- sorter_004.sort_array
15
+ # sorter_001.sort_array
16
+ # sorter_002.sort_array
17
+ # sorter_003.sort_quick_and_merge
18
+ # sorter_004.sort_array
19
+ # sorter_005.sort_array
20
+ sorter_006.sort_array
21
+ sorter_007.sort_array
47
22
 
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}"
23
+ # puts "Sorted Array Quicksort: #{sorter_001.array}"
24
+ # puts "Sorted Array Mergesort: #{sorter_002.array}"
25
+ # puts "Sorted Array Sonicsort: #{sorter_003.array}"
26
+ # puts "Sorted Array SelectionSort: #{sorter_004.array}"
27
+ # puts "Sorted Array BubbleSort: #{sorter_005.array}"
28
+ puts "Sorted Array BucketSort: #{sorter_006.array}"
29
+ puts "Sorted Array InsertionSort: #{sorter_007.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.4
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dants0
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-01-03 00:00:00.000000000 Z
11
+ date: 2024-01-05 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,
@@ -25,6 +25,8 @@ 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
29
+ - fastsort-0.1.5.gem
28
30
  - fastsort.gemspec
29
31
  - lib/fastsort.rb
30
32
  - lib/fastsort/version.rb