using system; using system.collections.generic; using system.linq; using system.text; namespace sort { class selectsorter { public static int[] sort(int[] a) { selectsort(a); return a; } private static void selectsort(int[] myarray) { int i, j, smallest; //数据起始位置,从0到倒数第二个数据 for (i = 0; i < myarray.length - 1; i++) { smallest = i;//记录最小数据的下标 for (j = i + 1; j < myarray.length; j++) { //在剩下的数据中寻找最小数据 if (myarray[j] < myarray[smallest]) { smallest = j;//如果有比它更小的,记录下标 } } //将最小数据和未排序的第一个数据交换 swap(ref myarray[i], ref myarray[smallest]); } } private static void swap(ref int left, ref int right) { int temp; temp = left; left = right; right = temp; } } }
选择排序的思想:
例子:
以上就是c# 选择排序的内容。
