From 355f0c8dc83113df39a2634fd362ea3126ade573 Mon Sep 17 00:00:00 2001 From: xxDOOMbox Date: Sun, 1 Oct 2017 16:31:40 -0400 Subject: [PATCH] add mergesort --- BogoSort.rb | 2 +- BubbleSort.rb | 2 +- InsertionSort.rb | 2 +- MergeSort.rb | 25 +++++++++++++++++++++++++ README.md | 29 ++++++++++++++++++++--------- 5 files changed, 48 insertions(+), 12 deletions(-) create mode 100644 MergeSort.rb diff --git a/BogoSort.rb b/BogoSort.rb index 1ff0f61..3f7c587 100644 --- a/BogoSort.rb +++ b/BogoSort.rb @@ -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('') diff --git a/BubbleSort.rb b/BubbleSort.rb index 9ff40f9..fb5f5ca 100644 --- a/BubbleSort.rb +++ b/BubbleSort.rb @@ -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) diff --git a/InsertionSort.rb b/InsertionSort.rb index 25588d8..25097d9 100644 --- a/InsertionSort.rb +++ b/InsertionSort.rb @@ -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) diff --git a/MergeSort.rb b/MergeSort.rb new file mode 100644 index 0000000..fd6a860 --- /dev/null +++ b/MergeSort.rb @@ -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)}" \ No newline at end of file diff --git a/README.md b/README.md index 83dac12..8849d07 100644 --- a/README.md +++ b/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"