From 40c21942efb4e191033cf9f125e0110b03f70442 Mon Sep 17 00:00:00 2001 From: Vaibhav Rai Date: Thu, 28 Sep 2017 18:48:29 +0530 Subject: [PATCH 1/2] Create Selection_Sort.rb Selection_Sort in Ruby --- Selection_Sort.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Selection_Sort.rb diff --git a/Selection_Sort.rb b/Selection_Sort.rb new file mode 100644 index 00000000..3314ead6 --- /dev/null +++ b/Selection_Sort.rb @@ -0,0 +1,18 @@ +def selection_sort(array) + n = array.length - 1 + i = 0 + while i <= n - 1 + smallest = i + j = i + 1 + while j <= n + smallest = j if array[j] < array[smallest] + j += 1 + end + array[i], array[smallest] = array[smallest], array[i] if i != smallest + i += 1 + end +end + +arr = ([9,8,3,1,2,55,68,48].shuffle) #We have taken a rondom example and also shuffling it +selection_sort(arr) +puts "Sorted array is: #{arr.inspect}" From 66127e3eacf973a2567ed86f2f40257e83080ade Mon Sep 17 00:00:00 2001 From: Vaibhav Rai Date: Thu, 28 Sep 2017 18:58:07 +0530 Subject: [PATCH 2/2] Update README.md Updated Readme --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index 56a76d9a..83dac120 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,23 @@ __Properties__ ###### View the algorithm in [action][insertion-toptal] +### Selection Sort +![alt text][selection-image] + +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) + +###### View the algorithm in [action][selection-toptal] + + +[selection-toptal]: https://www.toptal.com/developers/sorting-algorithms/selection-sort +[selection-wiki]: https://en.wikipedia.org/wiki/Selection_sort +[selection-image]: https://upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Selection_sort_animation.gif/250px-Selection_sort_animation.gif "Selection Sort Sort" + [bubble-toptal]: https://www.toptal.com/developers/sorting-algorithms/bubble-sort [bubble-wiki]: https://en.wikipedia.org/wiki/Bubble_sort