Created
September 11, 2017 19:16
-
-
Save Nitesh-Mishra/f77bf9fec98f20cb3d9bb0923435f6d3 to your computer and use it in GitHub Desktop.
Selection Sort program in ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # selection_sort.rb | |
| # | |
| # $ ruby selection_sort.rb | |
| # [1, 2, 3, 4, 5, 6, 7, 8] | |
| def selection_sort(array) | |
| n = array.length-1 | |
| for i in 0..n-1 | |
| min = i | |
| j = i + 1 | |
| for j in j..n | |
| if array[min] > array[j] | |
| min = j | |
| end | |
| end | |
| if i != min | |
| array[i],array[min] = array[min],array[i] | |
| end | |
| end | |
| end | |
| array = [6,5,3,1,8,7,2,4] | |
| selection_sort array | |
| print array |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment