-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSorting.cs
More file actions
39 lines (36 loc) · 1.15 KB
/
Copy pathSelectionSorting.cs
File metadata and controls
39 lines (36 loc) · 1.15 KB
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
using System;
namespace DA.Algorithms.Sorting
{
public static class SelectionSorting
{
/// <summary>
/// The sorting of an array by finding the smallest element
/// of the collection and exchange it with front elements.
/// </summary>
///
/// <typeparam name="T">type of an array</typeparam>
/// <param name="array">reference to the array</param>
public static void Sort<T> (T[] array) where T : IComparable<T>
{
int size = array.Length;
for (int i = 0; i < size - 1; i++)
{
int minValueIndex = i;
for (int current = i + 1; current < size; current++)
{
if (array[current].CompareTo (array[minValueIndex]) < 0)
{
minValueIndex = current;
}
}
Swap (ref array[minValueIndex], ref array[i]);
}
}
public static void Swap<T> (ref T first, ref T second)
{
T temp = first;
first = second;
second = temp;
}
}
}