Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JLILLY sorting #53

Open
wants to merge 1 commit into
base: sorting
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions sort.rb
Original file line number Diff line number Diff line change
@@ -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]<min_num
min_num = array[i+1]
min_index = i+1
end
end
temp_num = array[h]
array[h] = min_num
array[min_index] = temp_num
end
array
end

def self.countdown(n)
puts n
countdown(n-1) if n >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
33 changes: 33 additions & 0 deletions spec/sort_spec.rb
Original file line number Diff line number Diff line change
@@ -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