Skip to content

Instantly share code, notes, and snippets.

@aabir
Created November 20, 2019 04:19
Show Gist options
  • Save aabir/d0a5e609788d6c4cec77431d825bfaff to your computer and use it in GitHub Desktop.
Save aabir/d0a5e609788d6c4cec77431d825bfaff to your computer and use it in GitHub Desktop.
Selection Sort in C#
using System;
namespace SelectionSort
{
class Program
{
static void Main(string[] args)
{
int[] inputAry = new int[] { 39, 12, 33, 68, 44 };
int aryLen = inputAry.Length;
int min;
for (int i = 0; i < aryLen; i++)
{
min = i;
for (int j = i + 1; j < aryLen; j++)
{
if(inputAry[j] < inputAry[min])
{
min = j;
}
}
if (min != i)
{
var temp = inputAry[i];
inputAry[i] = inputAry[min];
inputAry[min] = temp;
}
}
for(int i = 0; i < inputAry.Length; i++)
{
Console.WriteLine(inputAry[i] + " ");
}
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment