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

assignment_sort #58

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

Chad Lucas
46 changes: 46 additions & 0 deletions bubble_sort.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
def bubble_sort(array)

# unsorted length begins as full length
unsorted_length = array.length

# swapped must begin true
swapped = true

# while we made a swap
while swapped

# assume no swap was made
swapped = false

# for each index in the unsorted length
unsorted_length.times do |index|

# get the current and next value
a = array[index]
b = array[index + 1]

# if the next value is not nil
# and the current is greater than
# the next value
if b && a > b

# swap the values
array[index] = b
array[index + 1] = a

# swapped is true
# so we iterate again
swapped = true
end
end

# decrement unsorted length
# since bubble sort makes
# n-1 of unsorted length
# sorted
unsorted_length -= 1
end

# return array of course
array
end
21 changes: 21 additions & 0 deletions insertion_sort.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def insert(array, right_index, value)
i = right_index

while i >= 0 && array[i] > value
array[i+1] = array[i]
i -= 1
end

array[i+1] = value
end


def insertion_sort(array)
(1...array.length).each do |i|
insert(array, i-1, array[i])
end
array
end


insertion_sort( [3,1,7,2,5] ) # => [1, 2, 3, 5, 7]
52 changes: 52 additions & 0 deletions merge_sort.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
def merge_sort(array)

# store length of array
n = array.length

# base case
return array if n <= 1

# get left upper bound
# and right lower bound indexes
r = l = n / 2

# adjust based on length
# so we don't repeat values
n.even? ? l -= 1 : r += 1

# get array halves
left = merge_sort(array[0..l])
right = merge_sort(array[r..n])

# initialize new array
# to merge onto
merged = []

# until both left and right
# have not values
until left.empty? && right.empty?

# get first value from both sides
l, r = left.first, right.first

# if both are not nil
if l && r

# get the lesser value
value = r < l ? right.shift : left.shift

# merge it
merged << value

elsif l
# else merge the value that
# was not nil
merged << left.shift
elsif r
merged << right.shift
end
end

# return merged array
merged
end