fastsort 0.1.2 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +8 -2
- data/fastsort-0.1.2.gem +0 -0
- data/fastsort-0.1.3.gem +0 -0
- data/lib/fastsort/version.rb +1 -1
- data/lib/fastsort.rb +74 -14
- data/main.rb +8 -2
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '08018e1e9579c8454b8cda1bfa90f63f979ffb37e948fdfc011835df2b9d1eea'
|
4
|
+
data.tar.gz: '09836fc38630639c43fa2bb7c475c34def609bb1e3d8dfeab799eb95035fedc3'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6e5d81f92f004e9add3cc6d4e14ee6e15d832a8fc5977b610304c82a4c3de3577e5540467d30328a71ed34b153bc8f2d8467578bbcff3999e4ef6787720e9f19
|
7
|
+
data.tar.gz: 2f35f038b12503ab1a4e415cabb44e0f886601ef85877010ddc3863b1f62f3c73017b3c89e2c06d876a61312a09c69d6c59d8bae7c78a326b7891ba651ad7471
|
data/README.md
CHANGED
@@ -30,14 +30,20 @@ arr = [
|
|
30
30
|
|
31
31
|
sorter001 = Fastsort::Quicksort.new(arr)
|
32
32
|
sorter002 = Fastsort::Mergesort.new(arr)
|
33
|
+
sorter003 = Fastsort::Sonicsort.new(arr)
|
34
|
+
sorter004 = Fastsort::SelectionSort.new(arr)
|
33
35
|
|
34
36
|
sorter001.sort_array
|
37
|
+
sorter002.sort_array
|
38
|
+
sorter003.sort_quick_and_merge
|
39
|
+
sorter004.sort_array
|
35
40
|
|
36
41
|
puts "Sorted Array: #{sorter001.array}"
|
37
42
|
puts "Sorted Array: #{sorter002.array}"
|
43
|
+
puts "Sorted Array: #{sorter003.array}"
|
44
|
+
puts "Sorted Array: #{sorter004.array}"
|
45
|
+
|
38
46
|
|
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
47
|
|
42
48
|
|
43
49
|
```
|
data/fastsort-0.1.2.gem
ADDED
Binary file
|
data/fastsort-0.1.3.gem
ADDED
Binary file
|
data/lib/fastsort/version.rb
CHANGED
data/lib/fastsort.rb
CHANGED
@@ -86,45 +86,105 @@ module Fastsort
|
|
86
86
|
attr_accessor :array
|
87
87
|
|
88
88
|
def initialize(array)
|
89
|
-
raise ArgumentError,
|
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,
|
95
|
-
|
94
|
+
raise ArgumentError, "Array cannot be empty" if array.empty?
|
95
|
+
quicksort(0, array.length - 1)
|
96
|
+
merge_sort(0, array.length - 1)
|
96
97
|
self
|
97
98
|
end
|
98
99
|
|
99
100
|
private
|
100
101
|
|
101
|
-
def
|
102
|
+
def quicksort(from_idx, to_idx)
|
102
103
|
return if from_idx >= to_idx
|
103
104
|
|
104
|
-
# Use Quicksort before Mergesort
|
105
105
|
pivot_idx = quicksort_partition(from_idx, to_idx)
|
106
106
|
|
107
|
-
|
108
|
-
|
109
|
-
sort_and_merge(pivot_idx + 1, to_idx)
|
107
|
+
quicksort(from_idx, pivot_idx - 1)
|
108
|
+
quicksort(pivot_idx + 1, to_idx)
|
110
109
|
end
|
111
110
|
|
112
111
|
def quicksort_partition(from_idx, to_idx)
|
113
|
-
pivot_idx =
|
114
|
-
|
112
|
+
pivot_idx = to_idx
|
113
|
+
pivot_value = array[pivot_idx]
|
115
114
|
|
116
|
-
|
117
|
-
|
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
|
118
125
|
swap_values(pointer_a_idx, pointer_b_idx)
|
119
126
|
pointer_b_idx += 1
|
120
127
|
end
|
121
|
-
pointer_a_idx += 1
|
122
128
|
end
|
123
129
|
|
124
|
-
swap_values(pointer_b_idx,
|
130
|
+
swap_values(pointer_b_idx, pivot_idx)
|
125
131
|
pointer_b_idx
|
126
132
|
end
|
127
133
|
|
134
|
+
def merge_sort(from_idx, to_idx)
|
135
|
+
return if from_idx >= to_idx
|
136
|
+
|
137
|
+
middle_idx = (from_idx + to_idx) / 2
|
138
|
+
|
139
|
+
merge_sort(from_idx, middle_idx)
|
140
|
+
merge_sort(middle_idx + 1, to_idx)
|
141
|
+
merge_subarrays(from_idx, middle_idx, to_idx)
|
142
|
+
end
|
143
|
+
|
144
|
+
def merge_subarrays(from_idx, middle_idx, to_idx)
|
145
|
+
left = array[from_idx..middle_idx]
|
146
|
+
right = array[(middle_idx + 1)..to_idx]
|
147
|
+
|
148
|
+
result = []
|
149
|
+
|
150
|
+
until left.empty? || right.empty?
|
151
|
+
result << (left.first <= right.first ? left.shift : right.shift)
|
152
|
+
end
|
153
|
+
|
154
|
+
result.concat(left).concat(right)
|
155
|
+
|
156
|
+
array[from_idx..to_idx] = result
|
157
|
+
end
|
158
|
+
|
159
|
+
def swap_values(let_idx_a, let_idx_b)
|
160
|
+
array[let_idx_a], array[let_idx_b] = array[let_idx_b], array[let_idx_a]
|
161
|
+
end
|
162
|
+
end
|
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
|
+
end
|
175
|
+
|
176
|
+
def selection_sort(arr)
|
177
|
+
for i in arr.length
|
178
|
+
min_idx = i
|
179
|
+
for j in i..arr.length - 1
|
180
|
+
if arr[j] < arr[min_idx]
|
181
|
+
min_idx = j
|
182
|
+
end
|
183
|
+
end
|
184
|
+
swap_values(arr[i], arr[min_i])
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
128
188
|
def swap_values(let_idx_a, let_idx_b)
|
129
189
|
array[let_idx_a], array[let_idx_b] = array[let_idx_b], array[let_idx_a]
|
130
190
|
end
|
data/main.rb
CHANGED
@@ -32,14 +32,20 @@ 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)
|
40
|
+
sorter_003 = Fastsort::Sonicsort.new(arr)
|
41
|
+
sorter_004 = Fastsort::SelectionSort.new(arr)
|
39
42
|
|
40
43
|
sorter_001.sort_array
|
41
44
|
sorter_002.sort_array
|
45
|
+
sorter_003.sort_quick_and_merge
|
46
|
+
sorter_004.sort_array
|
42
47
|
|
43
|
-
# puts "Sorted Array: #{sorter_001.array}"
|
44
48
|
puts "Sorted Array Quicksort: #{sorter_001.array}"
|
45
49
|
puts "Sorted Array Mergesort: #{sorter_002.array}"
|
50
|
+
puts "Sorted Array Sonicsort: #{sorter_003.array}"
|
51
|
+
puts "Sorted Array SelectionSort: #{sorter_004.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
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dants0
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
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,
|
@@ -23,6 +23,8 @@ files:
|
|
23
23
|
- Rakefile
|
24
24
|
- fastsort-0.1.0.gem
|
25
25
|
- fastsort-0.1.1.gem
|
26
|
+
- fastsort-0.1.2.gem
|
27
|
+
- fastsort-0.1.3.gem
|
26
28
|
- fastsort.gemspec
|
27
29
|
- lib/fastsort.rb
|
28
30
|
- lib/fastsort/version.rb
|