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

Created sorting methods - insert, bubble and merge techniques #48

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

* bubble_sort.rb - implementation of bubble sort algorithm with an array
* insertion_sort.rb - implementation of insertion sort algorithm with an array
* merge_sort.rb - implementation of merge sort algorithm with an array

## Getting Started

If you want to quick run some the examples to see the code in action, run
```
$ ruby example.rb
```
from the project directory.

## Authors

* **Dariusz Biskupski** - *Initial work* - https://dariuszbiskupski.com


## Acknowledgments

It is part of the assignment created for [Viking Code School](https://www.vikingcodeschool.com/)
14 changes: 14 additions & 0 deletions bubble_sort.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def bubble_sort(array)
temp_value = 0
while temp_value != nil
temp_value = nil
(array.length - 1).times do |idx|
if array[idx] > array[idx + 1]
Copy link

@remis1889 remis1889 May 14, 2017

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_value variable.

array[idx], array[idx + 1] = array[idx + 1], array[idx]
end
end
end
puts "#{array}"
end

bubble_sort( [1,3,7,2,5] )
20 changes: 20 additions & 0 deletions insertion_sort.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

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.upto(array.length - 1) do |idx|
element = array[idx]
insert(array, idx - 1, element)
end
puts "#{array}"
end

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

def merge(left_arr, right_arr)
new_arr = []
unless left_arr.nil? || right_arr.nil?
while left_arr.any? || right_arr.any?
if left_arr.empty?
new_arr << right_arr[0]
right_arr = right_arr[1..-1]
elsif right_arr.empty?
new_arr << left_arr[0]
left_arr = left_arr[1..-1]
elsif left_arr[0] > right_arr[0]
new_arr << right_arr[0]
right_arr = right_arr[1..-1]
else
new_arr << left_arr[0]
left_arr = left_arr[1..-1]
end
end
end
new_arr
end

def merge_sort(array)
if array.length == 1
array
else
left_arr = array[0...array.length/2]
right_arr = array[array.length/2..-1]
# // mergeSort() the left half of the array
# merge_sort(left_arr)
# // mergeSort() the right half of the array
# merge_sort(right_arr)
# // merge() the two halves
merge(merge_sort(left_arr), merge_sort(right_arr))
end
end

merge_sort( [1,3,7,2,5,4,30,22,80,50,6] )