mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-27 21:58:57 +01:00
add mergesort
This commit is contained in:
parent
5cd15d08d8
commit
355f0c8dc8
5 changed files with 48 additions and 12 deletions
|
@ -13,6 +13,6 @@ class Array
|
|||
end
|
||||
end
|
||||
|
||||
puts "Enter a list of numbers seprated by space"
|
||||
puts "Enter a list of numbers separated by spaces"
|
||||
str = gets.chomp.split('')
|
||||
puts str.bogosort.join('')
|
||||
|
|
|
@ -15,7 +15,7 @@ def bubble_sort(array)
|
|||
|
||||
array
|
||||
end
|
||||
puts "Enter a list of numbers seprated by space"
|
||||
puts "Enter a list of numbers separated by spaces"
|
||||
|
||||
list = gets
|
||||
bubble_sort(list)
|
||||
|
|
|
@ -10,7 +10,7 @@ def insertion_sort(array)
|
|||
end
|
||||
array
|
||||
end
|
||||
puts "Enter a list of numbers seprated by space"
|
||||
puts "Enter a list of numbers separated by spaces"
|
||||
|
||||
list = gets
|
||||
insertion_sort(list)
|
||||
|
|
25
MergeSort.rb
Normal file
25
MergeSort.rb
Normal file
|
@ -0,0 +1,25 @@
|
|||
def mergesort(arr)
|
||||
return arr if arr.length <= 1
|
||||
m = arr.length / 2
|
||||
l = mergesort(arr[0...m])
|
||||
r = mergesort(arr[m...arr.length])
|
||||
merge(l, r)
|
||||
end
|
||||
|
||||
def merge(l, r)
|
||||
aux = []
|
||||
|
||||
until l.empty? || r.empty?
|
||||
if l.first < r.first
|
||||
aux << l.shift
|
||||
else
|
||||
aux << r.shift
|
||||
end
|
||||
end
|
||||
|
||||
aux.concat(l).concat(r)
|
||||
end
|
||||
|
||||
puts "Enter a list of numbers separated by spaces"
|
||||
arr = gets.chomp.split.map(&:to_i)
|
||||
puts "Sorted: #{mergesort(arr)}"
|
29
README.md
29
README.md
|
@ -13,13 +13,21 @@ Add comments here
|
|||
From [Wikipedia][bubble-wiki]: Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.
|
||||
|
||||
__Properties__
|
||||
* Worst case performance O(n^2)
|
||||
* Best case performance O(n)
|
||||
* Average case performance O(n^2)
|
||||
* Worst case performance O(n^2)
|
||||
* Best case performance O(n)
|
||||
* Average case performance O(n^2)
|
||||
|
||||
###### View the algorithm in [action][bubble-toptal]
|
||||
|
||||
### Merge Sort
|
||||
![alt text][mergesort-image]
|
||||
|
||||
From [Wikipedia][mergesort-wiki]:
|
||||
Conceptually, a merge sort works as follows:
|
||||
|
||||
1. Divide the unsorted list into n sublists, each containing 1 element (a list of 1 element is considered sorted).
|
||||
|
||||
2. Repeatedly merge sublists to produce new sorted sublists until there is only 1 sublist remaining. This will be the sorted list.
|
||||
|
||||
### Insertion Sort
|
||||
![alt text][insertion-image]
|
||||
|
@ -27,9 +35,9 @@ __Properties__
|
|||
From [Wikipedia][insertion-wiki]: Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort.
|
||||
|
||||
__Properties__
|
||||
* Worst case performance O(n^2)
|
||||
* Best case performance O(n)
|
||||
* Average case performance O(n^2)
|
||||
* Worst case performance O(n^2)
|
||||
* Best case performance O(n)
|
||||
* Average case performance O(n^2)
|
||||
|
||||
###### View the algorithm in [action][insertion-toptal]
|
||||
|
||||
|
@ -40,9 +48,9 @@ __Properties__
|
|||
From [Wikipedia][selection-wiki]: The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.
|
||||
|
||||
__Properties__
|
||||
* Worst case performance O(n^2)
|
||||
* Best case performance O(n^2)
|
||||
* Average case performance O(n^2)
|
||||
* Worst case performance O(n^2)
|
||||
* Best case performance O(n^2)
|
||||
* Average case performance O(n^2)
|
||||
|
||||
###### View the algorithm in [action][selection-toptal]
|
||||
|
||||
|
@ -59,3 +67,6 @@ __Properties__
|
|||
[insertion-toptal]: https://www.toptal.com/developers/sorting-algorithms/insertion-sort
|
||||
[insertion-wiki]: https://en.wikipedia.org/wiki/Insertion_sort
|
||||
[insertion-image]: https://upload.wikimedia.org/wikipedia/commons/7/7e/Insertionsort-edited.png "Insertion Sort"
|
||||
|
||||
[mergesort-wiki]: https://en.wikipedia.org/wiki/Merge_sort
|
||||
[mergesort-image]: https://upload.wikimedia.org/wikipedia/commons/c/c5/Merge_sort_animation2.gif "Merge Sort"
|
||||
|
|
Loading…
Reference in a new issue