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

Travis Hooper Sorting #80

Open
wants to merge 2 commits 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
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
source "https://rubygems.org"
gem 'rspec'
gem 'pry-debugger'
gem 'pry-byebug'
65 changes: 65 additions & 0 deletions sort.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
module Sort

def self.select_sort(array)
for i in 0...array.length
minimum = array[i]
for j in i...array.length
if minimum > array[j]
minimum = array[j]
array[j] = array[i]
array[i] = minimum
end
end
end
array
end

def self.bubble_sort(array)
if array.empty?
return array
end
go = true
unsorted = array.dup
while go
for i in 0...(array.length-1)
if array[i] > array[i+1]
minimum = array[i+1]
array[i+1] = array[i]
array[i] = minimum
end
end
if array == unsorted
go = false
end
unsorted = array.dup
end
array
end

def self.bubble_sort2(array)
if array.empty?
return array
end
go = true
while go
for i in 0...(array.length-1)
if array[i] > array[i+1]
minimum = array[i+1]
array[i+1] = array[i]
array[i] = minimum
counter = 0
for j in 0...(array.length-1)
if array[j] <= array[j+1]
counter += 1
if counter == array.length - 1
go = false
end
end
end
end
end
end
array
end

end
36 changes: 36 additions & 0 deletions spec/sort_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'rubygems'
require 'rspec'
require 'pry-byebug'
require_relative '../sort.rb'


describe "Sort" do
describe '.selection_sort' do
it "should return a sorted array containing the same elements" do

expect(Sort.select_sort([])).to eq([])
expect(Sort.select_sort([-1,-4,-5,-2,-44,5,6,-100])).to eq([-1,-4,-5,-2,-44,5,6,-100].sort)
expect(Sort.select_sort([9,9,9,9,9,3,3,3,3,6,6,6,6,4,4,4,3,3,3])).to eq([9,9,9,9,9,3,3,3,3,6,6,6,6,4,4,4,3,3,3].sort)
expect(Sort.select_sort([1,5,88,3,5,7,66,-1,400,0,5,4,3,8,7,1,3,0,0,7])).to eq([1,5,88,3,5,7,66,-1,400,0,5,4,3,8,7,1,3,0,0,7].sort)
end
end

describe '.bubble_sort' do
it "should return a sorted array containing the same elements" do
expect(Sort.bubble_sort([])).to eq([])
expect(Sort.bubble_sort([-1,-4,-5,-2,-44,5,6,-100])).to eq([-1,-4,-5,-2,-44,5,6,-100].sort)
expect(Sort.bubble_sort([9,9,9,9,9,3,3,3,3,6,6,6,6,4,4,4,3,3,3])).to eq([9,9,9,9,9,3,3,3,3,6,6,6,6,4,4,4,3,3,3].sort)
expect(Sort.bubble_sort([1,5,88,3,5,7,66,-1,400,0,5,4,3,8,7,1,3,0,0,7])).to eq([1,5,88,3,5,7,66,-1,400,0,5,4,3,8,7,1,3,0,0,7].sort)
end
end

describe '.bubble_sort' do
it "should return a sorted array containing the same elements" do
expect(Sort.bubble_sort2([])).to eq([])
expect(Sort.bubble_sort2([-1,-4,-5,-2,-44,5,6,-100])).to eq([-1,-4,-5,-2,-44,5,6,-100].sort)
expect(Sort.bubble_sort2([9,9,9,9,9,3,3,3,3,6,6,6,6,4,4,4,3,3,3])).to eq([9,9,9,9,9,3,3,3,3,6,6,6,6,4,4,4,3,3,3].sort)
expect(Sort.bubble_sort2([1,5,88,3,5,7,66,-1,400,0,5,4,3,8,7,1,3,0,0,7])).to eq([1,5,88,3,5,7,66,-1,400,0,5,4,3,8,7,1,3,0,0,7].sort)
end
end

end