From 6fb3a8977aa93d27d12f2ec4b2779bd73d417724 Mon Sep 17 00:00:00 2001 From: Elliot Date: Tue, 31 Oct 2017 20:23:17 -0500 Subject: [PATCH] selection sort in c# --- sorting/selection_sort/selection_sort.cs | 41 ++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 sorting/selection_sort/selection_sort.cs 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; + } + } +}