Skip to content

Instantly share code, notes, and snippets.

@serkansendur
Created June 1, 2013 21:08
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 serkansendur/5691737 to your computer and use it in GitHub Desktop.
Save serkansendur/5691737 to your computer and use it in GitHub Desktop.
C# bubble and selection sort
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public class Sorting
{
public static void BubbleSort<T>(T[] input) where T : IComparable<T>
{
for (int x = 0; x < input.Length; x++)
{
for (int y = 0; y < input.Length - 1; y++)
{
if (input[y].CompareTo(input[y + 1]) > 0)
{
T temp = input[y + 1];
input[y + 1] = input[y];
input[y] = temp;
}
}
}
}
public static void SelectionSort<T>(T[] input) where T : IComparable<T>
{
for (int x = 0; x < input.Length; x++)
{
int index_of_min = x;
for (int y = x; y < input.Length; y++)
{
if (input[index_of_min].CompareTo(input[y]) > 0)
{
index_of_min = y;
}
}
T temp = input[x];
input[x] = input[index_of_min];
input[index_of_min] = temp;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment