diff --git a/sorting/selection_sort/selection_sort.cs b/sorting/selection_sort/selection_sort.cs new file mode 100644 index 000000000..27e7bed21 --- /dev/null +++ b/sorting/selection_sort/selection_sort.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; + +namespace Hacktoberfest +{ + class Program + { + static void Main(string[] args) + { + List someInput = new List() + { + 37, 253, 0, 6213, 323, -5376, 1, 48, 5, + -99999, 64, -55, 820, 2, 47, 8, -5, 48, + 3293, 683, 304, -8, 93, 400, 42, 42, 42 + }; + + Console.WriteLine(string.Join(", ", SelectionSort(someInput))); + } + + private static List SelectionSort(List list) where T : IComparable + { + for (int i = 0; i < list.Count - 1; i++) + { + int minIndex = i; + for (int j = i + 1; j < list.Count; j++) + { + if (list[j].CompareTo(list[minIndex]) < 0) + { + minIndex = j; + } + } + + T temp = list[i]; + list[i] = list[minIndex]; + list[minIndex] = temp; + } + + return list; + } + } +}