1. 程式人生 > >幾種常見排序演算法java實現

幾種常見排序演算法java實現

public class sort {
	public static void main(String[] args) {
		int[] a= {10,1,35,14,9,12,11};
		int c=(int)Math.floor(Math.random()*7);
		switch(c){
			case 1:
				BubbleSort(a);
				break;
			case 2:
				SelectSort(a);
				break;
			case 3:
				InsertSort(a);
				break;
			case 4:
				HeapSort(a);
				break;
			case 5:
				MergeSort(a,0,1);
				break;
			case 6:
				QuickSort(a,0,a.length);
				break;
			default:
				break;
		}
		System.out.println(c);
		System.out.println(Arrays.toString(a));
	}
	//氣泡排序
	public static void BubbleSort(int[] a) {
		for(int i = 0;i<a.length;i++) {
			for(int j=i+1;j<a.length;j++) {
				if(a[i]<a[j]) {
					int tem=a[i];
					a[i]=a[j];
					a[j]=tem;
				} 
			}
		}
		
	}
	//選擇排序
	public static void SelectSort(int[] a) {
		int minIndex = 0;
		int temp = 0;
		if ((a == null) || (a.length == 0))
			return;
		for (int i = 0; i < a.length - 1; i++) {
			minIndex = i;// 無序區的最小資料陣列下標
			for (int j = i + 1; j < a.length; j++) {
				// 在無序區中找到最小資料並儲存其陣列下標
				if (a[j] < a[minIndex]) {
					minIndex = j;
				}
			}
			if (minIndex != i) {
				// 如果不是無序區的最小值位置不是預設的第一個資料,則交換之。
				temp = a[i];
				a[i] = a[minIndex];
				a[minIndex] = temp;
			}
		}
	}
	//插入排序
	public static void InsertSort(int[] a) {
		if (a == null || a.length < 2) {
			return;
		}
		for (int i = 1; i < a.length; i++) {
			for (int j = i; j > 0; j--) {
				if (a[j] < a[j - 1]) {					
					int temp = a[j];
					a[j] = a[j - 1];
					a[j - 1] = temp;
				}
				else {
					break;
				}
			}
		}
	}
	//堆排序
	public static void HeapSort(int[] a) {
	
	}
	//歸併排序
	public static void MergeSort(int[] a, int s, int len) {
		int size = a.length;
        int mid = size / (len << 1);
        int c = size & ((len << 1) - 1);
        // -------歸併到只剩一個有序集合的時候結束演算法-------//
        if (mid == 0)
            return;
        // ------進行一趟歸併排序-------//
        for (int i = 0; i < mid; ++i) {
            s = i * 2 * len;
            merge(a, s, s + len, (len << 1) + s - 1);
        }
        // -------將剩下的數和倒數一個有序集合歸併-------//
        if (c != 0)
            merge(a, size - c - 2 * len, size - c, size - 1);
        // -------遞迴執行下一趟歸併排序------//
        MergeSort(a, 0, 2 * len);
	}
	private static void merge(int[] a, int s, int m, int t) {
        int[] tmp = new int[t - s + 1];
        int i = s, j = m, k = 0;
        while (i < m && j <= t) {
            if (a[i] <= a[j]) {
                tmp[k] = a[i];
                k++;
                i++;
            } else {
                tmp[k] = a[j];
                j++;
                k++;
            }
        }
        while (i < m) {
            tmp[k] = a[i];
            i++;
            k++;
        }
        while (j <= t) {
            tmp[k] = a[j];
            j++;
            k++;
        }
        System.arraycopy(tmp, 0, a, s, tmp.length);
    }
	//快速排序
	public static void QuickSort(int a[],int low,int high) {
		int l=low;
		int h=high;
		int povit=a[low];
		 
		while(l<h){
			while(l<h&&a[h]>=povit)
			h--;
			if(l<h){
				int temp=a[h];
				a[h]=a[l];
				a[l]=temp;
				l++;
			}

			while(l<h&&a[l]<=povit)
				l++;
			 
			if(l<h){
				int temp=a[h];
				a[h]=a[l];
				a[l]=temp;
				h--;
			}
		}

		if(l>low)QuickSort(a,low,l-1);
		if(h<high)QuickSort(a,l+1,high);
	}
	
}
這裡有個問題。快排入參int a[],其他入參Int[] a,兩種寫法有什麼區別?