1. 程式人生 > >JAVA 4種常用排序演算法,總是記不住,記錄下來,免得以後番網頁

JAVA 4種常用排序演算法,總是記不住,記錄下來,免得以後番網頁

public class ArraySort {

	public static void main(String[] args) {
		int[] array=getArray();
		selectSort(array);
		//insertSort(array);
		/*bubbleSort(array);
		quickSort(array,0,array.length-1);
		System.out.println("\n快速排序:");
		for(int i:array)
			System.out.print(i+",");*/
	}
	
	public static int[] getArray(){
		int[] array=new int[10];
		for(int i=0;i<array.length;i++){
			array[i]=(int) (Math.random()*100+100);
			System.out.print(array[i]+",");
		}
		return array;
	}
	
	public static void selectSort(int[] array){
		/**
		 * @author ZHI
		 * 選擇排序,先選出待比較值,再在第二次迴圈中從待排序陣列
		 * 中找到最小的或最大的數字放在第一次迴圈的起始位置
		 */
		System.out.println("\n選擇排序:");
		int index;
		for(int i=0;i<array.length-1;i++){
			index=i;
			for(int j=i+1;j<array.length;j++){
				if(array[index]>array[j]){
					index=j;
				}
			}
			if(index!=i){
				int temp=array[i];
				array[i]=array[index];
				array[index]=temp;
			}
		}
		for(int i:array)
			System.out.print(i+",");
	}
	/**
	 * @author ZHI
	 * 插入排序,預設第一個數值為已經排序好的,然後從後面的陣列中拿第一個數字放到前面已經排序
	 * 好的陣列中,在放入過程中不停比較,大於該數字的則向後移一位,直到新數字放入已排序好的數
	 * 組中之後,仍然是排序好的陣列
	 */
	public static void insertSort(int[] array){
		System.out.println("\n插入排序:");
		int key;
		for(int i=1;i<array.length;i++){
			key=array[i];
			int j=i;
			while(j>0&&key<array[j-1]){
				array[j]=array[j-1];
				j--;
			}
			/*for(;j>0&&key<array[j-1];j--)
				array[j]=array[j-1];*/
			array[j]=key;
		}
		for(int i:array)
			System.out.print(i+",");
	}
	/**
	 * @author ZHI
	 * 氣泡排序,比較相鄰的兩個數,不停地找出當前一輪迴圈中最大或最小的值向一邊推過去,
	 * 推過去的一邊即為已經排序好的一邊,不再遍歷。
	 * 模式一:(* 為需要遍歷點)
	 * for(int i=0;i<a.length;i++)
	 *   for(int j=0;j<a.length-i-1;j++)
	 *   
	 *   * * * * * *
	 *   * * * * *
	 *   * * * * 
	 *   * * *
	 *   * * 
	 *   *
	 * 模式二:
	 * for(int i=0;i<a.length;i++)
	 *   for(int j=a.length-1;j>i;j--)
	 *   
	 *   * * * * * *
	 *     * * * * *
	 *       * * * *
	 *         * * *
	 *           * *
	 *             * 
	 */
	public static void bubbleSort(int[] array){
		System.out.println("\n氣泡排序:");
		for(int i=0;i<array.length;i++){
			for(int j=0;j<array.length-i-1;j++){
				if(array[j]>array[j+1]){
					int temp=array[j+1];
					array[j+1]=array[j];
					array[j]=temp;
				}
			}
		}
		for(int i:array)
			System.out.print(i+",");
	}
	/**
	 * @author ZHI
	 * 快速排序,把一個數組分成兩部分,取第一個為關鍵值key,先把右邊小於key的值
	 * 放到左邊,然後再把左邊大於key的值放到右邊,直到上下標相等,得到一箇中間
	 * 下標,把key放到該下標中。最後在中間值下標處把陣列一分為二,分別重複(遞迴)
	 * 上述步驟,直到結束
	 */
	public static void quickSort(int[] array,int start,int end){
		if(start>=end)
			return;   //遞迴出口,所有遞迴支路都會出現上下標相等,這是遞迴結束
		int l=start,r=end;
		int mid;
		int key=array[l];//因為key值取出,故整個陣列有一個空的位置給予陣列移動
		while(l<r){
			while(array[r]>=key && l<r)//右邊大於key的值略過
				--r;
			array[l]=array[r];         //右邊小於key的值放到左邊(第一次時是放在key值的位置,這是空出右邊的位置)
			while(array[l]<=key && l<r)//左邊小於key的值略過
				++l;
			array[r]=array[l];         //左邊大於key的值放到右邊(放到上面空出來的右邊位置)
		}
		array[l]=key;
		mid=l;
		//int mid=partition(array,start,end);
		quickSort(array,start,mid-1); //左遞迴
		quickSort(array,mid+1,end);   //右遞迴
	}
	
/*	public static int partition(int[] array,int start,int end){
		int key=array[start];
		while(start<end){
			while(array[end]>=key && start<end){
				--end;
			}
			array[start]=array[end];
			while(array[start]<=key && start<end){
				start++;
			}
			array[end]=array[start];
		}
		array[start]=key;
		return start;
	}*/
}