fastsort 0.1.1 → 0.1.2

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: 5138e2c24fc0375ad7ce6ab52e0d2687c92298fd27ce998d6aaf718c2c43a846
4
+ data.tar.gz: 640b5566de987806787c1d8f38d1f77d8a6c476f0caf37077989fff8533d159e
5
5
  SHA512:
6
- metadata.gz: 380b01d08738b1293bec578ffee49a64e73298d63bcdcd1b35524b1949a85a4dc743e08da69326429cb6591cad71bc48a09510914e0f7b4f2e88d107e67a9af0
7
- data.tar.gz: b1ba27cb343db13bc6fd87b7296f7f41d021cc47fac0031213c30256de567ffa7f512dd2a8d4a9955adfb0e761e66031ddad44ab288845158b82b40909061485
6
+ metadata.gz: dc0abc389f64ee1fbea1b2e74643e130a87c69f3cdee81a904eaf7adfa0561f530ee43ca79e6a5d8eb40ca5812385a9a190a699b1acf6fdddb94a84b8ff25954
7
+ data.tar.gz: 713c5b8999af7a93a7f466c8ea2eb17ec36479f94d91736c1b8900efe22d689e7295720d3a2e992d5458f79539344ae6ad9053421f90529675f702224d08b32c
data/README.md CHANGED
@@ -28,11 +28,18 @@ 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
+
34
+ sorter001.sort_array
35
+
36
+ puts "Sorted Array: #{sorter001.array}"
37
+ puts "Sorted Array: #{sorter002.array}"
38
+
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]
32
41
 
33
- sorter.sort_array
34
42
 
35
- puts "Sorted Array: #{sorter.array}"
36
43
  ```
37
44
 
38
45
  ## Development
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.2"
5
5
  end
data/lib/fastsort.rb CHANGED
@@ -81,4 +81,52 @@ 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
+ sort_and_merge(0, array.length - 1)
96
+ self
97
+ end
98
+
99
+ private
100
+
101
+ def sort_and_merge(from_idx, to_idx)
102
+ return if from_idx >= to_idx
103
+
104
+ # Use Quicksort before Mergesort
105
+ pivot_idx = quicksort_partition(from_idx, to_idx)
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)
110
+ end
111
+
112
+ def quicksort_partition(from_idx, to_idx)
113
+ pivot_idx = array[to_idx]
114
+ pointer_a_idx = pointer_b_idx = from_idx
115
+
116
+ while pointer_a_idx < to_idx
117
+ if array[pointer_a_idx] <= pivot_idx
118
+ swap_values(pointer_a_idx, pointer_b_idx)
119
+ pointer_b_idx += 1
120
+ end
121
+ pointer_a_idx += 1
122
+ end
123
+
124
+ swap_values(pointer_b_idx, to_idx)
125
+ pointer_b_idx
126
+ end
127
+
128
+ def swap_values(let_idx_a, let_idx_b)
129
+ array[let_idx_a], array[let_idx_b] = array[let_idx_b], array[let_idx_a]
130
+ end
131
+ end
84
132
  end
data/main.rb CHANGED
@@ -1,12 +1,38 @@
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)
@@ -15,4 +41,5 @@ sorter_001.sort_array
15
41
  sorter_002.sort_array
16
42
 
17
43
  # puts "Sorted Array: #{sorter_001.array}"
18
- puts "Sorted Array: #{sorter_002.array}"
44
+ puts "Sorted Array Quicksort: #{sorter_001.array}"
45
+ puts "Sorted Array Mergesort: #{sorter_002.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.2
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,7 @@ files:
22
22
  - README.md
23
23
  - Rakefile
24
24
  - fastsort-0.1.0.gem
25
+ - fastsort-0.1.1.gem
25
26
  - fastsort.gemspec
26
27
  - lib/fastsort.rb
27
28
  - lib/fastsort/version.rb