Java实现选择排序

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+" ");

}

}

posted @ 2018-05-07 22:03  Loading~  阅读(112)  评论(0编辑  收藏  举报