Created
February 4, 2012 11:02
-
-
Save antsmartian/1737086 to your computer and use it in GitHub Desktop.
Alg for selection sort :)
This file contains 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
//change this array and play with selection sort :) | |
a = [2,34,4,123,45,56,3,56,67,87,21,45,56,67,22,4234,4234,2342,1,34,546,67,677,343,4,41,32,456,1341,13,454,234,453,565,5,524,234,234,5456,567,673,245,567,78,78,324,56576,782,435,676,873,2,345,6767,7,3463,3,6767673,34534,2,345,656,3,5652,1,34345,3453545,35345356,66,7883345,676787,8988] | |
b = a | |
long startTime = System.currentTimeMillis(); | |
(0..(a.size()-2)).each { | |
minimumValue = it | |
(it+1).upto(a.size()-1){ insideElement -> | |
if(a[insideElement] < a[minimumValue]) | |
minimumValue = insideElement | |
} | |
swap(it,minimumValue) | |
} | |
long endTime = System.currentTimeMillis(); | |
def swap(def one,def two) | |
{ | |
def temp = a[one] | |
a[one] = a[two] | |
a[two] = temp | |
} | |
println "The selection sort have taken ${endTime-startTime} seconds to finish sorting for ${a.size()} elements \n \n" | |
println a | |
//test cases... these are passing!! Yipeee :D | |
assert b.sort() == a | |
assert b.size() == a.size() |
This file contains 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
a = [2,34,4,123,45,56,3,56,67,87,21,45,56,67,22,4234,4234,2342,1,34,546,67,677,343,4,41,32,456,1341,13,454,234,453,565,5,524,234,234,5456,567,673,245,567,78,78,324,56576,782,435,676,873,2,345,6767,7,3463,3,6767673,34534,2,345,656,3,5652,1,34345,3453545,35345356,66,7883345,676787,8988] | |
b = a | |
long startTime = System.currentTimeMillis(); | |
(0..(a.size()-2)).each { | |
def temp = it | |
minimumValue = it | |
(it+1).upto(a.size()-1){ insideElement -> | |
if(a[insideElement] < a[minimumValue]) | |
minimumValue = insideElement | |
} | |
use(Collections) { | |
a.swap(temp,minimumValue) | |
} | |
} | |
long endTime = System.currentTimeMillis(); | |
println "The selection sort have taken ${endTime-startTime} seconds to finish sorting for ${a.size()} elements \n \n" | |
println a | |
//test cases... these are passing!! Yipeee :D | |
assert b.sort() == a | |
assert b.size() == a.size() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment