import java.util.Arrays; //================================================= // File Name : Select_Sort //------------------------------------------------------------------------------ // Author : Common //类名:Arrays_Select //属性: //方法: class Arrays_Select{ private int[] arrays; private int curNum; public Arrays_Select(int max) { //建立一个max长度的空数组 super(); arrays = new int[max]; curNum = 0; } public void insert(int value){ //往空的数组里面增加元素 arrays[curNum] = value; curNum++; } public void display(){ //显示数组 System.out.println(Arrays.toString(arrays)); } private void swap(int one,int two){ //交换 int temp = arrays[one]; arrays[one] = arrays[two]; arrays[two] = temp; } public void SelectSort(){ int out,in,min; for(out=0;out<curNum-1;out++){ min = out; //out从0开始,设第一个为最小 for(in=out+1;in<curNum;in++){ //搜索之后的元素,从1开始,如果有比第0个小,交换 if(arrays[in]<arrays[min]){ //都是找到未排序中最小的数 min = in; //找到最小数的标号 } swap(out,min); //交换 } } } } //主类 //Function : Select_Sort public class Select_Sort { public static void main(String[] args) { // TODO 自动生成的方法存根 int maxSize = 100; Arrays_Select arrays_demo = new Arrays_Select(maxSize); arrays_demo.insert(58); arrays_demo.insert(57); arrays_demo.insert(56); arrays_demo.insert(60); arrays_demo.insert(59); arrays_demo.display(); arrays_demo.SelectSort(); arrays_demo.display(); } }