排序之选择排序

package chooseSort;

/**
 * 选择排序
 * 遍历剩余无序数组,找到最小值并交换到适当的位置
 */
public class ChooseSort extends Example {
    @Override
    public void sort(Comparable[] a) {
        int n = a.length;
        int min;
        for(int i=0;i<n-1;i++){
            min = i;
            for(int j=i+1;j<n;j++){
                if(less(a[j],a[min])){
                    min = j;
                }
            }
            exch(a,i,min);
        }
    }

    /**
//     * 测试用例
//     * @param args
//     */
////    public static void main(String[] args) {
////        Integer[] a = new Integer[]{34,2,5,4,45,6,22};
////        ChooseSort sort = new ChooseSort();
////        sort.sort(a);
////        show(a);
////        System.out.println(isSorted(a));
////
////    }
}

 

posted @ 2019-06-20 15:17  由走啦啦啦  阅读(114)  评论(0编辑  收藏  举报