Java字符串05-选择排序
选择排序
package doudou; import java.util.SortedSet; public class test_xuanze { public static void main(String[] args) { // StringBuffer sb= new StringBuffer(); int[] before = { 10, 10, 24, 34, 9, 99, 15, 6, 55, 0, 1, 1, }; xuanze(before); } public static int[] xuanze(int[] before) { int before_len = before.length; if (before_len <= 1) { return before; } for (int i = 0; i < before_len; i++) { //记录最小数的索引 int tmp = i; for (int j = i + 1; j < before_len; j++) { if (before[tmp] > before[j]) { tmp = j;//这轮结束,tmp为最小的索引 } } if (tmp != i) { //交换 int k = before[i]; before[i] = before[tmp]; before[tmp] = k; } } for (int i = 0; i < before_len; i++) { System.out.print(before[i] + "、"); } return before; } }
选择排序的动态图 可参考:https://www.runoob.com/w3cnote/selection-sort.html