sorts_ESN 0.0.1
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 +7 -0
- data/lib/sorts_ESN.rb +127 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: be422d225bad294568066ff75c41fee7ec58ce5c
|
4
|
+
data.tar.gz: 94478d84b0df77ea8dbedf6f939500c324ce954a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4ff47f216adae873781dfce19bd908a6adf8485031b761a3708409c4d485e3053dddc56f6044d4aaa7ff87aa6c11f64aa3d28a03bdc78bd3ea82e772dc8c99f3
|
7
|
+
data.tar.gz: 2b968a9b23b9382dd24e10a21ac3fabf4f5cc45a4a75433aa8f4ec3c1f8085b60a41be73280561f5553aaa33f20cc8c6fd6f1387a385acba36cd88b919949413
|
data/lib/sorts_ESN.rb
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
class Sorts_ESN
|
2
|
+
|
3
|
+
# insertion sort!
|
4
|
+
# ~ best O(n)
|
5
|
+
# ~ average O(n^2)
|
6
|
+
# ~ worst O(n^2)
|
7
|
+
# stable sort
|
8
|
+
def self.insertion_sort (list)
|
9
|
+
|
10
|
+
# get to the sorting
|
11
|
+
list.each_with_index do |v,i|
|
12
|
+
|
13
|
+
# value and current index
|
14
|
+
@val = v
|
15
|
+
@hole = i
|
16
|
+
|
17
|
+
# iterate and swap as needed
|
18
|
+
while (@hole > 0 && @val < list[@hole -1])
|
19
|
+
list[@hole] = list[@hole -1]
|
20
|
+
@hole = @hole - 1
|
21
|
+
end
|
22
|
+
|
23
|
+
# assign
|
24
|
+
list[@hole] = @val
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# merge sort!
|
29
|
+
# ~ best O(n log n)
|
30
|
+
# ~ average O(n log n)
|
31
|
+
# ~ worst O(n log n)
|
32
|
+
# stable sort
|
33
|
+
def self.merge_sort (list)
|
34
|
+
|
35
|
+
# break case
|
36
|
+
return list if list.size <= 1
|
37
|
+
|
38
|
+
# find middle of list
|
39
|
+
@mid = (list.size / 2).round
|
40
|
+
# take first half
|
41
|
+
@l = list.take(@mid)
|
42
|
+
# take second half
|
43
|
+
@r = list.drop(@mid)
|
44
|
+
|
45
|
+
# recurse and join
|
46
|
+
join(merge_sort(l), merge_sort(r))
|
47
|
+
end
|
48
|
+
|
49
|
+
# quick sort!
|
50
|
+
# ~ best O(n log n)
|
51
|
+
# ~ normal O(n log n)
|
52
|
+
# ~ worst O(n^2)
|
53
|
+
# unstable sort
|
54
|
+
def self.quick_sort(list)
|
55
|
+
|
56
|
+
# return empty array if list is empty
|
57
|
+
return [] if list.size == 0
|
58
|
+
|
59
|
+
# create pointer to list
|
60
|
+
@x, *xs = *list
|
61
|
+
|
62
|
+
# split list
|
63
|
+
@less, @more = xs.partition{|y| y < x}
|
64
|
+
|
65
|
+
# recurse
|
66
|
+
quick_sort(@less) + [@x] + quick_sort(@more)
|
67
|
+
end
|
68
|
+
|
69
|
+
# bubble sort!
|
70
|
+
# ~ best O(n)
|
71
|
+
# ~ normal O(n^2)
|
72
|
+
# ~ worst O(n^2)
|
73
|
+
# stable sort
|
74
|
+
def self.bubble_sort (list)
|
75
|
+
|
76
|
+
# return if empty
|
77
|
+
return [] if list.size == 0
|
78
|
+
|
79
|
+
# sentinels (Ruby, y u no normal for loop?)
|
80
|
+
@i = 0
|
81
|
+
@j = 0
|
82
|
+
@len = list.size - 1
|
83
|
+
|
84
|
+
# get to the swapping
|
85
|
+
while @i < @len do
|
86
|
+
# reset @j per loop
|
87
|
+
@j = 0
|
88
|
+
while @j < (@len - @i) do
|
89
|
+
# if it's bigger swap it out
|
90
|
+
swap(list, @j, @j + 1) if list[@j] > list[@j + 1]
|
91
|
+
# increment
|
92
|
+
@j = @j + 1
|
93
|
+
end
|
94
|
+
# increment
|
95
|
+
@i = @i + 1
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
# join!
|
100
|
+
# helper function!
|
101
|
+
def self.join (a, b)
|
102
|
+
|
103
|
+
@joined = []
|
104
|
+
@a_i = 0
|
105
|
+
@b_i = 0
|
106
|
+
|
107
|
+
while (@a_i < a.length && @b_i < b.length)
|
108
|
+
if a[@a_i] < b[@b_i]
|
109
|
+
@joined.push(a[@a_i])
|
110
|
+
@a_i = @a_i + 1
|
111
|
+
else
|
112
|
+
joined.push(b[@b_i])
|
113
|
+
@b_i = @b_i + 1
|
114
|
+
end
|
115
|
+
end
|
116
|
+
# return
|
117
|
+
joined.concat(a.slice(@a_i, a.length)).concat(b.slice(@b_i, b.length))
|
118
|
+
end
|
119
|
+
# swap!
|
120
|
+
# helper function!
|
121
|
+
def self.swap (list, a, b)
|
122
|
+
@temp = list[a]
|
123
|
+
list[a] = list[b]
|
124
|
+
list[b] = @temp
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sorts_ESN
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ben Clark
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-07 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A simple gem holding insertion, merge, bubble, and quick sort functions
|
14
|
+
email: thecalestonrift@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/sorts_ESN.rb
|
20
|
+
homepage: http://rubygems.org/gems/sorts-ESN
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.0.6
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: basic tester gem for common sorts
|
44
|
+
test_files: []
|