标签:
1 package Sort; 2 3 import org.junit.Test; 4 5 public class SelectSort { 6 7 // 选择排序算法 8 public void selectSort(int [] array) { 9 // 循环数组的长度-1趟 10 for (int i = 0; i < array.length - 1; ++i) { 11 // 用来记录每趟循环的最小值 12 int minNumber = array[i]; 13 // 用来记录每趟循环最小值的下标 14 int temp = i; 15 for (int j = i + 1; j < array.length; ++j) { 16 if (array[j] <= minNumber) { 17 minNumber = array[j]; 18 temp = j; 19 } 20 } 21 if (temp != i) { 22 array[temp] = array[i]; 23 array[i] = minNumber; 24 } 25 } 26 } 27 28 @Test 29 public void test(){ 30 int[] array = {8,2,4,9,3,6}; 31 selectSort(array); 32 Array.print(array); 33 } 34 }
标签:
原文地址:http://www.cnblogs.com/wjf0/p/5873963.html