grosser-algorithms 0.4.0 → 0.4.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.
- data/Manifest +45 -0
- data/VERSION +1 -0
- data/ext/containers/bst/bst.c +249 -0
- data/ext/containers/bst/extconf.rb +4 -0
- data/grosser-algorithms.gemspec +4 -4
- data/rdoc/classes/Algorithms.html +221 -0
- data/rdoc/classes/Algorithms/Algorithms.html +115 -0
- data/rdoc/classes/Algorithms/Algorithms/Sort.html +569 -0
- data/rdoc/classes/Algorithms/Containers.html +162 -0
- data/rdoc/classes/Algorithms/Containers/Heap.html +690 -0
- data/rdoc/classes/Algorithms/Containers/KDTree.html +195 -0
- data/rdoc/classes/Algorithms/Containers/MaxHeap.html +238 -0
- data/rdoc/classes/Algorithms/Containers/MinHeap.html +238 -0
- data/rdoc/classes/Algorithms/Containers/PriorityQueue.html +456 -0
- data/rdoc/classes/Algorithms/Containers/Queue.html +363 -0
- data/rdoc/classes/Algorithms/Containers/RubyDeque.html +617 -0
- data/rdoc/classes/Algorithms/Containers/RubyRBTreeMap.html +662 -0
- data/rdoc/classes/Algorithms/Containers/RubySplayTreeMap.html +623 -0
- data/rdoc/classes/Algorithms/Containers/Stack.html +363 -0
- data/rdoc/classes/Algorithms/Containers/SuffixArray.html +246 -0
- data/rdoc/classes/Algorithms/Containers/Trie.html +555 -0
- data/rdoc/classes/Algorithms/Search.html +273 -0
- data/rdoc/created.rid +1 -0
- data/rdoc/files/History_txt.html +281 -0
- data/rdoc/files/README_markdown.html +248 -0
- data/rdoc/files/lib/algorithms/search_rb.html +108 -0
- data/rdoc/files/lib/algorithms/sort_rb.html +108 -0
- data/rdoc/files/lib/algorithms/string_rb.html +115 -0
- data/rdoc/files/lib/algorithms_rb.html +252 -0
- data/rdoc/files/lib/containers/deque_rb.html +119 -0
- data/rdoc/files/lib/containers/heap_rb.html +124 -0
- data/rdoc/files/lib/containers/kd_tree_rb.html +135 -0
- data/rdoc/files/lib/containers/priority_queue_rb.html +108 -0
- data/rdoc/files/lib/containers/queue_rb.html +108 -0
- data/rdoc/files/lib/containers/rb_tree_map_rb.html +109 -0
- data/rdoc/files/lib/containers/splay_tree_map_rb.html +109 -0
- data/rdoc/files/lib/containers/stack_rb.html +108 -0
- data/rdoc/files/lib/containers/suffix_array_rb.html +113 -0
- data/rdoc/files/lib/containers/trie_rb.html +117 -0
- data/rdoc/fr_class_index.html +43 -0
- data/rdoc/fr_file_index.html +42 -0
- data/rdoc/fr_method_index.html +147 -0
- data/rdoc/index.html +24 -0
- data/rdoc/rdoc-style.css +208 -0
- data/spec/bst_gc_mark_spec.rb +25 -0
- data/spec/bst_spec.rb +25 -0
- metadata +50 -3
@@ -0,0 +1,115 @@
|
|
1
|
+
<?xml version="1.0" encoding="iso-8859-1"?>
|
2
|
+
<!DOCTYPE html
|
3
|
+
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
4
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
5
|
+
|
6
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
7
|
+
<head>
|
8
|
+
<title>Module: Algorithms::Algorithms</title>
|
9
|
+
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
|
10
|
+
<meta http-equiv="Content-Script-Type" content="text/javascript" />
|
11
|
+
<link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
|
12
|
+
<script type="text/javascript">
|
13
|
+
// <![CDATA[
|
14
|
+
|
15
|
+
function popupCode( url ) {
|
16
|
+
window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
|
17
|
+
}
|
18
|
+
|
19
|
+
function toggleCode( id ) {
|
20
|
+
if ( document.getElementById )
|
21
|
+
elem = document.getElementById( id );
|
22
|
+
else if ( document.all )
|
23
|
+
elem = eval( "document.all." + id );
|
24
|
+
else
|
25
|
+
return false;
|
26
|
+
|
27
|
+
elemStyle = elem.style;
|
28
|
+
|
29
|
+
if ( elemStyle.display != "block" ) {
|
30
|
+
elemStyle.display = "block"
|
31
|
+
} else {
|
32
|
+
elemStyle.display = "none"
|
33
|
+
}
|
34
|
+
|
35
|
+
return true;
|
36
|
+
}
|
37
|
+
|
38
|
+
// Make codeblocks hidden by default
|
39
|
+
document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
|
40
|
+
|
41
|
+
// ]]>
|
42
|
+
</script>
|
43
|
+
|
44
|
+
</head>
|
45
|
+
<body>
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
<div id="classHeader">
|
50
|
+
<table class="header-table">
|
51
|
+
<tr class="top-aligned-row">
|
52
|
+
<td><strong>Module</strong></td>
|
53
|
+
<td class="class-name-in-header">Algorithms::Algorithms</td>
|
54
|
+
</tr>
|
55
|
+
<tr class="top-aligned-row">
|
56
|
+
<td><strong>In:</strong></td>
|
57
|
+
<td>
|
58
|
+
<a href="../../files/lib/algorithms_rb.html">
|
59
|
+
lib/algorithms.rb
|
60
|
+
</a>
|
61
|
+
<br />
|
62
|
+
<a href="../../files/lib/algorithms/sort_rb.html">
|
63
|
+
lib/algorithms/sort.rb
|
64
|
+
</a>
|
65
|
+
<br />
|
66
|
+
</td>
|
67
|
+
</tr>
|
68
|
+
|
69
|
+
</table>
|
70
|
+
</div>
|
71
|
+
<!-- banner header -->
|
72
|
+
|
73
|
+
<div id="bodyContent">
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
<div id="contextContent">
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
</div>
|
82
|
+
|
83
|
+
|
84
|
+
</div>
|
85
|
+
|
86
|
+
|
87
|
+
<!-- if includes -->
|
88
|
+
|
89
|
+
<div id="section">
|
90
|
+
|
91
|
+
<div id="class-list">
|
92
|
+
<h3 class="section-bar">Classes and Modules</h3>
|
93
|
+
|
94
|
+
Module <a href="Algorithms/Sort.html" class="link">Algorithms::Algorithms::Sort</a><br />
|
95
|
+
|
96
|
+
</div>
|
97
|
+
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
<!-- if method_list -->
|
105
|
+
|
106
|
+
|
107
|
+
</div>
|
108
|
+
|
109
|
+
|
110
|
+
<div id="validator-badges">
|
111
|
+
<p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
|
112
|
+
</div>
|
113
|
+
|
114
|
+
</body>
|
115
|
+
</html>
|
@@ -0,0 +1,569 @@
|
|
1
|
+
<?xml version="1.0" encoding="iso-8859-1"?>
|
2
|
+
<!DOCTYPE html
|
3
|
+
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
4
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
5
|
+
|
6
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
7
|
+
<head>
|
8
|
+
<title>Module: Algorithms::Algorithms::Sort</title>
|
9
|
+
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
|
10
|
+
<meta http-equiv="Content-Script-Type" content="text/javascript" />
|
11
|
+
<link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
|
12
|
+
<script type="text/javascript">
|
13
|
+
// <![CDATA[
|
14
|
+
|
15
|
+
function popupCode( url ) {
|
16
|
+
window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
|
17
|
+
}
|
18
|
+
|
19
|
+
function toggleCode( id ) {
|
20
|
+
if ( document.getElementById )
|
21
|
+
elem = document.getElementById( id );
|
22
|
+
else if ( document.all )
|
23
|
+
elem = eval( "document.all." + id );
|
24
|
+
else
|
25
|
+
return false;
|
26
|
+
|
27
|
+
elemStyle = elem.style;
|
28
|
+
|
29
|
+
if ( elemStyle.display != "block" ) {
|
30
|
+
elemStyle.display = "block"
|
31
|
+
} else {
|
32
|
+
elemStyle.display = "none"
|
33
|
+
}
|
34
|
+
|
35
|
+
return true;
|
36
|
+
}
|
37
|
+
|
38
|
+
// Make codeblocks hidden by default
|
39
|
+
document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
|
40
|
+
|
41
|
+
// ]]>
|
42
|
+
</script>
|
43
|
+
|
44
|
+
</head>
|
45
|
+
<body>
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
<div id="classHeader">
|
50
|
+
<table class="header-table">
|
51
|
+
<tr class="top-aligned-row">
|
52
|
+
<td><strong>Module</strong></td>
|
53
|
+
<td class="class-name-in-header">Algorithms::Algorithms::Sort</td>
|
54
|
+
</tr>
|
55
|
+
<tr class="top-aligned-row">
|
56
|
+
<td><strong>In:</strong></td>
|
57
|
+
<td>
|
58
|
+
<a href="../../../files/lib/algorithms/sort_rb.html">
|
59
|
+
lib/algorithms/sort.rb
|
60
|
+
</a>
|
61
|
+
<br />
|
62
|
+
</td>
|
63
|
+
</tr>
|
64
|
+
|
65
|
+
</table>
|
66
|
+
</div>
|
67
|
+
<!-- banner header -->
|
68
|
+
|
69
|
+
<div id="bodyContent">
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
<div id="contextContent">
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
</div>
|
78
|
+
|
79
|
+
<div id="method-list">
|
80
|
+
<h3 class="section-bar">Methods</h3>
|
81
|
+
|
82
|
+
<div class="name-list">
|
83
|
+
<a href="#M000112">bubble_sort</a>
|
84
|
+
<a href="#M000113">comb_sort</a>
|
85
|
+
<a href="#M000115">heapsort</a>
|
86
|
+
<a href="#M000116">insertion_sort</a>
|
87
|
+
<a href="#M000121">merge</a>
|
88
|
+
<a href="#M000120">mergesort</a>
|
89
|
+
<a href="#M000118">partition</a>
|
90
|
+
<a href="#M000119">quicksort</a>
|
91
|
+
<a href="#M000114">selection_sort</a>
|
92
|
+
<a href="#M000117">shell_sort</a>
|
93
|
+
</div>
|
94
|
+
</div>
|
95
|
+
|
96
|
+
</div>
|
97
|
+
|
98
|
+
|
99
|
+
<!-- if includes -->
|
100
|
+
|
101
|
+
<div id="section">
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
|
110
|
+
<!-- if method_list -->
|
111
|
+
<div id="methods">
|
112
|
+
<h3 class="section-bar">Public Class methods</h3>
|
113
|
+
|
114
|
+
<div id="method-M000112" class="method-detail">
|
115
|
+
<a name="M000112"></a>
|
116
|
+
|
117
|
+
<div class="method-heading">
|
118
|
+
<a href="#M000112" class="method-signature">
|
119
|
+
<span class="method-name">bubble_sort</span><span class="method-args">(container)</span>
|
120
|
+
</a>
|
121
|
+
</div>
|
122
|
+
|
123
|
+
<div class="method-description">
|
124
|
+
<p>
|
125
|
+
Bubble sort: A very naive sort that keeps swapping elements until the
|
126
|
+
container is sorted. Requirements: Needs to be able to compare elements
|
127
|
+
with <=>, and the [] []= methods should be implemented for the
|
128
|
+
container. Time Complexity: О(n^2) Space Complexity: О(n) total, O(1)
|
129
|
+
auxiliary Stable: Yes
|
130
|
+
</p>
|
131
|
+
<pre>
|
132
|
+
Algorithms::Sort.bubble_sort [5, 4, 3, 1, 2] => [1, 2, 3, 4, 5]
|
133
|
+
</pre>
|
134
|
+
<p><a class="source-toggle" href="#"
|
135
|
+
onclick="toggleCode('M000112-source');return false;">[Source]</a></p>
|
136
|
+
<div class="method-source-code" id="M000112-source">
|
137
|
+
<pre>
|
138
|
+
<span class="ruby-comment cmt"># File lib/algorithms/sort.rb, line 19</span>
|
139
|
+
19: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">bubble_sort</span>(<span class="ruby-identifier">container</span>)
|
140
|
+
20: <span class="ruby-identifier">loop</span> <span class="ruby-keyword kw">do</span>
|
141
|
+
21: <span class="ruby-identifier">swapped</span> = <span class="ruby-keyword kw">false</span>
|
142
|
+
22: (<span class="ruby-identifier">container</span>.<span class="ruby-identifier">size</span><span class="ruby-operator">-</span><span class="ruby-value">1</span>).<span class="ruby-identifier">times</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span>
|
143
|
+
23: <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">container</span>[<span class="ruby-identifier">i</span>] <span class="ruby-operator"><=></span> <span class="ruby-identifier">container</span>[<span class="ruby-identifier">i</span><span class="ruby-operator">+</span><span class="ruby-value">1</span>]) <span class="ruby-operator">==</span> <span class="ruby-value">1</span>
|
144
|
+
24: <span class="ruby-identifier">container</span>[<span class="ruby-identifier">i</span>], <span class="ruby-identifier">container</span>[<span class="ruby-identifier">i</span><span class="ruby-operator">+</span><span class="ruby-value">1</span>] = <span class="ruby-identifier">container</span>[<span class="ruby-identifier">i</span><span class="ruby-operator">+</span><span class="ruby-value">1</span>], <span class="ruby-identifier">container</span>[<span class="ruby-identifier">i</span>] <span class="ruby-comment cmt"># Swap</span>
|
145
|
+
25: <span class="ruby-identifier">swapped</span> = <span class="ruby-keyword kw">true</span>
|
146
|
+
26: <span class="ruby-keyword kw">end</span>
|
147
|
+
27: <span class="ruby-keyword kw">end</span>
|
148
|
+
28: <span class="ruby-keyword kw">break</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">swapped</span>
|
149
|
+
29: <span class="ruby-keyword kw">end</span>
|
150
|
+
30: <span class="ruby-identifier">container</span>
|
151
|
+
31: <span class="ruby-keyword kw">end</span>
|
152
|
+
</pre>
|
153
|
+
</div>
|
154
|
+
</div>
|
155
|
+
</div>
|
156
|
+
|
157
|
+
<div id="method-M000113" class="method-detail">
|
158
|
+
<a name="M000113"></a>
|
159
|
+
|
160
|
+
<div class="method-heading">
|
161
|
+
<a href="#M000113" class="method-signature">
|
162
|
+
<span class="method-name">comb_sort</span><span class="method-args">(container)</span>
|
163
|
+
</a>
|
164
|
+
</div>
|
165
|
+
|
166
|
+
<div class="method-description">
|
167
|
+
<p>
|
168
|
+
Comb sort: A variation on bubble sort that dramatically improves
|
169
|
+
performance. Source: <a
|
170
|
+
href="http://yagni.com/combsort">yagni.com/combsort</a>/ Requirements:
|
171
|
+
Needs to be able to compare elements with <=>, and the [] []= methods
|
172
|
+
should be implemented for the container. Time Complexity: О(n^2) Space
|
173
|
+
Complexity: О(n) total, O(1) auxiliary Stable: Yes
|
174
|
+
</p>
|
175
|
+
<pre>
|
176
|
+
Algorithms::Sort.comb_sort [5, 4, 3, 1, 2] => [1, 2, 3, 4, 5]
|
177
|
+
</pre>
|
178
|
+
<p><a class="source-toggle" href="#"
|
179
|
+
onclick="toggleCode('M000113-source');return false;">[Source]</a></p>
|
180
|
+
<div class="method-source-code" id="M000113-source">
|
181
|
+
<pre>
|
182
|
+
<span class="ruby-comment cmt"># File lib/algorithms/sort.rb, line 42</span>
|
183
|
+
42: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">comb_sort</span>(<span class="ruby-identifier">container</span>)
|
184
|
+
43: <span class="ruby-identifier">container</span>
|
185
|
+
44: <span class="ruby-identifier">gap</span> = <span class="ruby-identifier">container</span>.<span class="ruby-identifier">size</span>
|
186
|
+
45: <span class="ruby-identifier">loop</span> <span class="ruby-keyword kw">do</span>
|
187
|
+
46: <span class="ruby-identifier">gap</span> = <span class="ruby-identifier">gap</span> <span class="ruby-operator">*</span> <span class="ruby-value">10</span><span class="ruby-operator">/</span><span class="ruby-value">13</span>
|
188
|
+
47: <span class="ruby-identifier">gap</span> = <span class="ruby-value">11</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">gap</span> <span class="ruby-operator">==</span> <span class="ruby-value">9</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">gap</span> <span class="ruby-operator">==</span> <span class="ruby-value">10</span>
|
189
|
+
48: <span class="ruby-identifier">gap</span> = <span class="ruby-value">1</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">gap</span> <span class="ruby-operator"><</span> <span class="ruby-value">1</span>
|
190
|
+
49: <span class="ruby-identifier">swapped</span> = <span class="ruby-keyword kw">false</span>
|
191
|
+
50: (<span class="ruby-identifier">container</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">gap</span>).<span class="ruby-identifier">times</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span>
|
192
|
+
51: <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">container</span>[<span class="ruby-identifier">i</span>] <span class="ruby-operator"><=></span> <span class="ruby-identifier">container</span>[<span class="ruby-identifier">i</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">gap</span>]) <span class="ruby-operator">==</span> <span class="ruby-value">1</span>
|
193
|
+
52: <span class="ruby-identifier">container</span>[<span class="ruby-identifier">i</span>], <span class="ruby-identifier">container</span>[<span class="ruby-identifier">i</span><span class="ruby-operator">+</span><span class="ruby-identifier">gap</span>] = <span class="ruby-identifier">container</span>[<span class="ruby-identifier">i</span><span class="ruby-operator">+</span><span class="ruby-identifier">gap</span>], <span class="ruby-identifier">container</span>[<span class="ruby-identifier">i</span>] <span class="ruby-comment cmt"># Swap</span>
|
194
|
+
53: <span class="ruby-identifier">swapped</span> = <span class="ruby-keyword kw">true</span>
|
195
|
+
54: <span class="ruby-keyword kw">end</span>
|
196
|
+
55: <span class="ruby-keyword kw">end</span>
|
197
|
+
56: <span class="ruby-keyword kw">break</span> <span class="ruby-keyword kw">if</span> <span class="ruby-operator">!</span><span class="ruby-identifier">swapped</span> <span class="ruby-operator">&&</span> <span class="ruby-identifier">gap</span> <span class="ruby-operator">==</span> <span class="ruby-value">1</span>
|
198
|
+
57: <span class="ruby-keyword kw">end</span>
|
199
|
+
58: <span class="ruby-identifier">container</span>
|
200
|
+
59: <span class="ruby-keyword kw">end</span>
|
201
|
+
</pre>
|
202
|
+
</div>
|
203
|
+
</div>
|
204
|
+
</div>
|
205
|
+
|
206
|
+
<div id="method-M000115" class="method-detail">
|
207
|
+
<a name="M000115"></a>
|
208
|
+
|
209
|
+
<div class="method-heading">
|
210
|
+
<a href="#M000115" class="method-signature">
|
211
|
+
<span class="method-name">heapsort</span><span class="method-args">(container)</span>
|
212
|
+
</a>
|
213
|
+
</div>
|
214
|
+
|
215
|
+
<div class="method-description">
|
216
|
+
<p>
|
217
|
+
Heap sort: Uses a heap (implemented by the <a
|
218
|
+
href="../Containers.html">Containers</a> module) to sort the collection.
|
219
|
+
Requirements: Needs to be able to compare elements with <=> Time
|
220
|
+
Complexity: О(n^2) Space Complexity: О(n) total, O(1) auxiliary Stable:
|
221
|
+
Yes
|
222
|
+
</p>
|
223
|
+
<pre>
|
224
|
+
Algorithms::Sort.heapsort [5, 4, 3, 1, 2] => [1, 2, 3, 4, 5]
|
225
|
+
</pre>
|
226
|
+
<p><a class="source-toggle" href="#"
|
227
|
+
onclick="toggleCode('M000115-source');return false;">[Source]</a></p>
|
228
|
+
<div class="method-source-code" id="M000115-source">
|
229
|
+
<pre>
|
230
|
+
<span class="ruby-comment cmt"># File lib/algorithms/sort.rb, line 88</span>
|
231
|
+
88: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">heapsort</span>(<span class="ruby-identifier">container</span>)
|
232
|
+
89: <span class="ruby-identifier">heap</span> = <span class="ruby-operator">::</span><span class="ruby-constant">Algorithms</span><span class="ruby-operator">::</span><span class="ruby-constant">Containers</span><span class="ruby-operator">::</span><span class="ruby-constant">Heap</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">container</span>)
|
233
|
+
90: <span class="ruby-identifier">ary</span> = []
|
234
|
+
91: <span class="ruby-identifier">ary</span> <span class="ruby-operator"><<</span> <span class="ruby-identifier">heap</span>.<span class="ruby-identifier">pop</span> <span class="ruby-keyword kw">until</span> <span class="ruby-identifier">heap</span>.<span class="ruby-identifier">empty?</span>
|
235
|
+
92: <span class="ruby-identifier">ary</span>
|
236
|
+
93: <span class="ruby-keyword kw">end</span>
|
237
|
+
</pre>
|
238
|
+
</div>
|
239
|
+
</div>
|
240
|
+
</div>
|
241
|
+
|
242
|
+
<div id="method-M000116" class="method-detail">
|
243
|
+
<a name="M000116"></a>
|
244
|
+
|
245
|
+
<div class="method-heading">
|
246
|
+
<a href="#M000116" class="method-signature">
|
247
|
+
<span class="method-name">insertion_sort</span><span class="method-args">(container)</span>
|
248
|
+
</a>
|
249
|
+
</div>
|
250
|
+
|
251
|
+
<div class="method-description">
|
252
|
+
<p>
|
253
|
+
Insertion sort: Elements are inserted sequentially into the right position.
|
254
|
+
Requirements: Needs to be able to compare elements with <=>, and the
|
255
|
+
[] []= methods should be implemented for the container. Time Complexity:
|
256
|
+
О(n^2) Space Complexity: О(n) total, O(1) auxiliary Stable: Yes
|
257
|
+
</p>
|
258
|
+
<pre>
|
259
|
+
Algorithms::Sort.insertion_sort [5, 4, 3, 1, 2] => [1, 2, 3, 4, 5]
|
260
|
+
</pre>
|
261
|
+
<p><a class="source-toggle" href="#"
|
262
|
+
onclick="toggleCode('M000116-source');return false;">[Source]</a></p>
|
263
|
+
<div class="method-source-code" id="M000116-source">
|
264
|
+
<pre>
|
265
|
+
<span class="ruby-comment cmt"># File lib/algorithms/sort.rb, line 103</span>
|
266
|
+
103: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">insertion_sort</span>(<span class="ruby-identifier">container</span>)
|
267
|
+
104: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">container</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">container</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator"><</span> <span class="ruby-value">2</span>
|
268
|
+
105: (<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-identifier">container</span>.<span class="ruby-identifier">size</span><span class="ruby-operator">-</span><span class="ruby-value">1</span>).<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span>
|
269
|
+
106: <span class="ruby-identifier">value</span> = <span class="ruby-identifier">container</span>[<span class="ruby-identifier">i</span>]
|
270
|
+
107: <span class="ruby-identifier">j</span> = <span class="ruby-identifier">i</span><span class="ruby-operator">-</span><span class="ruby-value">1</span>
|
271
|
+
108: <span class="ruby-keyword kw">while</span> <span class="ruby-identifier">j</span> <span class="ruby-operator">>=</span> <span class="ruby-value">0</span> <span class="ruby-keyword kw">and</span> <span class="ruby-identifier">container</span>[<span class="ruby-identifier">j</span>] <span class="ruby-operator">></span> <span class="ruby-identifier">value</span> <span class="ruby-keyword kw">do</span>
|
272
|
+
109: <span class="ruby-identifier">container</span>[<span class="ruby-identifier">j</span><span class="ruby-operator">+</span><span class="ruby-value">1</span>] = <span class="ruby-identifier">container</span>[<span class="ruby-identifier">j</span>]
|
273
|
+
110: <span class="ruby-identifier">j</span> = <span class="ruby-identifier">j</span><span class="ruby-operator">-</span><span class="ruby-value">1</span>
|
274
|
+
111: <span class="ruby-keyword kw">end</span>
|
275
|
+
112: <span class="ruby-identifier">container</span>[<span class="ruby-identifier">j</span><span class="ruby-operator">+</span><span class="ruby-value">1</span>] = <span class="ruby-identifier">value</span>
|
276
|
+
113: <span class="ruby-keyword kw">end</span>
|
277
|
+
114: <span class="ruby-identifier">container</span>
|
278
|
+
115: <span class="ruby-keyword kw">end</span>
|
279
|
+
</pre>
|
280
|
+
</div>
|
281
|
+
</div>
|
282
|
+
</div>
|
283
|
+
|
284
|
+
<div id="method-M000121" class="method-detail">
|
285
|
+
<a name="M000121"></a>
|
286
|
+
|
287
|
+
<div class="method-heading">
|
288
|
+
<a href="#M000121" class="method-signature">
|
289
|
+
<span class="method-name">merge</span><span class="method-args">(left, right)</span>
|
290
|
+
</a>
|
291
|
+
</div>
|
292
|
+
|
293
|
+
<div class="method-description">
|
294
|
+
<p><a class="source-toggle" href="#"
|
295
|
+
onclick="toggleCode('M000121-source');return false;">[Source]</a></p>
|
296
|
+
<div class="method-source-code" id="M000121-source">
|
297
|
+
<pre>
|
298
|
+
<span class="ruby-comment cmt"># File lib/algorithms/sort.rb, line 233</span>
|
299
|
+
233: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">merge</span>(<span class="ruby-identifier">left</span>, <span class="ruby-identifier">right</span>)
|
300
|
+
234: <span class="ruby-identifier">sorted</span> = []
|
301
|
+
235: <span class="ruby-keyword kw">until</span> <span class="ruby-identifier">left</span>.<span class="ruby-identifier">empty?</span> <span class="ruby-keyword kw">or</span> <span class="ruby-identifier">right</span>.<span class="ruby-identifier">empty?</span>
|
302
|
+
236: <span class="ruby-identifier">left</span>.<span class="ruby-identifier">first</span> <span class="ruby-operator"><=</span> <span class="ruby-identifier">right</span>.<span class="ruby-identifier">first</span> <span class="ruby-value">? </span><span class="ruby-identifier">sorted</span> <span class="ruby-operator"><<</span> <span class="ruby-identifier">left</span>.<span class="ruby-identifier">shift</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">sorted</span> <span class="ruby-operator"><<</span> <span class="ruby-identifier">right</span>.<span class="ruby-identifier">shift</span>
|
303
|
+
237: <span class="ruby-keyword kw">end</span>
|
304
|
+
238: <span class="ruby-identifier">sorted</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">left</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">right</span>
|
305
|
+
239: <span class="ruby-keyword kw">end</span>
|
306
|
+
</pre>
|
307
|
+
</div>
|
308
|
+
</div>
|
309
|
+
</div>
|
310
|
+
|
311
|
+
<div id="method-M000120" class="method-detail">
|
312
|
+
<a name="M000120"></a>
|
313
|
+
|
314
|
+
<div class="method-heading">
|
315
|
+
<a href="#M000120" class="method-signature">
|
316
|
+
<span class="method-name">mergesort</span><span class="method-args">(container)</span>
|
317
|
+
</a>
|
318
|
+
</div>
|
319
|
+
|
320
|
+
<div class="method-description">
|
321
|
+
<p>
|
322
|
+
Mergesort: A stable divide-and-conquer sort that sorts small chunks of the
|
323
|
+
container and then merges them together. Returns an array of the sorted
|
324
|
+
elements. Requirements: Container should implement [] Time Complexity: О(n
|
325
|
+
log n) average and worst-case Space Complexity: О(n) auxiliary Stable: Yes
|
326
|
+
</p>
|
327
|
+
<pre>
|
328
|
+
Algorithms::Sort.mergesort [5, 4, 3, 1, 2] => [1, 2, 3, 4, 5]
|
329
|
+
</pre>
|
330
|
+
<p><a class="source-toggle" href="#"
|
331
|
+
onclick="toggleCode('M000120-source');return false;">[Source]</a></p>
|
332
|
+
<div class="method-source-code" id="M000120-source">
|
333
|
+
<pre>
|
334
|
+
<span class="ruby-comment cmt"># File lib/algorithms/sort.rb, line 225</span>
|
335
|
+
225: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">mergesort</span>(<span class="ruby-identifier">container</span>)
|
336
|
+
226: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">container</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">container</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator"><=</span> <span class="ruby-value">1</span>
|
337
|
+
227: <span class="ruby-identifier">mid</span> = <span class="ruby-identifier">container</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator">/</span> <span class="ruby-value">2</span>
|
338
|
+
228: <span class="ruby-identifier">left</span> = <span class="ruby-identifier">container</span>[<span class="ruby-value">0</span><span class="ruby-operator">...</span><span class="ruby-identifier">mid</span>]
|
339
|
+
229: <span class="ruby-identifier">right</span> = <span class="ruby-identifier">container</span>[<span class="ruby-identifier">mid</span><span class="ruby-operator">...</span><span class="ruby-identifier">container</span>.<span class="ruby-identifier">size</span>]
|
340
|
+
230: <span class="ruby-identifier">merge</span>(<span class="ruby-identifier">mergesort</span>(<span class="ruby-identifier">left</span>), <span class="ruby-identifier">mergesort</span>(<span class="ruby-identifier">right</span>))
|
341
|
+
231: <span class="ruby-keyword kw">end</span>
|
342
|
+
</pre>
|
343
|
+
</div>
|
344
|
+
</div>
|
345
|
+
</div>
|
346
|
+
|
347
|
+
<div id="method-M000118" class="method-detail">
|
348
|
+
<a name="M000118"></a>
|
349
|
+
|
350
|
+
<div class="method-heading">
|
351
|
+
<a href="#M000118" class="method-signature">
|
352
|
+
<span class="method-name">partition</span><span class="method-args">(data, left, right)</span>
|
353
|
+
</a>
|
354
|
+
</div>
|
355
|
+
|
356
|
+
<div class="method-description">
|
357
|
+
<p>
|
358
|
+
Quicksort: A divide-and-conquer sort that recursively partitions a
|
359
|
+
container until it is sorted. Requirements: Container should implement pop
|
360
|
+
and include the Enumerable module. Time Complexity: О(n log n) average,
|
361
|
+
O(n^2) worst-case Space Complexity: О(n) auxiliary Stable: No
|
362
|
+
</p>
|
363
|
+
<pre>
|
364
|
+
Algorithms::Sort.quicksort [5, 4, 3, 1, 2] => [1, 2, 3, 4, 5]
|
365
|
+
</pre>
|
366
|
+
<p>
|
367
|
+
def self.quicksort(container)
|
368
|
+
</p>
|
369
|
+
<pre>
|
370
|
+
return [] if container.empty?
|
371
|
+
|
372
|
+
x, *xs = container
|
373
|
+
|
374
|
+
quicksort(xs.select { |i| i < x }) + [x] + quicksort(xs.select { |i| i >= x })
|
375
|
+
</pre>
|
376
|
+
<p>
|
377
|
+
end
|
378
|
+
</p>
|
379
|
+
<p><a class="source-toggle" href="#"
|
380
|
+
onclick="toggleCode('M000118-source');return false;">[Source]</a></p>
|
381
|
+
<div class="method-source-code" id="M000118-source">
|
382
|
+
<pre>
|
383
|
+
<span class="ruby-comment cmt"># File lib/algorithms/sort.rb, line 157</span>
|
384
|
+
157: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">partition</span>(<span class="ruby-identifier">data</span>, <span class="ruby-identifier">left</span>, <span class="ruby-identifier">right</span>)
|
385
|
+
158: <span class="ruby-identifier">pivot</span> = <span class="ruby-identifier">data</span>[<span class="ruby-identifier">front</span>]
|
386
|
+
159: <span class="ruby-identifier">left</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span>
|
387
|
+
160:
|
388
|
+
161: <span class="ruby-keyword kw">while</span> <span class="ruby-identifier">left</span> <span class="ruby-operator"><=</span> <span class="ruby-identifier">right</span> <span class="ruby-keyword kw">do</span>
|
389
|
+
162: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">data</span>[<span class="ruby-identifier">frontUnknown</span>] <span class="ruby-operator"><</span> <span class="ruby-identifier">pivot</span>
|
390
|
+
163: <span class="ruby-identifier">back</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span>
|
391
|
+
164: <span class="ruby-identifier">data</span>[<span class="ruby-identifier">frontUnknown</span>], <span class="ruby-identifier">data</span>[<span class="ruby-identifier">back</span>] = <span class="ruby-identifier">data</span>[<span class="ruby-identifier">back</span>], <span class="ruby-identifier">data</span>[<span class="ruby-identifier">frontUnknown</span>] <span class="ruby-comment cmt"># Swap</span>
|
392
|
+
165: <span class="ruby-keyword kw">end</span>
|
393
|
+
166:
|
394
|
+
167: <span class="ruby-identifier">frontUnknown</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span>
|
395
|
+
168: <span class="ruby-keyword kw">end</span>
|
396
|
+
169:
|
397
|
+
170: <span class="ruby-identifier">data</span>[<span class="ruby-identifier">front</span>], <span class="ruby-identifier">data</span>[<span class="ruby-identifier">back</span>] = <span class="ruby-identifier">data</span>[<span class="ruby-identifier">back</span>], <span class="ruby-identifier">data</span>[<span class="ruby-identifier">front</span>] <span class="ruby-comment cmt"># Swap</span>
|
398
|
+
171: <span class="ruby-identifier">back</span>
|
399
|
+
172: <span class="ruby-keyword kw">end</span>
|
400
|
+
</pre>
|
401
|
+
</div>
|
402
|
+
</div>
|
403
|
+
</div>
|
404
|
+
|
405
|
+
<div id="method-M000119" class="method-detail">
|
406
|
+
<a name="M000119"></a>
|
407
|
+
|
408
|
+
<div class="method-heading">
|
409
|
+
<a href="#M000119" class="method-signature">
|
410
|
+
<span class="method-name">quicksort</span><span class="method-args">(container)</span>
|
411
|
+
</a>
|
412
|
+
</div>
|
413
|
+
|
414
|
+
<div class="method-description">
|
415
|
+
<p>
|
416
|
+
def self.quicksort(container, left = 0, right = container.size - 1)
|
417
|
+
</p>
|
418
|
+
<pre>
|
419
|
+
if left < right
|
420
|
+
middle = partition(container, left, right)
|
421
|
+
quicksort(container, left, middle - 1)
|
422
|
+
quicksort(container, middle + 1, right)
|
423
|
+
end
|
424
|
+
</pre>
|
425
|
+
<p>
|
426
|
+
end
|
427
|
+
</p>
|
428
|
+
<p><a class="source-toggle" href="#"
|
429
|
+
onclick="toggleCode('M000119-source');return false;">[Source]</a></p>
|
430
|
+
<div class="method-source-code" id="M000119-source">
|
431
|
+
<pre>
|
432
|
+
<span class="ruby-comment cmt"># File lib/algorithms/sort.rb, line 183</span>
|
433
|
+
183: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">quicksort</span>(<span class="ruby-identifier">container</span>)
|
434
|
+
184: <span class="ruby-identifier">bottom</span>, <span class="ruby-identifier">top</span> = [], []
|
435
|
+
185: <span class="ruby-identifier">top</span>[<span class="ruby-value">0</span>] = <span class="ruby-value">0</span>
|
436
|
+
186: <span class="ruby-identifier">bottom</span>[<span class="ruby-value">0</span>] = <span class="ruby-identifier">container</span>.<span class="ruby-identifier">size</span>
|
437
|
+
187: <span class="ruby-identifier">i</span> = <span class="ruby-value">0</span>
|
438
|
+
188: <span class="ruby-keyword kw">while</span> <span class="ruby-identifier">i</span> <span class="ruby-operator">>=</span> <span class="ruby-value">0</span> <span class="ruby-keyword kw">do</span>
|
439
|
+
189: <span class="ruby-identifier">l</span> = <span class="ruby-identifier">top</span>[<span class="ruby-identifier">i</span>]
|
440
|
+
190: <span class="ruby-identifier">r</span> = <span class="ruby-identifier">bottom</span>[<span class="ruby-identifier">i</span>] <span class="ruby-operator">-</span> <span class="ruby-value">1</span>;
|
441
|
+
191: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">l</span> <span class="ruby-operator"><</span> <span class="ruby-identifier">r</span>
|
442
|
+
192: <span class="ruby-identifier">pivot</span> = <span class="ruby-identifier">container</span>[<span class="ruby-identifier">l</span>]
|
443
|
+
193: <span class="ruby-keyword kw">while</span> <span class="ruby-identifier">l</span> <span class="ruby-operator"><</span> <span class="ruby-identifier">r</span> <span class="ruby-keyword kw">do</span>
|
444
|
+
194: <span class="ruby-identifier">r</span> <span class="ruby-operator">-=</span> <span class="ruby-value">1</span> <span class="ruby-keyword kw">while</span> (<span class="ruby-identifier">container</span>[<span class="ruby-identifier">r</span>] <span class="ruby-operator">>=</span> <span class="ruby-identifier">pivot</span> <span class="ruby-operator">&&</span> <span class="ruby-identifier">l</span> <span class="ruby-operator"><</span> <span class="ruby-identifier">r</span>)
|
445
|
+
195: <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">l</span> <span class="ruby-operator"><</span> <span class="ruby-identifier">r</span>)
|
446
|
+
196: <span class="ruby-identifier">container</span>[<span class="ruby-identifier">l</span>] = <span class="ruby-identifier">container</span>[<span class="ruby-identifier">r</span>]
|
447
|
+
197: <span class="ruby-identifier">l</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span>
|
448
|
+
198: <span class="ruby-keyword kw">end</span>
|
449
|
+
199: <span class="ruby-identifier">l</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span> <span class="ruby-keyword kw">while</span> (<span class="ruby-identifier">container</span>[<span class="ruby-identifier">l</span>] <span class="ruby-operator"><=</span> <span class="ruby-identifier">pivot</span> <span class="ruby-operator">&&</span> <span class="ruby-identifier">l</span> <span class="ruby-operator"><</span> <span class="ruby-identifier">r</span>)
|
450
|
+
200: <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">l</span> <span class="ruby-operator"><</span> <span class="ruby-identifier">r</span>)
|
451
|
+
201: <span class="ruby-identifier">container</span>[<span class="ruby-identifier">r</span>] = <span class="ruby-identifier">container</span>[<span class="ruby-identifier">l</span>]
|
452
|
+
202: <span class="ruby-identifier">r</span> <span class="ruby-operator">-=</span> <span class="ruby-value">1</span>
|
453
|
+
203: <span class="ruby-keyword kw">end</span>
|
454
|
+
204: <span class="ruby-keyword kw">end</span>
|
455
|
+
205: <span class="ruby-identifier">container</span>[<span class="ruby-identifier">l</span>] = <span class="ruby-identifier">pivot</span>
|
456
|
+
206: <span class="ruby-identifier">top</span>[<span class="ruby-identifier">i</span><span class="ruby-operator">+</span><span class="ruby-value">1</span>] = <span class="ruby-identifier">l</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>
|
457
|
+
207: <span class="ruby-identifier">bottom</span>[<span class="ruby-identifier">i</span><span class="ruby-operator">+</span><span class="ruby-value">1</span>] = <span class="ruby-identifier">bottom</span>[<span class="ruby-identifier">i</span>]
|
458
|
+
208: <span class="ruby-identifier">bottom</span>[<span class="ruby-identifier">i</span>] = <span class="ruby-identifier">l</span>
|
459
|
+
209: <span class="ruby-identifier">i</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span>
|
460
|
+
210: <span class="ruby-keyword kw">else</span>
|
461
|
+
211: <span class="ruby-identifier">i</span> <span class="ruby-operator">-=</span> <span class="ruby-value">1</span>
|
462
|
+
212: <span class="ruby-keyword kw">end</span>
|
463
|
+
213: <span class="ruby-keyword kw">end</span>
|
464
|
+
214: <span class="ruby-identifier">container</span>
|
465
|
+
215: <span class="ruby-keyword kw">end</span>
|
466
|
+
</pre>
|
467
|
+
</div>
|
468
|
+
</div>
|
469
|
+
</div>
|
470
|
+
|
471
|
+
<div id="method-M000114" class="method-detail">
|
472
|
+
<a name="M000114"></a>
|
473
|
+
|
474
|
+
<div class="method-heading">
|
475
|
+
<a href="#M000114" class="method-signature">
|
476
|
+
<span class="method-name">selection_sort</span><span class="method-args">(container)</span>
|
477
|
+
</a>
|
478
|
+
</div>
|
479
|
+
|
480
|
+
<div class="method-description">
|
481
|
+
<p>
|
482
|
+
Selection sort: A naive sort that goes through the container and selects
|
483
|
+
the smallest element, putting it at the beginning. Repeat until the end is
|
484
|
+
reached. Requirements: Needs to be able to compare elements with <=>,
|
485
|
+
and the [] []= methods should be implemented for the container. Time
|
486
|
+
Complexity: О(n^2) Space Complexity: О(n) total, O(1) auxiliary Stable:
|
487
|
+
Yes
|
488
|
+
</p>
|
489
|
+
<pre>
|
490
|
+
Algorithms::Sort.selection_sort [5, 4, 3, 1, 2] => [1, 2, 3, 4, 5]
|
491
|
+
</pre>
|
492
|
+
<p><a class="source-toggle" href="#"
|
493
|
+
onclick="toggleCode('M000114-source');return false;">[Source]</a></p>
|
494
|
+
<div class="method-source-code" id="M000114-source">
|
495
|
+
<pre>
|
496
|
+
<span class="ruby-comment cmt"># File lib/algorithms/sort.rb, line 70</span>
|
497
|
+
70: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">selection_sort</span>(<span class="ruby-identifier">container</span>)
|
498
|
+
71: <span class="ruby-value">0</span>.<span class="ruby-identifier">upto</span>(<span class="ruby-identifier">container</span>.<span class="ruby-identifier">size</span><span class="ruby-operator">-</span><span class="ruby-value">1</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span>
|
499
|
+
72: <span class="ruby-identifier">min</span> = <span class="ruby-identifier">i</span>
|
500
|
+
73: (<span class="ruby-identifier">i</span><span class="ruby-operator">+</span><span class="ruby-value">1</span>).<span class="ruby-identifier">upto</span>(<span class="ruby-identifier">container</span>.<span class="ruby-identifier">size</span><span class="ruby-operator">-</span><span class="ruby-value">1</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">j</span><span class="ruby-operator">|</span>
|
501
|
+
74: <span class="ruby-identifier">min</span> = <span class="ruby-identifier">j</span> <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">container</span>[<span class="ruby-identifier">j</span>] <span class="ruby-operator"><=></span> <span class="ruby-identifier">container</span>[<span class="ruby-identifier">min</span>]) <span class="ruby-operator">==</span> <span class="ruby-value">-1</span>
|
502
|
+
75: <span class="ruby-keyword kw">end</span>
|
503
|
+
76: <span class="ruby-identifier">container</span>[<span class="ruby-identifier">i</span>], <span class="ruby-identifier">container</span>[<span class="ruby-identifier">min</span>] = <span class="ruby-identifier">container</span>[<span class="ruby-identifier">min</span>], <span class="ruby-identifier">container</span>[<span class="ruby-identifier">i</span>] <span class="ruby-comment cmt"># Swap</span>
|
504
|
+
77: <span class="ruby-keyword kw">end</span>
|
505
|
+
78: <span class="ruby-identifier">container</span>
|
506
|
+
79: <span class="ruby-keyword kw">end</span>
|
507
|
+
</pre>
|
508
|
+
</div>
|
509
|
+
</div>
|
510
|
+
</div>
|
511
|
+
|
512
|
+
<div id="method-M000117" class="method-detail">
|
513
|
+
<a name="M000117"></a>
|
514
|
+
|
515
|
+
<div class="method-heading">
|
516
|
+
<a href="#M000117" class="method-signature">
|
517
|
+
<span class="method-name">shell_sort</span><span class="method-args">(container)</span>
|
518
|
+
</a>
|
519
|
+
</div>
|
520
|
+
|
521
|
+
<div class="method-description">
|
522
|
+
<p>
|
523
|
+
Shell sort: Similar approach as insertion sort but slightly better.
|
524
|
+
Requirements: Needs to be able to compare elements with <=>, and the
|
525
|
+
[] []= methods should be implemented for the container. Time Complexity:
|
526
|
+
О(n^2) Space Complexity: О(n) total, O(1) auxiliary Stable: Yes
|
527
|
+
</p>
|
528
|
+
<pre>
|
529
|
+
Algorithms::Sort.shell_sort [5, 4, 3, 1, 2] => [1, 2, 3, 4, 5]
|
530
|
+
</pre>
|
531
|
+
<p><a class="source-toggle" href="#"
|
532
|
+
onclick="toggleCode('M000117-source');return false;">[Source]</a></p>
|
533
|
+
<div class="method-source-code" id="M000117-source">
|
534
|
+
<pre>
|
535
|
+
<span class="ruby-comment cmt"># File lib/algorithms/sort.rb, line 125</span>
|
536
|
+
125: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">shell_sort</span>(<span class="ruby-identifier">container</span>)
|
537
|
+
126: <span class="ruby-identifier">increment</span> = <span class="ruby-identifier">container</span>.<span class="ruby-identifier">size</span><span class="ruby-operator">/</span><span class="ruby-value">2</span>
|
538
|
+
127: <span class="ruby-keyword kw">while</span> <span class="ruby-identifier">increment</span> <span class="ruby-operator">></span> <span class="ruby-value">0</span> <span class="ruby-keyword kw">do</span>
|
539
|
+
128: (<span class="ruby-identifier">increment</span><span class="ruby-operator">..</span><span class="ruby-identifier">container</span>.<span class="ruby-identifier">size</span><span class="ruby-operator">-</span><span class="ruby-value">1</span>).<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span>
|
540
|
+
129: <span class="ruby-identifier">temp</span> = <span class="ruby-identifier">container</span>[<span class="ruby-identifier">i</span>]
|
541
|
+
130: <span class="ruby-identifier">j</span> = <span class="ruby-identifier">i</span>
|
542
|
+
131: <span class="ruby-keyword kw">while</span> <span class="ruby-identifier">j</span> <span class="ruby-operator">>=</span> <span class="ruby-identifier">increment</span> <span class="ruby-operator">&&</span> <span class="ruby-identifier">container</span>[<span class="ruby-identifier">j</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">increment</span>] <span class="ruby-operator">></span> <span class="ruby-identifier">temp</span> <span class="ruby-keyword kw">do</span>
|
543
|
+
132: <span class="ruby-identifier">container</span>[<span class="ruby-identifier">j</span>] = <span class="ruby-identifier">container</span>[<span class="ruby-identifier">j</span><span class="ruby-operator">-</span><span class="ruby-identifier">increment</span>]
|
544
|
+
133: <span class="ruby-identifier">j</span> <span class="ruby-operator">-=</span> <span class="ruby-identifier">increment</span>
|
545
|
+
134: <span class="ruby-keyword kw">end</span>
|
546
|
+
135: <span class="ruby-identifier">container</span>[<span class="ruby-identifier">j</span>] = <span class="ruby-identifier">temp</span>
|
547
|
+
136: <span class="ruby-keyword kw">end</span>
|
548
|
+
137: <span class="ruby-identifier">increment</span> = (<span class="ruby-identifier">increment</span> <span class="ruby-operator">==</span> <span class="ruby-value">2</span> <span class="ruby-operator">?</span> <span class="ruby-value">1</span> <span class="ruby-operator">:</span> (<span class="ruby-identifier">increment</span> <span class="ruby-operator">/</span> <span class="ruby-value">2.2</span>).<span class="ruby-identifier">round</span>)
|
549
|
+
138: <span class="ruby-keyword kw">end</span>
|
550
|
+
139: <span class="ruby-identifier">container</span>
|
551
|
+
140: <span class="ruby-keyword kw">end</span>
|
552
|
+
</pre>
|
553
|
+
</div>
|
554
|
+
</div>
|
555
|
+
</div>
|
556
|
+
|
557
|
+
|
558
|
+
</div>
|
559
|
+
|
560
|
+
|
561
|
+
</div>
|
562
|
+
|
563
|
+
|
564
|
+
<div id="validator-badges">
|
565
|
+
<p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
|
566
|
+
</div>
|
567
|
+
|
568
|
+
</body>
|
569
|
+
</html>
|