Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
private static int[] SelectionSort(int[] value)
{
if (value != null && value.Length > 1)
{
for (var targetIndex = 0; targetIndex < value.Length - 1; ++targetIndex)
{
var minIndex = targetIndex;
for (var index = targetIndex + 1; index < value.Length; ++index)
{
if (value[index] < value[minIndex])
{
minIndex = index;
}
}
if (minIndex != targetIndex) // if we found a minimum in the rest of the array, swap
{
var tmp = value[minIndex];
value[minIndex] = value[targetIndex];
value[targetIndex] = tmp;
}
}
}
return value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment