c#写出一个选择排序法,然后调用此方法对数组
时间: 2023-12-20 16:06:36 浏览: 72
好的,以下是C#代码实现选择排序法:
```csharp
public static void SelectionSort(int[] arr)
{
int n = arr.Length;
// One by one move boundary of unsorted subarray
for (int i = 0; i < n - 1; i++)
{
// Find the minimum element in unsorted array
int min_idx = i;
for (int j = i + 1; j < n; j++)
{
if (arr[j] < arr[min_idx])
{
min_idx = j;
}
}
// Swap the found minimum element with the first element
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
```
调用方法:
```csharp
int[] arr = {64, 25, 12, 22, 11};
SelectionSort(arr);
```
调用后,`arr` 数组将会被排序。
阅读全文