pavsort 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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/pavsort.rb +82 -0
  3. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: da78c0f4070165bee47b967761ac59d9cc8062f7
4
+ data.tar.gz: b46e4d9594f661f330dfecabd8ea851d1de39f6f
5
+ SHA512:
6
+ metadata.gz: f1f6f26088e9763b4650d9ef5ce7c1229a11fdefe733173c0383e50e1ad7f19eb528e1c5d7fa5af27ffbd55d429f467edb83c24f9d26ac73b539de2a765c22a9
7
+ data.tar.gz: 52689038148ae38eb3743d2e5dac7a6c9dcc167a7b35cfe85c37cfe4ac7f253d6c2e19faa6f9284f5f2877e06381d6c72cba5f57383e22424491d7adeec560dd
data/lib/pavsort.rb ADDED
@@ -0,0 +1,82 @@
1
+ module Pavsort
2
+ class Merge
3
+ class << self
4
+
5
+ def sort(array)
6
+ part = array.partition{|element| element.respond_to?(:/)}
7
+ num_sort(part.first) + alph_sort(part.last)
8
+ end
9
+
10
+ private
11
+
12
+ def alph_sort(array)
13
+ return array if array.length <= 1
14
+ first_half, second_half = split_array_in_half(array)
15
+ return alph_merge(alph_sort(first_half),alph_sort(second_half))
16
+ end
17
+
18
+ def num_sort(array)
19
+ return array if array.length <= 1
20
+ first_half, second_half = split_array_in_half(array)
21
+ return num_merge(num_sort(first_half),num_sort(second_half))
22
+ end
23
+
24
+ def split_array_in_half(array)
25
+ mid_index = array.length / 2
26
+ [array.take(mid_index), array.drop(mid_index)]
27
+ end
28
+
29
+ def alph_merge(left, right)
30
+ result = []
31
+ until left.empty? && right.empty?
32
+ case
33
+ when left.empty? then result << right.shift
34
+ when right.empty? then result << left.shift
35
+ when left.first.downcase < right.first.downcase then result << left.shift
36
+ when right.first.downcase < left.first.downcase then result << right.shift
37
+ end
38
+ end
39
+ result
40
+ end
41
+
42
+ def num_merge(left, right)
43
+ result = []
44
+ until left.empty? && right.empty?
45
+ case
46
+ when left.empty? then result << right.shift
47
+ when right.empty? then result << left.shift
48
+ when left.first < right.first then result << left.shift
49
+ when right.first < left.first then result << right.shift
50
+ end
51
+ end
52
+ result
53
+ end
54
+ end
55
+ end
56
+
57
+ class Quick
58
+ class << self
59
+
60
+ def sort(array)
61
+ part = array.partition{|element| element.respond_to?(:/)}
62
+ num_sort(part.first) + alph_sort(part.last)
63
+ end
64
+
65
+ private
66
+
67
+ def num_sort(array)
68
+ return array if array.length <= 1
69
+ pivot = array.delete_at(rand(array.length))
70
+ partition = array.partition{|element| element < pivot}
71
+ num_sort(partition.first) + [pivot] + num_sort(partition.last)
72
+ end
73
+
74
+ def alph_sort(array)
75
+ return array if array.length <= 1
76
+ pivot = array.delete_at(rand(array.length))
77
+ partition = array.partition{|element| element.downcase < pivot.downcase}
78
+ alph_sort(partition.first) + [pivot] + alph_sort(partition.last)
79
+ end
80
+ end
81
+ end
82
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pavsort
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Phil Vargas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Merge sort and Quick sort methods implemented in ruby. Methods are case
14
+ and numerically insensitive
15
+ email: ''
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/pavsort.rb
21
+ homepage: http://rubygems.org/gems/pavsort
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.2.1
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Implementation of various numerically-and-case-insensitive Sorting Methods
45
+ test_files: []