直接选择排序

/**
 * 直接插入排序
 * @author TMAC-J
 * 默认按照从小到大的顺序排列
 * 思路:从所有数中选取一个最小的数,用来和第一个数交换,然后再从剩下的数中选取一个最小的数
 * 用来和第二个数交换,重复此操作
 *
 */
public class InsertSort {
    
private int[] array;
    
    public InsertSort(int[] array) {
        this.array = array;
    }
    /**
     * 按从小到大的顺序排列
     */
    public void calculate(){
        boolean isSwitch = false;
        int temp = 0;
        int which = 0;
        for(int i = 0;i<array.length-1;i++){
            for(int j = i;j<array.length-1;j++){
                if(array[j]>array[j+1]){
                    which = j+1;
                    isSwitch = true;
                }
            }
            if(isSwitch){
                temp = array[i];
                array[i] = array[which];
                array[which] = temp;
                isSwitch = false;
            }
        }
    }
    
    public static void main(String[] args) {
        int[] a = {10,9,8,7,6,5,4,3,2,1};
        InsertSort insertSort  = new InsertSort(a);
        insertSort.calculate();
        for(int i = 0;i<a.length;i++){
            System.out.println(a[i]);
        }
    }
    
}

 

posted @ 2017-01-05 20:03  麦子TMAC  阅读(152)  评论(0编辑  收藏  举报