From 00ba2430433d548cbf46880ce6a77151f85ca849 Mon Sep 17 00:00:00 2001 From: Pere Date: Tue, 12 Jul 2022 10:43:14 +0200 Subject: [PATCH] SelectionSort csharp using array as input --- .../csharp/01_selection_sort/Program.cs | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/02_selection_sort/csharp/01_selection_sort/Program.cs b/02_selection_sort/csharp/01_selection_sort/Program.cs index 4b5a1bd0..77d0a09a 100644 --- a/02_selection_sort/csharp/01_selection_sort/Program.cs +++ b/02_selection_sort/csharp/01_selection_sort/Program.cs @@ -37,5 +37,24 @@ private static int FindSmallest(List arr) } return smallestIndex; } + + public static int[] SelectionSort(int[] unorderedArray) + { + for (var i = 0; i < unorderedArray.Length; i++) + { + var smallestIndex = i; + + for (var remainingIndex = i + 1; remainingIndex < unorderedArray.Length; remainingIndex++) + { + if (unorderedArray[remainingIndex] < unorderedArray[smallestIndex]) + { + smallestIndex = remainingIndex; + } + } + (unorderedArray[i], unorderedArray[smallestIndex]) = (unorderedArray[smallestIndex], unorderedArray[i]); + } + + return unorderedArray; + } } -} +} \ No newline at end of file