選擇排序

選擇排序:從第一個元素逐次選定爲最小,然後將其依次與後面元素比較

package com.mao.bubble;

public class SelectionSort {
	public static void main(String[] args) {
		int[] array = {23,1,243,12,9,5,7};
		//外層控制:比較輪數
		for (int i = 0; i < array.length - 1; i++) {
			//內層控制:每一輪的比較過程
			for (int j = i + 1; j < array.length; j++) {
				if (array[j] < array[i]) {
					int temp = array[i];
					array[i] = array[j];
					array[j] = temp;
				}
			}
			printArray(array);
		}
	}
	//打印數組
	public static void printArray(int[] a) {
		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i] + "\t");
		}
		System.out.println();
	}
}

 

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