brutish_sorts 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8f0c598a0a8ab6d06851a2dda378255d23fcf3f5
4
+ data.tar.gz: ccdf9dd3e1fdba4b73723c2cf656f8d571a79516
5
+ SHA512:
6
+ metadata.gz: c3e262e6c2058182fa5251dcfc7c2c8a220c83a0bb4f202eb5eb9466c57f5309db8b9185d76ffd5b0db0b29a6e468dfbbe18ebf318b511eb6f315196f7d95392
7
+ data.tar.gz: 9ae35cdf6399e22fb7d2c6ffb4414667d572607b28052904aa11379de70ecf8adc23dc9c1c71d937cc0cc91541d4fa69e7d6e6176a7408faef682e900252336a
@@ -0,0 +1,34 @@
1
+ require 'brutish_sorts/core'
2
+ require 'brutish_sorts/selection_sort'
3
+
4
+ module BrutishSorts
5
+ extend self
6
+
7
+ def selection_sort(array)
8
+ dup = array.dup
9
+ pivot = 0
10
+ for i in 0..dup.length - 1
11
+ pivot = SelectionSort.min_index(dup,i)
12
+ dup = SelectionSort.swap(dup,pivot,i)
13
+ end
14
+ dup
15
+ end
16
+
17
+ def insertion_sort(array)
18
+ dup = array.dup
19
+ tmp = 0
20
+ 0.upto(dup.length - 1) do |a|
21
+ a.downto(0) do |b|
22
+ if b < (dup.length - 1)
23
+ if dup[b + 1] < dup[b]
24
+ tmp = dup[b]
25
+ dup[b] = dup[b + 1]
26
+ dup[b + 1] = tmp
27
+ end
28
+ end
29
+ end
30
+ end
31
+ dup
32
+ end
33
+
34
+ end
@@ -0,0 +1,9 @@
1
+ module Enumerable
2
+ def selection_sort
3
+ BrutishSorts.selection_sort(self)
4
+ end
5
+
6
+ def insertion_sort
7
+ BrutishSorts.insertion_sort(self)
8
+ end
9
+ end
@@ -0,0 +1,23 @@
1
+ class SelectionSort
2
+
3
+ def self.swap(array, pivot, index)
4
+ temp = array[pivot]
5
+ array[pivot] = array[index]
6
+ array[index] = temp
7
+ array
8
+ end
9
+
10
+ def self.min_index(array, start_index)
11
+ min_value = array[start_index]
12
+ min_index = start_index
13
+ si_1 = start_index + 1
14
+
15
+ for a in si_1..array.length - 1
16
+ if array[a] < min_value
17
+ min_index = a
18
+ min_value = array[a]
19
+ end
20
+ end
21
+ min_index
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: brutish_sorts
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Josiah Siegel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-15 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: 'Unpolished sorts: selection sort, insertion sort'
14
+ email: josiah0601@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/brutish_sorts.rb
20
+ - lib/brutish_sorts/core.rb
21
+ - lib/brutish_sorts/selection_sort.rb
22
+ homepage: http://rubygems.org/gems/brutish_sorts
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.4.5
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Brutish Sorts
46
+ test_files: []