- Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathSelectionSorter.cs
48 lines (45 loc) · 1.46 KB
/
SelectionSorter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
usingSystem.Collections.Generic;
usingAlgorithms.Common;
namespaceAlgorithms.Sorting
{
publicstaticclassSelectionSorter
{
publicstaticvoidSelectionSort<T>(thisIList<T>collection,Comparer<T>comparer=null)
{
comparer=comparer??Comparer<T>.Default;
collection.SelectionSortAscending(comparer);
}
/// <summary>
/// Public API: Sorts ascending
/// </summary>
publicstaticvoidSelectionSortAscending<T>(thisIList<T>collection,Comparer<T>comparer)
{
inti;
for(i=0;i<collection.Count;i++){
intmin=i;
for(intj=i+1;j<collection.Count;j++){
if(comparer.Compare(collection[j],collection[min])<0)
min=j;
}
collection.Swap(i,min);
}
}
/// <summary>
/// Public API: Sorts ascending
/// </summary>
publicstaticvoidSelectionSortDescending<T>(thisIList<T>collection,Comparer<T>comparer)
{
inti;
for(i=collection.Count-1;i>0;i--)
{
intmax=i;
for(intj=0;j<=i;j++)
{
if(comparer.Compare(collection[j],collection[max])<0)
max=j;
}
collection.Swap(i,max);
}
}
}
}