直接選擇排序

package acm.result.test1;

/**
 * @author BTiger
 *
 */
public class Main {

 /**
  * 直接選擇排序 9 19 21 22 23 25 59 63 70 73
  *
  * @param args
  */
 public static void main(String[] args) {
  int[] test = new int[10];
  for (int i = 0; i < 10; i++) {
   test[i] = (int) (Math.random() * 100);
  }
  for (int i = 0; i < test.length; i++) {
   System.out.print(test[i] + " ");
  }
  System.out.println();

  int temp = 0;
  for (int j = 0; j < test.length - 1; j++) {
   int min = j;
   // 找出最小的小標,將它和test[j]交換
   for (int i = j + 1; i < test.length; i++) {
    if (test[min] > test[i]) {
     min = i;
    }
   }
   temp = test[j];
   test[j] = test[min];
   test[min] = temp;
   System.out.print("第"+j+"次排序");
   for (int i = 0; i < test.length; i++) {
    System.out.print(test[i] + " ");
   }
   System.out.println();
  }

 }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章