public class SelectSort {
public static void selectSort(int[] a) {
int minIndex = 0;
int temp = 0;
if ((a == null) || (a.length == 0))
return;
for (int i = 0; i < a.length - 1; i++) {
minIndex = i;// 无序区的最小数据数组下标
for (int j = i + 1; j < a.length; j++) {
// 在无序区中找到最小数据并保存其数组下标
if (a[j] < a[minIndex]) {
minIndex = j;
}
}
if (minIndex != i) {
// 如果不是无序区的最小值位置不是默认的第一个数据,则交换之。
temp = a[i];
a[i] = a[minIndex];
a[minIndex] = temp;
}
System.out.println("第" + (i + 1) + "趟:");
printArray(a);
}
}
private static void printArray(int[] source) {
for (int i = 0; i < source.length; i++) {
System.out.print("\t" + source[i]);
}
System.out.println();
}
public static void main(String[] args) {
int[] source = { 70,30,40,10,80,20,90,100,75,60,45};
System.out.println("初始关键字:");
printArray(source);
selectSort(source);
System.out.println("\n\n排序后:");
printArray(source);
}
}初始关键字: 70 30 40 10 80 20 90 100 75 60 45 第1趟: 10 30 40 70 80 20 90 100 75 60 45 第2趟: 10 20 40 70 80 30 90 100 75 60 45 第3趟: 10 20 30 70 80 40 90 100 75 60 45 第4趟: 10 20 30 40 80 70 90 100 75 60 45 第5趟: 10 20 30 40 45 70 90 100 75 60 80 第6趟: 10 20 30 40 45 60 90 100 75 70 80 第7趟: 10 20 30 40 45 60 70 100 75 90 80 第8趟: 10 20 30 40 45 60 70 75 100 90 80 第9趟: 10 20 30 40 45 60 70 75 80 90 100 第10趟: 10 20 30 40 45 60 70 75 80 90 100 排序后: 10 20 30 40 45 60 70 75 80 90 100
==================================================================================================
作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
转载请保留原文地址:http://blog.csdn.net/ouyang_peng
==================================================================================================
我的Java开发学习之旅------>Java经典排序算法之选择排序
原文地址:http://blog.csdn.net/ouyang_peng/article/details/46546137