package lsn1.sjjg.cct.cn.lsn1; import org.junit.Test; /** * 选择排序 * * Created by linyaokui on 17/11/28. */ public class SelectSort { @Test public void test(){ int[] array = {5,4,2,1,9,8,7,6,3}; selectSort(array); for(int a : array){ System.out.print(a + " "); } } public void selectSort(int[] array){ for(int i = 0;i<array.length-1;i++){ int index = i; // 选择一个数 for(int j=i+1;j<array.length;j++){ if(array[j] < array[index]){ index = j; } } if(index != i){ // 表示找到过最小值 int temp = array[index]; array[index] = array[i]; array[i] = temp; } } } }
选择排序
最新推荐文章于 2024-09-13 21:39:37 发布