fastsort 0.1.0 → 0.1.2

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: 12702d25d0475df203ee081b543e8a9d715b086fd561b0f5c550cf0848fa4de2
4
- data.tar.gz: 8fba45cfd6374245cde79cac4c26f7856a726d5a35081cbbf67e749da589d863
3
+ metadata.gz: 5138e2c24fc0375ad7ce6ab52e0d2687c92298fd27ce998d6aaf718c2c43a846
4
+ data.tar.gz: 640b5566de987806787c1d8f38d1f77d8a6c476f0caf37077989fff8533d159e
5
5
  SHA512:
6
- metadata.gz: d45f75d7834d313ea818cb70ba2d0d329257fe91700f30f090daace3d00b8478f15a29276b4ae5794e6777bb292be7f54cdb62cba9b426a5e829ffb71d1e4615
7
- data.tar.gz: 83ae98469a47a75ef41c80c0ec14ec24b065c055419f189ff454e3fc843568185499caed3776941251e88173eff46668580e79ef552e9162ba38f6d71e93fe5b
6
+ metadata.gz: dc0abc389f64ee1fbea1b2e74643e130a87c69f3cdee81a904eaf7adfa0561f530ee43ca79e6a5d8eb40ca5812385a9a190a699b1acf6fdddb94a84b8ff25954
7
+ data.tar.gz: 713c5b8999af7a93a7f466c8ea2eb17ec36479f94d91736c1b8900efe22d689e7295720d3a2e992d5458f79539344ae6ad9053421f90529675f702224d08b32c
data/README.md CHANGED
@@ -1,42 +1,49 @@
1
- # Fastsort
2
-
3
- Fastsort is a Ruby gem that provides an efficient sorting algorithm for arrays.
4
-
5
- ## Installation
6
-
7
- Add this line to your application's Gemfile:
8
-
9
- ```ruby
10
- $ gem install 'fastsort'
11
- $ bundle install
12
- ```
13
-
14
- # Gemfile
15
-
16
- gem 'fastsort'
17
-
18
- ## Example usage
19
-
20
- ```bash
21
- require 'fastsort'
22
-
23
- arr = [
24
- 75, 79, 21, 30, 12, 85, 24, 6, 66, 69,
25
- 92, 98, 18, 57, 40, 71, 92, 14, 1, 79,
26
- 15, 21, 55, 83, 45, 34, 25, 67, 9, 75,
27
- 83, 75, 99, 10, 41, 63, 79, 22, 80, 7,
28
- 38, 87, 31, 93, 60, 53, 39, 59, 67, 97
29
- ]
30
-
31
- sorter = Fastsort::Quicksort.new(arr)
32
-
33
- sorter.sort_array
34
-
35
- puts "Sorted Array: #{sorter.array}"
36
- ```
37
-
38
- ## Development
39
- After checking out the repo, run bin/setup to install dependencies. You can also run bin/console for an interactive prompt that will allow you to experiment.
40
-
41
- ## Contributing
1
+ # Fastsort
2
+
3
+ Fastsort is a Ruby gem that provides an efficient sorting algorithm for arrays.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ $ gem install 'fastsort'
11
+ $ bundle install
12
+ ```
13
+
14
+ ## TODO
15
+ Add more efficient sorting algorithms for arrays and arrays of objects...
16
+
17
+
18
+ ## Example usage
19
+
20
+ ```bash
21
+ require 'fastsort'
22
+
23
+ arr = [
24
+ 75, 79, 21, 30, 12, 85, 24, 6, 66, 69,
25
+ 92, 98, 18, 57, 40, 71, 92, 14, 1, 79,
26
+ 15, 21, 55, 83, 45, 34, 25, 67, 9, 75,
27
+ 83, 75, 99, 10, 41, 63, 79, 22, 80, 7,
28
+ 38, 87, 31, 93, 60, 53, 39, 59, 67, 97
29
+ ]
30
+
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]
41
+
42
+
43
+ ```
44
+
45
+ ## Development
46
+ After checking out the repo, run bin/setup to install dependencies. You can also run bin/console for an interactive prompt that will allow you to experiment.
47
+
48
+ ## Contributing
42
49
  Bug reports and pull requests are welcome on GitHub at https://github.com/dants0/fastsort.
data/Rakefile CHANGED
@@ -1,4 +1,4 @@
1
- # frozen_string_literal: true
2
-
3
- require "bundler/gem_tasks"
4
- task default: %i[]
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
Binary file
Binary file
data/fastsort.gemspec CHANGED
@@ -1,39 +1,39 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "lib/fastsort/version"
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = "fastsort"
7
- spec.version = Fastsort::VERSION
8
- spec.authors = ["Dants0"]
9
- spec.email = ["gui.dg1@hotmail.com"]
10
-
11
- spec.summary = "a fast array sorter"
12
- spec.description = "Designed to efficiently organize the elements of an array in a specific order. Sorting is a fundamental operation in computer science and data processing, and the performance of a sorting algorithm is crucial."
13
- spec.homepage = "https://rubygems.org/gems/fastsort"
14
- spec.required_ruby_version = ">= 2.6.0"
15
-
16
- spec.metadata["allowed_push_host"] = "https://rubygems.org"
17
-
18
- spec.metadata["homepage_uri"] = spec.homepage
19
- spec.metadata["source_code_uri"] = "https://github.com/Dants0/fastsort"
20
- spec.metadata["changelog_uri"] = "https://github.com/Dants0/fastsort"
21
-
22
- # Specify which files should be added to the gem when it is released.
23
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
- spec.files = Dir.chdir(__dir__) do
25
- `git ls-files -z`.split("\x0").reject do |f|
26
- (File.expand_path(f) == __FILE__) ||
27
- f.start_with?(*%w[bin/ test/ spec/ features/ .git appveyor Gemfile])
28
- end
29
- end
30
- spec.bindir = "exe"
31
- spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
- spec.require_paths = ["lib"]
33
-
34
- # Uncomment to register a new dependency of your gem
35
- # spec.add_dependency "example-gem", "~> 1.0"
36
-
37
- # For more information and examples about making a new gem, check out our
38
- # guide at: https://bundler.io/guides/creating_gem.html
39
- end
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/fastsort/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "fastsort"
7
+ spec.version = Fastsort::VERSION
8
+ spec.authors = ["Dants0"]
9
+ spec.email = ["gui.dg1@hotmail.com"]
10
+
11
+ spec.summary = "a fast array sorter"
12
+ spec.description = "Designed to efficiently organize the elements of an array in a specific order. Sorting is a fundamental operation in computer science and data processing, and the performance of a sorting algorithm is crucial."
13
+ spec.homepage = "https://rubygems.org/gems/fastsort"
14
+ spec.required_ruby_version = ">= 2.6.0"
15
+
16
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "https://github.com/Dants0/fastsort"
20
+ spec.metadata["changelog_uri"] = "https://github.com/Dants0/fastsort"
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(__dir__) do
25
+ `git ls-files -z`.split("\x0").reject do |f|
26
+ (File.expand_path(f) == __FILE__) ||
27
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git appveyor Gemfile])
28
+ end
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ # Uncomment to register a new dependency of your gem
35
+ # spec.add_dependency "example-gem", "~> 1.0"
36
+
37
+ # For more information and examples about making a new gem, check out our
38
+ # guide at: https://bundler.io/guides/creating_gem.html
39
+ end
@@ -1,5 +1,5 @@
1
- # frozen_string_literal: true
2
-
3
- module Fastsort
4
- VERSION = "0.1.0"
5
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Fastsort
4
+ VERSION = "0.1.2"
5
+ end
data/lib/fastsort.rb CHANGED
@@ -40,4 +40,93 @@ module Fastsort
40
40
  array[idx_a], array[idx_b] = array[idx_b], array[idx_a]
41
41
  end
42
42
  end
43
+
44
+ class Mergesort
45
+ attr_accessor :array
46
+
47
+ def initialize(array)
48
+ @array = array
49
+ end
50
+
51
+ def sort_array
52
+ return array if array.length <= 1
53
+
54
+ middle = array.length / 2
55
+ left_half = array[0...middle]
56
+ right_half = array[middle..-1]
57
+
58
+ left_half = Mergesort.new(left_half).sort_array
59
+ right_half = Mergesort.new(right_half).sort_array
60
+
61
+ merge(left_half, right_half)
62
+ end
63
+
64
+ def merge(left, right)
65
+ result = []
66
+ left_idx = right_idx = 0
67
+
68
+ while left_idx < left.length && right_idx < right.length
69
+ if left[left_idx] < right[right_idx]
70
+ result << left[left_idx]
71
+ left_idx += 1
72
+ else
73
+ result << right[right_idx]
74
+ right_idx += 1
75
+ end
76
+ end
77
+
78
+ result.concat(left[left_idx..-1]) if left_idx < left.length
79
+ result.concat(right[right_idx..-1]) if right_idx < right.length
80
+
81
+ result
82
+ end
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
43
132
  end
data/main.rb CHANGED
@@ -1,15 +1,45 @@
1
- require_relative './lib/fastsort'
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
- sorter = Fastsort::Quicksort.new(arr)
37
+ sorter_001 = Fastsort::Quicksort.new(arr)
38
+ sorter_002 = Fastsort::Mergesort.new(arr)
12
39
 
13
- sorter.sort_array
40
+ sorter_001.sort_array
41
+ sorter_002.sort_array
14
42
 
15
- puts "Sorted Array: #{sorter.array}"
43
+ # puts "Sorted Array: #{sorter_001.array}"
44
+ puts "Sorted Array Quicksort: #{sorter_001.array}"
45
+ puts "Sorted Array Mergesort: #{sorter_002.array}"
data/sig/fastsort.rbs CHANGED
@@ -1,4 +1,4 @@
1
- module Fastsort
2
- VERSION: String
3
- # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
- end
1
+ module Fastsort
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
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.0
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-27 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,
@@ -21,6 +21,8 @@ extra_rdoc_files: []
21
21
  files:
22
22
  - README.md
23
23
  - Rakefile
24
+ - fastsort-0.1.0.gem
25
+ - fastsort-0.1.1.gem
24
26
  - fastsort.gemspec
25
27
  - lib/fastsort.rb
26
28
  - lib/fastsort/version.rb