Instantly share code, notes, and snippets.

Embed
What would you like to do?
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