From f53fe3b98abfc85b0d9201747caf0f6959ae7cfe Mon Sep 17 00:00:00 2001 From: Pere Frontera Date: Wed, 19 Jul 2023 17:49:01 +0200 Subject: [PATCH] SelectionSort csharp using array as input (#226) Co-authored-by: Pere --- .../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 4b5a1bd..77d0a09 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 @@ namespace ConsoleApplication } 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