排序算法

快速排序

public class quickSort {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int [] arr={1,3,9,4,10,5,18,6,2,100,34,32,98,48,12,0,34,89};
		sort(arr,0,arr.length-1);
		for(int i=0;i<arr.length;i++){
			System.out.print(arr[i]+" ");
		}				

	}

	private static void sort(int[] array, int lo, int hi) {
		if(lo>=hi){
            return ;
        }
        int index=partition(array,lo,hi);
        sort(array,lo,index-1);
        sort(array,index+1,hi); 	
	}
	private static int partition(int[] array, int lo, int hi) {
		int key=array[lo];
        while(lo<hi){
            while(hi>lo&&array[hi]>=key){//從後半部分向前掃描
                hi--;
            }
            array[lo]=array[hi];
            while(hi>lo&&array[lo]<=key){//從前半部分向後掃描
                lo++;
            }
            array[hi]=array[lo];
        }
        array[hi]=key;
        return hi;
	}

}

運行結果


冒泡排序

private static void bubbleSort(int[] arr) {
		// TODO Auto-generated method stub
		int temp=0;
		boolean flag=true;
		while(flag){
			flag=false;
			for(int i=0;i<arr.length-1;i++){
				for(int j=0;j<arr.length-1-i;j++){
					if(arr[j]>arr[j+1]){
						temp=arr[i];
						arr[i]=arr[j];
						arr[j]=temp;
						flag=true;
					}
				}
			}
		}
		
	}

選擇排序

public static void selectSort(int[] a) {
        if (a == null || a.length <= 0) {
            return;
        }
        for (int i = 0; i < a.length; i++) {
            int temp = a[i];
            int flag = i; // 將當前下標定義爲最小值下標
            for (int j = i + 1; j < a.length; j++) {
                if (a[j] < temp) {// a[j] < temp 從小到大排序;a[j] > temp 從大到小排序
                    temp = a[j];
                    flag = j; // 如果有小於當前最小值的關鍵字將此關鍵字的下標賦值給flag
                }
            }
            if (flag != i) {
                a[flag] = a[i];
                a[i] = temp;
            }
        }
    }

運行結果


堆排序:

    堆是一種完全二叉樹結構。在排序的過程中,元素是存儲在一維數組中的,那麼知道一個元素的位置i(起始位置爲0),若存在左右子節點,則子節點的位置分別爲2i+1,2i+2。父節點爲(i-1)/2取整。

算法過程(大頂堆爲例)

[html] view plain copy
  1. class ArrayUtils {    
  2.       
  3.     public static void printArray(int[] array) {    
  4.         System.out.print("{");    
  5.         for (int i = 0; i < array.length; i++) {    
  6.             System.out.print(array[i]);    
  7.             if (i < array.length - 1) {    
  8.                 System.out.print(", ");    
  9.             }    
  10.         }    
  11.         System.out.println("}");    
  12.     }    
  13.     public static void exchangeElements(int[] array, int index1, int index2) {    
  14.         int temp = array[index1];    
  15.         array[index1] = array[index2];    
  16.         array[index2] = temp;    
  17.     }    
  18. }    
  19. public class HeapSort {    
  20.     public static void main(String[] args) {    
  21.         int[] array = { 12, 13, 5,24, 43, 4, 21, 33, 0, -12, -44, 18, 6 };  
  22.         System.out.println("初始序列");    
  23.         ArrayUtils.printArray(array);    
  24.         //創建堆  
  25.         heapSort(array);    
  26.         System.out.println("堆排序後結果");    
  27.         //升序輸出  
  28.         ArrayUtils.printArray(array);    
  29.     }    
  30.     public static void heapSort(int[] array) {    
  31.         //如果數組中的元素爲空,或者數組中的元素個數不超過1,則返回  
  32.         if (array == null || array.length <= 1) {    
  33.             return;    
  34.         }     
  35.         //創建大頂堆  
  36.         buildMaxHeap(array);    
  37.         //創建完大頂堆之後,數組中的第一個元素應該是堆中最大的元素,將最大的元素,與堆中最後一個葉子節點進行交換  
  38.         for (int i = array.length - 1; i >= 1; i--) {    
  39.             ArrayUtils.exchangeElements(array, 0, i);  
  40.             //輸出堆排序的過程  
  41.             for(int t=0;t<array.length;t++){  
  42.                 System.out.print(array[t]+",");  
  43.             }  
  44.             System.out.println();          
  45.             maxHeap(array, i, 0);    
  46.         }    
  47.     }    
  48.     private static void buildMaxHeap(int[] array) {   
  49.         System.out.println("建堆運行了1次");  
  50.         if (array == null || array.length <= 1) {    
  51.             return;    
  52.         }    
  53.         //從第一個非葉子節點開始調整  
  54.         int half = array.length / 2;    
  55.         for (int i = half; i >= 0; i--) {    
  56.             maxHeap(array, array.length, i);    
  57.         }    
  58.     }    
  59.     private static void maxHeap(int[] array, int heapSize, int index) {    
  60.         int left = index * 2 + 1;    
  61.         int right = index * 2 + 2;    
  62.         int largest = index;    
  63.         //左節點較大  
  64.         if (left < heapSize && array[left] > array[index]) {    
  65.             largest = left;    
  66.         }    
  67.         //右節點較大  
  68.         if (right < heapSize && array[right] > array[largest]) {    
  69.             largest = right;    
  70.         }    
  71.         //不滿足大頂堆,需要交換  
  72.         if (index != largest) {    
  73.             ArrayUtils.exchangeElements(array, index, largest);    
  74.             //有可能會破壞堆的下層結構,需自上到下進行修正  
  75.             maxHeap(array, heapSize, largest);    
  76.         }    
  77.     }    
  78. }  

運行結果









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