我正在研究CLRS(算法导论,第3版)。我已经完成了练习2.2-2,它要求实现选择排序算法。我用C#实现了整个算法。有人可以验证我确实实现了选择排序还是我实现了其他类型的排序?该程序运行并对数组进行排序。
using System;
namespace selectionsort{
public class MainClass{
public static void Main(string[] args){
int[] numbers = {55,20,74,46,71,63};
Console.WriteLine("unsorted: " + String.Join(",", numbers));
selectionsort(numbers);
Console.WriteLine("sorted: " + String.Join(",", numbers));
}
public static void selectionsort(int[] A){
for(int i=0; i<A.Length; i++){
int smallest = i;
//find smallest
for(int j=i; j<A.Length; j++){
if(A[j] < A[smallest]){
smallest = j;
}
}
//exchange A[i] with smallest
int temp = A[i];
A[i] = A[smallest];
A[smallest] = temp;
}
}
}
}