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

complete w/ quick sort #57

Open
wants to merge 2 commits into
base: master
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# assignment_sort
Insertion and Merge Sort assignment


Richard Bell
31 changes: 31 additions & 0 deletions bubble.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# implementation of bubble sort on an array
class BubbleSort
def initialize(input_array)
@input = input_array
@flipping = true
end

def sort
while @flipping
@flipping = false
(@input.length - 1).times do |i|
flip(i) if @input[i + 1] < @input[i]
end
end
@input
end

private

def flip(index)
@flipping = true
@input[index], @input[index + 1] = @input[index + 1], @input[index]
end
end


# test code
if __FILE__ == $PROGRAM_NAME
bub = BubbleSort.new([2, 3, 1,5,3,6,-4,2,8,9,14])
puts bub.sort.inspect
end
41 changes: 41 additions & 0 deletions insertion.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# implementation of insertion sort on an array
class InsertionSort
def initialize(input_array)
@input = input_array
end

def sort
(@input.length - 1).times do |i|
insert(@input, i, @input[i + 1])
end

@input
end

private

def insert(array, right_index, value)
# value is the value of array[right_index + 1]
# right_index is the furthest right sorted element

# Step through sorted elements right to left.
# As long as your value is less than the element
# at array[i] and you still have elements
i = right_index
while (i >= 0 && array[i] > value)
# copy the element
array[i + 1] = array[i]
i -= 1
end

# insert the actual element
array[i + 1] = value
end
end


# test code
if __FILE__ == $PROGRAM_NAME
ins = InsertionSort.new([2, 3, 1,7,4,5,2,9,0,-5,3])
puts ins.sort.inspect
end
36 changes: 36 additions & 0 deletions merge.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# implementation of merge sort on an array
class MergeSort
def initialize(input_array)
@input = input_array
end

def sort
merge_sort(@input)
end

private

def merge_sort(array)
return array if array.length == 1
middle = (array.length / 2).floor - 1
left = merge_sort(array[0..middle])
right = merge_sort(array[(middle + 1)..-1])
merge(left, right)
end

def merge(array_left, array_right)
merged = []
until array_left.empty? || array_right.empty?
array_left[0] < array_right[0] ? merged << array_left.shift : merged << array_right.shift
end
merged += array_right if array_left.empty?
merged += array_left if array_right.empty?
merged
end
end

# test code
if __FILE__ == $PROGRAM_NAME
merge = MergeSort.new([2, 3, 1, 5, 7, -5, 4, 14, 1, 0, 17])
puts merge.sort.inspect
end
30 changes: 30 additions & 0 deletions quicksort.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# implementation of quick sort on an array
class QuickSort
def initialize(input_array)
@input = input_array
end

def sort
quick_sort(@input)
end

private

def quick_sort(array)
return array if array.length <= 1
pivot = array.delete_at(rand(array.size))
left = []
right = []
array.each do |e|
e < pivot ? left << e : right << e
end
left << pivot
quick_sort(left) + quick_sort(right)
end
end

# test code
if __FILE__ == $PROGRAM_NAME
quick = QuickSort.new([2, 4, 3, 1, 5, -7, 0, 15, 4, 8, 12])
puts quick.sort.inspect
end