Java排序

Java排序

一、選擇排序

/**
 * <p>Title: SelectionSort</p>
 * <p>Description: 選擇排序</p>
 */
public class SelectionSort {

	/**
	 * 主方法
	 * <p>Title: main</p>
	 * <p>Description: 主方法</p>
	 * @param args
	 */
	public static void main(String[] args) {
		int[] array = newArray();
		printArray("排序前:", array);
		sortn(array);
		printArray("排序後:", array);
	}
	
	/**
	 * 創建一個數組
	 * <p>Title: newArray</p>
	 * <p>Description: 創建一個數組</p>
	 * @return
	 */
	private static int[] newArray() {
		int[] array = new int[] {5, 7, 1, 3, 9, 2 ,8, 4, 6, 10};
		return array;
	}
	
	/**
	 * 排序
	 * <p>Title: sort</p>
	 * <p>Description: 排序</p>
	 * @param array
	 */
	private static void sort1(int[] array) {
		for (int x = 0; x < array.length - 1; x++) {
			for (int y = x + 1; y < array.length; y++) {
				if (array[x] > array[y]) {
					int temp = array[x];
					array[x] = array[y];
					array[y] = temp;
				}
			}
		}
	}
	
	/**
	 * 排序
	 * <p>Title: sort</p>
	 * <p>Description: 排序</p>
	 * @param array
	 */
	private static void sort2(int[] array) {
		for (int x = array.length - 1; x > 0; x--) {
			for (int y = 0; y < x; y++) {
				if (array[y] > array[y + 1]) {
					int temp = array[y];
					array[y] = array[y + 1];
					array[y + 1] = temp;
				}
			}
		}
	}
	
	/**
	 * 排序
	 * <p>Title: sort</p>
	 * <p>Description: 排序</p>
	 * @param array
	 */
	private static void sort3(int[] array) {
		for (int x = 0; x < array.length - 1; x++) {
			int index = x;
			int num = array[x];
			for (int y = x + 1; y < array.length; y++) {
				if (num > array[y]) {
					index = y;
					num = array[y];
				}
			}
			if (index != x) {
				swap(array, x, index);
			}
		}
	}
	
	/**
	 * 打印數組
	 * <p>Title: printArray</p>
	 * <p>Description: 打印數組</p>
	 * @param str
	 * @param array
	 */
	private static void printArray(String str, int[] array) {
		System.out.print(str);
		for (int x = 0; x < array.length; x++) {
			if (x != array.length -1)
				System.out.print(array[x] + ", ");
			else {
				System.out.println(array[x]);
			}
		}
	}
	
}

二、冒泡排序

/**
 * <p>Title: SelectionSort</p>
 * <p>Description: 選擇排序</p>
 */
public class BubboleSort {

	/**
	 * 主方法
	 * <p>Title: main</p>
	 * <p>Description: 主方法</p>
	 * @param args
	 */
	public static void main(String[] args) {
		int[] array = newArray();
		printArray("排序前:", array);
		sort(array);
		printArray("排序後:", array);
	}
	
	/**
	 * 創建一個數組
	 * <p>Title: newArray</p>
	 * <p>Description: 創建一個數組</p>
	 * @return
	 */
	private static int[] newArray() {
		int[] array = new int[] {5, 7, 1, 3, 9, 2 ,8, 4, 6, 10};
		return array;
	}
	
	/**
	 * 排序
	 * <p>Title: sort</p>
	 * <p>Description: 排序</p>
	 * @param array
	 */
	private static void sort1(int[] array) {
		for (int x = 0; x < array.length - 1; x++) {
			for (int y = 0; y < array.length - 1 - x; y++) {
				if (array[y] > array[y + 1]) {
					int temp = array[y];
					array[y] = array[y + 1];
					array[y + 1] = temp;
				}
			}
		}
	}
	
	/**
	 * 排序
	 * <p>Title: sort</p>
	 * <p>Description: 排序</p>
	 * @param array
	 */
	private static void sort2(int[] array) {
		for (int x = array.length - 1; x > 0; x--) {
			for (int y = 0; y < x; y++) {
				if (array[y] > array[y + 1]) {
					int temp = array[y];
					array[y] = array[y + 1];
					array[y + 1] = temp;
				}
			}
		}
	}
	
	/**
	 * 打印數組
	 * <p>Title: printArray</p>
	 * <p>Description: 打印數組</p>
	 * @param str
	 * @param array
	 */
	private static void printArray(String str, int[] array) {
		System.out.print(str);
		for (int x = 0; x < array.length; x++) {
			if (x != array.length -1)
				System.out.print(array[x] + ", ");
			else {
				System.out.println(array[x]);
			}
		}
	}
	
}

 

發佈了76 篇原創文章 · 獲贊 4 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章