選擇排序

選擇排序

/** * 選擇排序 * Created by xueping.you on 15-8-5. */
public class ChooseSort {
    private final static Logger logger = LoggerFactory.getLogger(ChooseSort.class);

    public static void chooseSort(int []unSortArray){
        for(int i=0; i<unSortArray.length; i++){
            int temp = unSortArray[i];
            int smallestIndex = i;
            for(int j=i+1; j<unSortArray.length;j++){
                if(unSortArray[j]<temp){
                    smallestIndex=j;
                    temp=unSortArray[j];
                }
            }
            if(smallestIndex!=i){
                unSortArray[smallestIndex]=unSortArray[i];
                unSortArray[i]=temp;

            }
        }
    }

    public static void main(String []args){
        int [] array = new int[]{12,10,2,45,31,56,1,9};
        logger.info("before:{}", array);
        chooseSort(array);
        logger.info("after:{}",array);

    }

}

result:
19:28:00.208 [main] INFO com.qyou.data.arithmetic.ChooseSort – before:[12, 10, 2, 45, 31, 56, 1, 9]
19:28:00.217 [main] INFO com.qyou.data.arithmetic.ChooseSort – after:[1, 2, 9, 10, 12, 31, 45, 56]

点赞