SelectionSort csharp using array as input (#226)

Co-authored-by: Pere <pere.frontera@fdsa.es>
This commit is contained in:
Pere Frontera
2023-07-19 17:49:01 +02:00
committed by GitHub
parent fa75dc144d
commit f53fe3b98a

View File

@@ -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;
}
}
}
}