阅读:1781次   评论:0条   更新时间:2014-03-26    
基本思想:
在要排序的一组数中,选出最小的一个数与第一个位置的数交换;然后在剩下的数当中再找最小的与第二个位置的数交换,如此循环到倒数第二个数和最后一个数比较为止。

package test;
public class Sort {
	public static void main(String[] args) {
		selectSort();
	}
	public static void selectSort() {
		int a[] = { 9, 8, 2, 6, 4, 0, 3, 7, 5, 1 };
		for (int k = 0; k < a.length; k++){
			System.out.print(a[k] + "--");
		}
		System.out.println();
		
		int position = 0;
		for (int i = 0; i < a.length; i++) {
			int j = i + 1; // 我只关心我后面的内容
			position = i;
			int temp = a[i];
			for (; j < a.length; j++) {
				if (a[j] < temp) { // 找到我后面最小的值
					temp = a[j];
					position = j;
				}
			}
			a[position] = a[i]; // 互换位置
			a[i] = temp;
			
			for (int k = 0; k < a.length; k++){
				System.out.print(a[k] + "--");
			}
			System.out.println();
		}
	}
}


输出内容:
9--8--2--6--4--0--3--7--5--1--
0--8--2--6--4--9--3--7--5--1--
0--1--2--6--4--9--3--7--5--8--
0--1--2--6--4--9--3--7--5--8--
0--1--2--3--4--9--6--7--5--8--
0--1--2--3--4--9--6--7--5--8--
0--1--2--3--4--5--6--7--9--8--
0--1--2--3--4--5--6--7--9--8--
0--1--2--3--4--5--6--7--9--8--
0--1--2--3--4--5--6--7--8--9--
0--1--2--3--4--5--6--7--8--9--
评论 共 0 条 请登录后发表评论

发表评论

您还没有登录,请您登录后再发表评论

文章信息

Global site tag (gtag.js) - Google Analytics