Sorting algorithms/Selection sort: Difference between revisions

no edit summary
(→‎{{header|Scala}}: Alternate solution forgoing extra definition)
No edit summary
Line 215:
 
</lang>
 
=={{header|C sharp|C#}}==
This is a generic implementation that works with any type that implements the IComparable interface
 
<lang csharp>
class SelectionSort<T> where T : IComparable {
public T[] Sort(T[] list) {
int k;
T temp;
 
for (int i = 0; i < list.Length; i++) {
k = i;
for (int j=i + 1; j < list.Length; j++) {
if (list[j].CompareTo(list[k]) < 0) {
k = j;
}
}
temp = list[i];
list[i] = list[k];
list[k] = temp;
}
 
return list;
}
}</lang>
 
Example of usage:
<lang csharp>
String[] str = { "this", "is", "a", "test", "of", "generic", "selection", "sort" };
 
SelectionSort<String> mySort = new SelectionSort<string>();
 
String[] result = mySort.Sort(str);
 
for (int i = 0; i < result.Length; i++) {
Console.WriteLine(result[i]);
}</lang>
 
Output:
<pre>a
generic
is
of
selection
sort
test
this</pre>
 
 
 
=={{header|Clojure}}==
Anonymous user