Java implements selection sort

public class SelectSort {

public static void selectSort(int [] list){
    for(int i=0;i<list.length;i++){
        int temp = i;
        for(int j=i+1;j<list.length;j++){
            if(list[temp]>list[j])
                temp = j;
        }
        //一次迭代完成 判断最小元素放到前面
        if(temp!=i){
            int result = list[temp];
            list[temp] = list[i];
            list[i] = result;
        }
    }
}
public static void main(String[] args) {
    int [] list = {2,3,2,5,6,1,-2,3,14,12};
    selectSort(list);
    for(int num:list)
        System.out.print(num+" ");

}

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325661464&siteId=291194637