Skip to content

Instantly share code, notes, and snippets.

@milang
Created May 26, 2010 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save milang/414931 to your computer and use it in GitHub Desktop.
Save milang/414931 to your computer and use it in GitHub Desktop.
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