Skip to content

Commit f53fe3b

Browse files
SelectionSort csharp using array as input (egonSchiele#226)
Co-authored-by: Pere <pere.frontera@fdsa.es>
1 parent fa75dc1 commit f53fe3b

File tree

1 file changed

+20
-1
lines changed
  • 02_selection_sort/csharp/01_selection_sort

1 file changed

+20
-1
lines changed

02_selection_sort/csharp/01_selection_sort/Program.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,24 @@ private static int FindSmallest(List<int> arr)
3737
}
3838
return smallestIndex;
3939
}
40+
41+
public static int[] SelectionSort(int[] unorderedArray)
42+
{
43+
for (var i = 0; i < unorderedArray.Length; i++)
44+
{
45+
var smallestIndex = i;
46+
47+
for (var remainingIndex = i + 1; remainingIndex < unorderedArray.Length; remainingIndex++)
48+
{
49+
if (unorderedArray[remainingIndex] < unorderedArray[smallestIndex])
50+
{
51+
smallestIndex = remainingIndex;
52+
}
53+
}
54+
(unorderedArray[i], unorderedArray[smallestIndex]) = (unorderedArray[smallestIndex], unorderedArray[i]);
55+
}
56+
57+
return unorderedArray;
58+
}
4059
}
41-
}
60+
}

0 commit comments

Comments
 (0)