/** * 此类举例选择法排序 * 思想:从数据中选择最小的同第一个值交换, * 在从剩下的部分中选择最小的与第二个交换,这样往复下去 */package sort; /** * @author 杨忠盛 * @version 1.0 ,2007-3-21 * 下午03:23:10 */public class SelectSort { /**创建一新数组*/ private static int[] numArray = {5,12,2,63,22,6,54,42}; /**计算新数组的长度*/ private static int count = numArray.length; /** * 构造方法 */ protected SelectSort() { } /** * 程序的唯一入口 main 方法 * @param args */ public static void main(String[] args) { System.out.print("选择法排序结果(从大到小排序):/t"); int iTemp; int iPos; for(int i=0;i<count;i++){ iTemp = numArray[i];//假设第 i 个数是最大的数 iPos = i;//存储最大数的位置 for(int j=i+1;j<count;j++){ if(numArray[j] > iTemp){//通过比较选择最大的数 iTemp = numArray[j]; iPos = j; } } numArray[iPos] = numArray[i];//把第i个位置的数存入第j(i+1)个位置, numArray[i] = iTemp;//把比较出来的最大的数存入第i个位置 /** * 一次循环结束后,i 跳到下一个位置 ,也就是原来的numArray[i]=numArray[i+1] */ System.out.print(numArray[i] + "/t"); } }}