-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
24 lines (21 loc) · 933 Bytes
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int[] arr = new[] { 1, 7, 3, 2, 9, 5, 4, 2, 7, 3, 8, 6 };
// Iterate over the array. Each iteration finds the minimum element in the unsorted part
for (int index = 0; index < arr.Length - 1; index++)
{
// Assume the first element in the unsorted part is the minimum
var currentMinValue = arr[index];
var currentMinIndex = index;
// Iterate over the unsorted part of the array
for (int compareIndex = index + 1; compareIndex < arr.Length; compareIndex++)
{
// If a smaller element is found, update currentMinValue and currentMinIndex
if (currentMinValue > arr[compareIndex])
{
currentMinValue = arr[compareIndex];
currentMinIndex = compareIndex;
}
}
// Swap the found minimum element with the first element in the unsorted part
Swap(ref arr[index], ref arr[currentMinIndex]);
}
void Swap(ref int x1, ref int x2) => (x2, x1) = (x1, x2);