### 排序算法分析

` 1 /** 2      * 快速排序的划分方法一：算法導論版本 sit的下一個比最后一個大 3      *  4      * @param begin 5      * @param end 6      * @return 7      */ 8     private int QuickMergeA(int begin, int end) { 9         System.out.print("初始：" + arr);10         System.out.print(begin + " " + end);11         int anchor = (int) arr.get(end);12         int sit = begin - 1;13         int temp;14         for (int i = begin; i < end; i++) {15             if ((int) arr.get(i) <= anchor) {16                 sit++;17                 temp = (int) arr.get(i);18                 arr.set(i, (int) arr.get(sit));19                 arr.set(sit, temp);20             }21         }22         temp = (int) arr.get(sit + 1);23         arr.set(sit + 1, anchor);24         arr.set(end, temp);25         System.out.print(arr);26         System.out.println(sit + 1);27         return sit + 1;28     }`

`/**     * 快速排序的划分方法二：數據結構課本版本     *      * @param begin     * @param end     * @return     */    private int QuickMergeB(int begin, int end) {        System.out.print("初始：" + arr);        System.out.print(begin + " " + end);        int anchor = arr.get(end);        int send = end - 1;        int temp;        int tbegin = begin;        while (begin <= send) {            while (arr.get(begin) <= anchor && begin <= end - 1) {                begin++;            }            while (send >= tbegin && arr.get(send) > anchor) {                send--;            }            if (begin < send) {                temp = arr.get(begin);                arr.set(begin, arr.get(send));                arr.set(send, temp);            }            if (begin >= send) {                temp = arr.get(begin);                arr.set(begin, arr.get(end));                arr.set(end, temp);            }        }        System.out.print(arr);        System.out.println(send + 1);        return send + 1;    }`

`/**     * 快速排序入口     *      * @param begin     * @param end     */    private void QuickSequence(int begin, int end) {        // if (begin < end) {        // int q = QuickMergeA(begin, end);        // this.QuickSequence(begin, q - 1);        // this.QuickSequence(q + 1, end);        // }        if (begin < end) {            int q = QuickMergeB(begin, end);            this.QuickSequence(begin, q - 1);            this.QuickSequence(q + 1, end);        }    }`

`/**     * 插入排序     */    private void InsertSequence() {        for (int i = 0; i < arr.size(); i++) {            int p = 0;            while (p < i && arr.get(p) < arr.get(i)) {                p++;            }            if (p < i) {                int temp = arr.get(i);                for (int k = i; k > p; k--) {                    arr.set(k, arr.get(k - 1));                }                arr.set(p, temp);            }        }    }`

` 1 private void MaintainHeap(int length) { 2         int temp; 3         for (int i = length / 2 - 1; i >= 0; i--) { 4             if ((arr.get(i) < arr.get((i + 1) * 2 - 1)) 5                     || ((i + 1) * 2 < length && arr.get(i) < arr 6                             .get((i + 1) * 2))) { 7                 if ((i + 1) * 2 < length) { 8                     if (arr.get((i + 1) * 2) < arr.get((i + 1) * 2 - 1)) { 9                         temp = arr.get(i);10                         arr.set(i, arr.get((i + 1) * 2 - 1));11                         arr.set((i + 1) * 2 - 1, temp);12                     } else {13                         temp = arr.get(i);14                         arr.set(i, arr.get((i + 1) * 2));15                         arr.set((i + 1) * 2, temp);16                     }17 18                 } else {19                     temp = arr.get(i);20                     arr.set(i, arr.get((i + 1) * 2 - 1));21                     arr.set((i + 1) * 2 - 1, temp);22                 }23 24             }25         }26 27         System.out.println(arr);28     }29 30     private void Heapsequence(int length) {31         for (int i = length; i > 0; i--) {32             this.MaintainHeap(i);33             int temp = arr.get(0);34             arr.set(0,arr.get(i - 1));35             arr.set(i - 1, temp);36         }37     }`