Skip to content
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
44 changes: 44 additions & 0 deletions bubble/lib/bubble_sort.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class BubbleSort

def sort(array)

array.length.times do |index|
bubble_element(array, array.length-1)
end
# previous_index = 0
# current_index = 1
#
# until current_index == array.length
#
# previous = array[previous_index]
# current = array[current_index]
# if current < previous
# array[previous_index] = current
# array[current_index] = previous
# end
#
# previous_index += 1
# current_index += 1
# end
array
end

def bubble_element(array, sort_length)
previous_index = 0
current_index = 1

until current_index == sort_length

previous = array[previous_index]
current = array[current_index]
if current < previous
array[previous_index] = current
array[current_index] = previous
end

previous_index += 1
current_index += 1
end
array
end
end
31 changes: 31 additions & 0 deletions bubble/test/bubble_sort_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'minitest'
require 'minitest/autorun'
require 'minitest/pride'
require './bubble/lib/bubble_sort'

class BubbleSortTest < Minitest::Test

def test_bubble_class_exists
bubble = BubbleSort.new

assert_instance_of BubbleSort, bubble
end

def test_bubble_can_sort_two_elements
bubble = BubbleSort.new

assert_equal [1, 2], bubble.sort([2, 1])
end

def test_can_sort_three_elements
bubble = BubbleSort.new

assert_equal [1, 2, 3], bubble.sort([3, 2, 1])
end

def test_can_sort_four_letters
bubble = BubbleSort.new

assert_equal ["a", "b", "c", "d"], bubble.sort(["d", "b", "a", "c"])
end
end
30 changes: 30 additions & 0 deletions insertion/lib/insertion_sort.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class InsertionSort

def sort(array)
result = []
until array.empty?
to_insert = array.shift
result = insert(result, to_insert)
end
result

end

def insert(array, to_insert)

if array.empty?
array[0] = to_insert

else
array.length.times do |index|
if to_insert < array[index]
array.insert(index, to_insert)
return array
end
end
array.push(to_insert)
end
array

end
end
33 changes: 33 additions & 0 deletions insertion/test/insertion_sort_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'minitest'
require 'minitest/autorun'
require 'minitest/pride'
require './insertion/lib/insertion_sort'

class InsertionSortTest < Minitest::Test

def test_insertion_class_exists
insertion = InsertionSort.new

assert_instance_of InsertionSort, insertion
end

def test_bubble_can_sort_two_elements
insertion = InsertionSort.new

assert_equal [1, 2], insertion.sort([2, 1])
end

def test_can_sort_three_elements
#skip
insertion = InsertionSort.new

assert_equal [1, 2, 3], insertion.sort([3, 2, 1])
end

def test_can_sort_four_letters
#skip
insertion = InsertionSort.new

assert_equal ["a", "b", "c", "d"], insertion.sort(["d", "b", "a", "c"])
end
end
Empty file added merge/lib/merge_sort.rb
Empty file.
Empty file added merge/test/merge_sort_test.rb
Empty file.