fastsort 0.1.1 → 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: 8d86a8ff31ddc7b969d1a9b917813710b69f8ad0f2aef649f7faba7afcfd751a
4
- data.tar.gz: 3eca0655b2567839b287a05b322b51862409a2a62c5ce2b0f79afcd0df3729d9
3
+ metadata.gz: 9c7b13967306e965a337cf00b9b8debca59787d6ee788af65b3242e606507ae2
4
+ data.tar.gz: 04a5570f6b32a7eea2f18e22e174ef53591e7968ff9b4fe1bc5222148c1f070d
5
5
  SHA512:
6
- metadata.gz: 380b01d08738b1293bec578ffee49a64e73298d63bcdcd1b35524b1949a85a4dc743e08da69326429cb6591cad71bc48a09510914e0f7b4f2e88d107e67a9af0
7
- data.tar.gz: b1ba27cb343db13bc6fd87b7296f7f41d021cc47fac0031213c30256de567ffa7f512dd2a8d4a9955adfb0e761e66031ddad44ab288845158b82b40909061485
6
+ metadata.gz: 961c2cc4b1a10d4dbd97408546d02294ffa884a784da0655cc96a4cb74848ff50aa757c71fe3e18ec102e706bce77bd99c14e8d1d34a31af5a2180aae57ec291
7
+ data.tar.gz: 17ffc5adb2895eb51c53f45512771111be4a9c4f50892fdf63d61e7ed8489b7427d12ef7386ebb46d3b104674ed1ea5894f4a0708038f688e7019b6ea742e767
data/README.md CHANGED
@@ -28,11 +28,21 @@ arr = [
28
28
  38, 87, 31, 93, 60, 53, 39, 59, 67, 97
29
29
  ]
30
30
 
31
- sorter = Fastsort::Quicksort.new(arr)
31
+ sorter001 = Fastsort::Quicksort.new(arr)
32
+ sorter002 = Fastsort::Mergesort.new(arr)
33
+ sorter003 = Fastsort::Sonicsort.new(arr)
34
+
35
+ sorter001.sort_array
36
+ sorter002.sort_array
37
+ sorter003.sort_array
38
+
39
+ puts "Sorted Array: #{sorter001.array}"
40
+ puts "Sorted Array: #{sorter002.array}"
41
+ puts "Sorted Array: #{sorter003.array}"
42
+
43
+
32
44
 
33
- sorter.sort_array
34
45
 
35
- puts "Sorted Array: #{sorter.array}"
36
46
  ```
37
47
 
38
48
  ## Development
Binary file
Binary file
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Fastsort
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.3"
5
5
  end
data/lib/fastsort.rb CHANGED
@@ -81,4 +81,85 @@ module Fastsort
81
81
  result
82
82
  end
83
83
  end
84
+
85
+ class Sonicsort
86
+ attr_accessor :array
87
+
88
+ def initialize(array)
89
+ raise ArgumentError, 'Array cannot be nil' if array.nil?
90
+ @array = array
91
+ end
92
+
93
+ def sort_quick_and_merge
94
+ raise ArgumentError, 'Array cannot be empty' if array.empty?
95
+ quicksort(0, array.length - 1)
96
+ merge_sort(0, array.length - 1)
97
+ self
98
+ end
99
+
100
+ private
101
+
102
+ def quicksort(from_idx, to_idx)
103
+ return if from_idx >= to_idx
104
+
105
+ pivot_idx = quicksort_partition(from_idx, to_idx)
106
+
107
+ quicksort(from_idx, pivot_idx - 1)
108
+ quicksort(pivot_idx + 1, to_idx)
109
+ end
110
+
111
+ def quicksort_partition(from_idx, to_idx)
112
+ pivot_idx = to_idx
113
+ pivot_value = array[pivot_idx]
114
+
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
125
+ swap_values(pointer_a_idx, pointer_b_idx)
126
+ pointer_b_idx += 1
127
+ end
128
+ end
129
+
130
+ swap_values(pointer_b_idx, pivot_idx)
131
+ pointer_b_idx
132
+ end
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
+
160
+ def swap_values(let_idx_a, let_idx_b)
161
+ array[let_idx_a], array[let_idx_b] = array[let_idx_b], array[let_idx_a]
162
+ end
163
+ end
164
+
84
165
  end
data/main.rb CHANGED
@@ -1,18 +1,47 @@
1
1
  require_relative "./lib/fastsort"
2
2
 
3
3
  arr = [
4
- 75, 79, 21, 30, 12, 85, 24, 6, 66, 69,
5
- 92, 98, 18, 57, 40, 71, 92, 14, 1, 79,
6
- 15, 21, 55, 83, 45, 34, 25, 67, 9, 75,
7
- 83, 75, 99, 10, 41, 63, 79, 22, 80, 7,
8
- 38, 87, 31, 93, 60, 53, 39, 59, 67, 97,
9
- ]
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]
10
36
 
11
37
  sorter_001 = Fastsort::Quicksort.new(arr)
12
38
  sorter_002 = Fastsort::Mergesort.new(arr)
39
+ sorter_003 = Fastsort::Sonicsort.new(arr)
13
40
 
14
41
  sorter_001.sort_array
15
42
  sorter_002.sort_array
43
+ sorter_003.sort_quick_and_merge
16
44
 
17
- # puts "Sorted Array: #{sorter_001.array}"
18
- puts "Sorted Array: #{sorter_002.array}"
45
+ puts "Sorted Array Quicksort: #{sorter_001.array}"
46
+ puts "Sorted Array Mergesort: #{sorter_002.array}"
47
+ puts "Sorted Array Sonicsort: #{sorter_003.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.1
4
+ version: 0.1.3
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-28 00:00:00.000000000 Z
11
+ date: 2023-12-29 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,
@@ -22,6 +22,8 @@ files:
22
22
  - README.md
23
23
  - Rakefile
24
24
  - fastsort-0.1.0.gem
25
+ - fastsort-0.1.1.gem
26
+ - fastsort-0.1.2.gem
25
27
  - fastsort.gemspec
26
28
  - lib/fastsort.rb
27
29
  - lib/fastsort/version.rb