二路歸併排序java實現

來源:互聯網
上載者:User

標籤:amp   參考   sys   targe   font   分而治之   detail   class   alt   

二路歸併排序:
其核心思想時將問題一分為二,並遞迴調用一分為二方法,使問題分割到不能再分各的原子問題,然後再歸併,從實現原子問題開始,層層向上歸併,最終解決整體問題。即所謂“分而治之,萬流歸一”


二路歸併排序的時間複雜度計算如下:

 

參考資料:演算法導論------遞迴演算法的時間複雜度求解:

二路歸併java實現:

 1 public class MergeSort { 2      3     public static void main(String[] args) { 4         int [] array = {1,8,6,7,2,4,11,17,6,48,3}; 5         //int [] array ={9,8,7,6,5,4,3,2,1}; 6         int [] temp = new int [array.length]; 7         sort(array, temp, 0, array.length - 1); 8         System.out.println("排序後:"); 9         for (int i = 0; i < array.length; i++) {10             System.out.print(array[i] + " ");11         }12     }13     /**14      * 總的sort迴圈。先將問題一分為二,再將問題解決後的兩個子問題結果合并15      * 要想清楚 子數組 邊界的處理。16      * 如子數組為 下標為(k,k + 1):則再分解為(K),(K+1)。17      * 如子數組下標為(k,k+1,k+2):則分解為(k,k+1)和(k+2)。再遞迴調用分解(k,k+1)18      * @param array19      * @param temp20      * @param start21      * @param end22      */23     public static void sort (int [] array, int [] temp, int start, int end){24         if (start < end) {25             int mid = (start + end)/2;26             //解決左邊子問題27             sort(array, temp, start, mid);28             //解決右邊子問題29             sort(array, temp, mid + 1, end);30             //合并兩個子問題31             merge(array,temp,start,end,mid);32         }33     }34     /**35      * 歸併函數36      * @param array 原始數組37      * @param temp 輔助數組38      * @param start 歸併開始座標39      * @param end 歸併結束座標40      * @param mid = (start + end)/241      */42     public static void merge(int[] array, int[] temp, int start, int end, int mid) {43         // TODO Auto-generated method stub44         //i:左子問題的操作下標; j:右子問題的操作下標45         int i = start;46         int j = mid + 1;47         int k = start;48         while (i <= mid && j <= end){49             if (array[i] < array[j]) {50                 temp[k] = array[i];51                 k++;52                 i++;53             }else {54                 temp[k] = array[j];55                 k++;56                 j++;57             }58         }    59         //左子數組還有元素60         while (i <= mid){61             temp[k] = array[i];62             k++;63             i++;64         }65         //右子數字還是元素66         while (j <= end){67             temp[k] = array[j];68             k++;69             j++;70         }71 72         //將輔助數組中排好序的元素複製到原數組73         //注意:輔助數組與原數組一一對應74         for (int index = start; index <= end; index ++){75             array[index] = temp[index];76             System.out.println("temp[" + index +"]" + "....." + temp[index]);77         }78         System.out.println("===============");79     }80 }

 

通過控制台輸出,我們可以看到排序的過程:

temp[0].....1temp[1].....8===============temp[0].....1temp[1].....6temp[2].....8===============temp[3].....2temp[4].....7===============temp[3].....2temp[4].....4temp[5].....7===============temp[0].....1temp[1].....2temp[2].....4temp[3].....6temp[4].....7temp[5].....8===============temp[6].....11temp[7].....17===============temp[6].....6temp[7].....11temp[8].....17===============temp[9].....3temp[10].....48===============temp[6].....3temp[7].....6temp[8].....11temp[9].....17temp[10].....48===============temp[0].....1temp[1].....2temp[2].....3temp[3].....4temp[4].....6temp[5].....6temp[6].....7temp[7].....8temp[8].....11temp[9].....17temp[10].....48===============排序後:1 2 3 4 6 6 7 8 11 17 48 

 

二路歸併排序java實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.