diff --git a/sort.rb b/sort.rb new file mode 100644 index 0000000..defb264 --- /dev/null +++ b/sort.rb @@ -0,0 +1,48 @@ +module Sort + + def self.selection_sort(array) + #array.sort + + temp_num = 0 + min_num = 0 + min_index=0 + + for h in 0...array.length + min_num = array[h] + for i in h...array.length-1 + if array[i+1]0 + end + + def self.fizzbuzz(n) + fizzbuzz(n-1) if n>0 + puts n%3==0 && n!=0 ? "fizz" : n%5==0 && n!=0 ? "buzz" : n + end + + def self.exp(a,b) + return 1 if b<=0 + a*exp(a,b-1) + end + + def self.collatz(n) + puts n + return if n==1 + return collatz(n/2) if n%2 == 0 + collatz(3*n+1) + end + + +end \ No newline at end of file diff --git a/spec/sort_spec.rb b/spec/sort_spec.rb new file mode 100644 index 0000000..d878bfa --- /dev/null +++ b/spec/sort_spec.rb @@ -0,0 +1,33 @@ +require 'rubygems' +require 'rspec' +require_relative '../sort.rb' + + +describe 'Sort' do |variable| + describe '.selection_sort' do + it "returns an empty array" do + expect(Sort.selection_sort([])).to eq([]) + end + end + + describe '.selection_sort' do + it "successful with duplicates" do + expect(Sort.selection_sort([4,5,2,3,8,9,1,2,2])).to eq([4,5,2,3,8,9,1,2,2].sort) + end + end + + describe '.selection_sort' do + it "works with negatives" do + expect(Sort.selection_sort([-1,-5,2,6,44,95,-4])).to eq([-1,-5,2,6,44,95,-4].sort) + end + end + + describe '.selection_sort' do + rand_arr = (0..20000).to_a.map!{|x| x = (rand()*1000).to_i } + + it "works for large unordered arrays." do + expect(Sort.selection_sort(rand_arr)).to eq(rand_arr.sort) + end + end + +end \ No newline at end of file