选择排序就是不断地从未排序的元素中选择最大(或者最下)的元素放入已经排好序的元素集合中,直到未排序中仅剩一个元素为止
具体实现步骤如图:
private void selectSort() {
ArrayList<Integer> dataSource = new ArrayList<Integer> ();
dataSource .add(1);
dataSource .add(5);
dataSource .add(9);
dataSource .add(7);
dataSource .add(13);
dataSource .add(2);
dataSource .add(8);
dataSource .add(18);
for (int i = 0, len = dataSource.size(); i < len; i++) {
// 当前拿出比较的数据
int value = dataSource.get(i);
int litterIndex = i;
for (int j = i + 1; j < len; j++) {
int tempValue = dataSource.get(j);
if (tempValue < value) {
litterIndex = j;
}
}
if (i != litterIndex) {
Integer obj = dataSource.get(litterIndex);
dataSource.set(litterIndex, value);
dataSource.set(i, obj);
}
}
Log.i("@@@", dataSource.toString());
}