SelectSort 选择排序

//SelectSort (( O(n²)))
public class TestSelectSort {
    
    public int[] selectSortArray(int[] arr){
        int min_index;
        
        for(int i = 0; i <arr.length - 1; i ++){
            min_index = i;
            for(int j = i + 1; j < arr.length; j ++){
                if(arr[j] < min_index){
                    min_index = j;
                }
                if(min_index != i){
                    int temp;
                    temp = arr[i];
                    arr[i] = arr[min_index];
                    arr[min_index] = temp;
                }
            }
        }
        
        return arr;
    }
    
    public static void main(String[] args) {
        int[] arr = {6,2,4,1,5,9};
        TestSelectSort test = new TestSelectSort();
        test.selectSortArray(arr);
        
        for(int i = 0 ; i < arr.length; i ++){
            System.out.println(arr[i]);
        }
    }

}

选择排序——每次最小/大排在相应的位置 

posted @ 2013-09-05 18:12  Ruth/Christy  阅读(175)  评论(0编辑  收藏  举报