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 #51

Open
wants to merge 3 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
Binary file added .DS_Store
Binary file not shown.
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

DAVID WIESENBERG
33 changes: 33 additions & 0 deletions bubble_sort.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Implementing Sorting Algorithm

# Warmup: Bubble Sort

def bubble_sort(array_2)
loop do
swapped = false
(0..array_2.length-2).each do |i|
if array_2[i] > array_2[i+1]
temp = array_2[i]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To swap 2 elements, a and b in ruby - a, b = b, a.This will eliminate the use of temp variable.

array_2[i] = array_2[i+1]
array_2[i+1] = temp
swapped = true
end
end
break unless swapped
end
puts "SORTED array_2 = #{array_2}\n\n"
end




bubble_sort([1, 7, 2, 5])

bubble_sort([1, 3, 7, 2, 5])

bubble_sort([1, 3, 7, 7, 2, 5])

bubble_sort([1, 3, 7, 2, 2, 5])

bubble_sort([1, -3, 7, 2, -2, -5, 10])

25 changes: 25 additions & 0 deletions insertion_sort.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Implementing Sorting Algorithm

# Warmup: Insertion Sort

def insertion_sort(array_1)
(0..array_1.length-2).each do |i|
value = array_1[i+1]
while (i >= 0 && array_1[i] > value)
array_1[i+1] = array_1[i]
i -= 1
end
array_1[i+1] = value
end
puts "SORTED array_1 = #{array_2}\n\n"
end


insertion_sort([1, 3, 7, 2, 5])

insertion_sort([1, 3, 7, 7, 2, 5])

insertion_sort([1, 3, 7, 2, 2, 5])

insertion_sort([1, -3, 7, 2, -2, -5, 10])

34 changes: 34 additions & 0 deletions merge_sort.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Implementing Sorting Algorithm

# Merge Sort

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

def merge(left, right)
result = []
until left.length == 0 || right.length == 0 do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

==0 can be replaced by .zero?

result << (left.first <= right.first ? left.shift : right.shift)
end
result + left + right
end





merge_sort([1, 3, 7, 2, 5])
puts "merge_sort([1, 3, 7, 2, 5]) = #{merge_sort([1, 3, 7, 2, 5])}"

merge_sort([1, 3, 7, 7, 2, 5])
puts "merge_sort([1, 3, 7, 7, 2, 5]) = #{merge_sort([1, 3, 7, 7, 2, 5])}"

merge_sort([1, 3, 7, 2, 2, 5])
puts "merge_sort([1, 3, 7, 2, 2, 5]) = #{merge_sort([1, 3, 7, 2, 2, 5])}"

merge_sort([1, -3, 7, 2, -2, -5, 10])
puts "merge_sort([1, -3, 7, 2, -2, -5, 10]) = #{merge_sort([1, -3, 7, 2, -2, -5, 10])}"