Skip to content

Instantly share code, notes, and snippets.

@AbhinavPradeep
Created June 25, 2020 02:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AbhinavPradeep/39372f7ea0e3eed28c4d2b7a4e1bb655 to your computer and use it in GitHub Desktop.
Save AbhinavPradeep/39372f7ea0e3eed28c4d2b7a4e1bb655 to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
namespace SortingArrays
{
class SelectionSort
{
public void Sort(int[] input)
{
var watch = Stopwatch.StartNew();
try
{
int smallestIndex;
for (int i = 0; i < input.Length - 1; i++)
{
smallestIndex = i;
for (int currentIndex = i + 1; currentIndex < input.Length; currentIndex++)
{
if (input[currentIndex] < input[smallestIndex])
{
smallestIndex = currentIndex;
}
}
int TemporaryVariable = input[i];
input[i] = input[smallestIndex];
input[smallestIndex] = TemporaryVariable;
}
watch.Stop();
}
finally
{
PrintArray printArray = new PrintArray();
printArray.PrintIntegerArray(input);
Console.WriteLine($" Selection Sort executed in {watch.ElapsedTicks} ticks");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment