From 3a2b1b5023e47b5c74b15ceef7fc044767be151f Mon Sep 17 00:00:00 2001 From: Travis Hooper Date: Thu, 4 Sep 2014 23:19:01 -0500 Subject: [PATCH 1/2] A working select sort and bubble sort method along with passing spec tests. --- Gemfile | 2 +- sort.rb | 42 ++++++++++++++++++++++++++++++++++++++++++ spec/sort_spec.rb | 26 ++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 sort.rb create mode 100644 spec/sort_spec.rb diff --git a/Gemfile b/Gemfile index 202988e..a1b4084 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,3 @@ source "https://rubygems.org" gem 'rspec' -gem 'pry-debugger' +gem 'pry-byebug' diff --git a/sort.rb b/sort.rb new file mode 100644 index 0000000..37482e6 --- /dev/null +++ b/sort.rb @@ -0,0 +1,42 @@ +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 + 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 diff --git a/spec/sort_spec.rb b/spec/sort_spec.rb new file mode 100644 index 0000000..65cb097 --- /dev/null +++ b/spec/sort_spec.rb @@ -0,0 +1,26 @@ +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 +end \ No newline at end of file From 6560382549b60bdaa38feca7d9c4eff04ca502f8 Mon Sep 17 00:00:00 2001 From: Travis Hooper Date: Fri, 5 Sep 2014 00:09:15 -0500 Subject: [PATCH 2/2] Added a second bubble sorting method that should be quicker to run through --- sort.rb | 23 +++++++++++++++++++++++ spec/sort_spec.rb | 10 ++++++++++ 2 files changed, 33 insertions(+) diff --git a/sort.rb b/sort.rb index 37482e6..8df1a8f 100644 --- a/sort.rb +++ b/sort.rb @@ -15,6 +15,28 @@ def self.select_sort(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 @@ -39,4 +61,5 @@ def self.bubble_sort(array) end array end + end diff --git a/spec/sort_spec.rb b/spec/sort_spec.rb index 65cb097..b22998f 100644 --- a/spec/sort_spec.rb +++ b/spec/sort_spec.rb @@ -23,4 +23,14 @@ 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 \ No newline at end of file