四、稀尔排序
java代码实现:
- 01.
- 06.public class ShellSort {       
- 07.      
- 08.    
- 15.    public void sort(Integer[] array, int from, int end) {       
- 16.        
- 17.        int step = initialStep(end - from + 1);       
- 18.      
- 19.        
- 20.        for (; step >= 1; step = (step + 1) / 2 - 1) {       
- 21.            
- 22.            for (int groupIndex = 0; groupIndex < step; groupIndex++) {       
- 23.      
- 24.                
- 25.                insertSort(array, groupIndex, step, end);       
- 26.            }       
- 27.        }       
- 28.    }       
- 29.      
- 30.    
- 38.    public void insertSort(Integer[] array, int groupIndex, int step, int end) {       
- 39.        int startIndex = groupIndex;
- 40.        int endIndex = startIndex;
- 41.        
- 45.        while ((endIndex + step) <= end) {       
- 46.            endIndex += step;       
- 47.        }       
- 48.      
- 49.        
- 50.        for (int i = groupIndex + step; i <= end; i += step) {       
- 51.            for (int j = groupIndex; j < i; j += step) {       
- 52.                Integer insertedElem = array[i];       
- 53.                
- 54.                if ((array[j].compareTo(insertedElem)) >= 0) {       
- 55.                    
- 56.                    move(array, j, i - step, step);       
- 57.                    array[j] = insertedElem;       
- 58.                    break;       
- 59.                }       
- 60.            }       
- 61.        }       
- 62.    }       
- 63.      
- 64.    
- 82.    public static int initialStep(int len) {       
- 83.        
- 88.        int step = 1;       
- 89.      
- 90.        
- 91.        while ((step + 1) * 2 - 1 < len - 1) {       
- 92.            step = (step + 1) * 2 - 1;       
- 93.        }       
- 94.      
- 95.        System.out.println("初始步长 : " + step);       
- 96.        return step;       
- 97.    }       
- 98.        
- 99.    
- 106.    protected final void move(Integer[] array, int startIndex, int endIndex, int step)  {       
- 107.        for (int i = endIndex; i >= startIndex; i -= step) {       
- 108.            array[i + step] = array[i];       
- 109.        }       
- 110.    }       
- 111.    
- 112.        
- 113.    
- 117.    public static void main(String[] args) {       
- 118.        Integer[] intgArr = { 5, 9, 1, 4, 8, 2, 6, 3, 7, 0 };       
- 119.        ShellSort shellSort = new ShellSort();       
- 120.        shellSort.sort(intgArr,0,intgArr.length-1);    
- 121.        for(Integer intObj:intgArr){    
- 122.            System.out.print(intObj + " ");    
- 123.        }    
- 124.    }       
- 125.}       
 
- 01.
- 06.public class ShellSort {       
- 07.      
- 08.    
- 15.    public void sort(Integer[] array, int from, int end) {       
- 16.        
- 17.        int step = initialStep(end - from + 1);       
- 18.      
- 19.        
- 20.        for (; step >= 1; step = (step + 1) / 2 - 1) {       
- 21.            
- 22.            for (int groupIndex = 0; groupIndex < step; groupIndex++) {       
- 23.      
- 24.                
- 25.                insertSort(array, groupIndex, step, end);       
- 26.            }       
- 27.        }       
- 28.    }       
- 29.      
- 30.    
- 38.    public void insertSort(Integer[] array, int groupIndex, int step, int end) {       
- 39.        int startIndex = groupIndex;
- 40.        int endIndex = startIndex;
- 41.        
- 45.        while ((endIndex + step) <= end) {       
- 46.            endIndex += step;       
- 47.        }       
- 48.      
- 49.        
- 50.        for (int i = groupIndex + step; i <= end; i += step) {       
- 51.            for (int j = groupIndex; j < i; j += step) {       
- 52.                Integer insertedElem = array[i];       
- 53.                
- 54.                if ((array[j].compareTo(insertedElem)) >= 0) {       
- 55.                    
- 56.                    move(array, j, i - step, step);       
- 57.                    array[j] = insertedElem;       
- 58.                    break;       
- 59.                }       
- 60.            }       
- 61.        }       
- 62.    }       
- 63.      
- 64.    
- 82.    public static int initialStep(int len) {       
- 83.        
- 88.        int step = 1;       
- 89.      
- 90.        
- 91.        while ((step + 1) * 2 - 1 < len - 1) {       
- 92.            step = (step + 1) * 2 - 1;       
- 93.        }       
- 94.      
- 95.        System.out.println("初始步长 : " + step);       
- 96.        return step;       
- 97.    }       
- 98.        
- 99.    
- 106.    protected final void move(Integer[] array, int startIndex, int endIndex, int step)  {       
- 107.        for (int i = endIndex; i >= startIndex; i -= step) {       
- 108.            array[i + step] = array[i];       
- 109.        }       
- 110.    }       
- 111.    
- 112.        
- 113.    
- 117.    public static void main(String[] args) {       
- 118.        Integer[] intgArr = { 5, 9, 1, 4, 8, 2, 6, 3, 7, 0 };       
- 119.        ShellSort shellSort = new ShellSort();       
- 120.        shellSort.sort(intgArr,0,intgArr.length-1);    
- 121.        for(Integer intObj:intgArr){    
- 122.            System.out.print(intObj + " ");    
- 123.        }    
- 124.    }       
- 125.}       
 
 
五、快速排序(Quick Sort)
1. 基本思想:
  在当前无序区R[1..H]中任取一个数据元素作为比较的"基准"(不妨记为X),用此基准将当前无序区划分为左右两个较小的无序区:R[1..I-1]和R[I+1..H],且左边的无序子区中数据元素均小于等于基准元素,右边的无序子区中数据元素均大于等于基准元素,而基准X则位于最终排序的位置上,即R[1..I-1]≤X.Key≤R[I+1..H](1≤I≤H),当R[1..I-1]和R[I+1..H]均非空时,分别对它们进行上述的划分过程,直至所有无序子区中的数据元素均已排序为止。
2. 排序过程:
【示例】:
初始关键字 [49 38 65 97 76 13 27 49]
一趟排序之后 [27 38 13] 49 [76 97 65 49] 
二趟排序之后 [13] 27 [38] 49 [49 65]76 [97]
三趟排序之后 13 27 38 49 49 [65]76 97
最后的排序结果 13 27 38 49 49 65 76 97 
各趟排序之后的状态
java代码实现:
- 01.
- 04.    
- 05.public class QuickSort {       
- 06.      
- 07.    
- 14.    
- 15.    public void sort(Integer[] array, int from, int end) {       
- 16.        quickSort(array, from, end);       
- 17.    }       
- 18.      
- 19.    
- 26.    private void quickSort(Integer[] array, int low, int high) {       
- 27.        
- 32.        if (low < high) {       
- 33.            
- 34.                
- 35.            
- 36.            int pivot = partition2(array, low, high);       
- 37.            
- 38.                
- 39.            
- 44.            quickSort(array, low, pivot - 1);       
- 45.            quickSort(array, pivot + 1, high);       
- 46.        }       
- 47.      
- 48.    }       
- 49.      
- 50.    
- 59.    private int partition1(Integer[] array, int low, int high) {       
- 60.        Integer pivotElem = array[low];
- 61.        
- 62.        int border = low;       
- 63.      
- 64.        
- 69.        for (int i = low + 1; i <= high; i++) {       
- 70.            
- 71.            if ((array[i].compareTo(pivotElem)) < 0) {       
- 72.                swap(array, ++border, i);
- 73.            }       
- 74.        }       
- 75.        
- 83.        swap(array, border, low);       
- 84.        return border;       
- 85.    }       
- 86.      
- 87.    
- 96.    private int partition2(Integer[] array, int low, int high) {       
- 97.        int pivot = low;
- 98.        
- 99.        while (true) {       
- 100.            if (pivot != high) {
- 101.                
- 102.                if ((array[high].compareTo(array[pivot])) < 0) {       
- 103.                    swap(array, high, pivot);       
- 104.                    
- 105.                    pivot = high;       
- 106.                } else {
- 107.                    high--;       
- 108.                }       
- 109.            } else {
- 110.                
- 111.                if ((array[low].compareTo(array[pivot])) > 0) {       
- 112.                    swap(array, low, pivot);       
- 113.                    
- 114.                    pivot = low;       
- 115.                } else {
- 116.                    low++;       
- 117.                }       
- 118.            }       
- 119.            if (low == high) {       
- 120.                break;       
- 121.            }       
- 122.        }       
- 123.        
- 124.        return pivot;       
- 125.    }       
- 126.      
- 127.    
- 136.    private int partition3(Integer[] array, int low, int high) {       
- 137.        int pivot = low;
- 138.        low++;       
- 139.        
- 140.        
- 141.      
- 142.        while (true) {       
- 143.            
- 144.            while (low < high) {       
- 145.                
- 146.                if ((array[low].compareTo(array[pivot])) >= 0) {       
- 147.                    break;       
- 148.                } else {
- 149.                    low++;       
- 150.                }       
- 151.            }       
- 152.      
- 153.            while (high > low) {       
- 154.                
- 155.                if ((array[high].compareTo(array[pivot])) < 0) {       
- 156.                    break;       
- 157.                } else {
- 158.                    high--;       
- 159.                }       
- 160.            }       
- 161.            
- 162.            if (low == high) {       
- 163.                break;       
- 164.            }       
- 165.      
- 166.            swap(array, low, high);       
- 167.        }       
- 168.      
- 169.        
- 170.        if ((array[pivot].compareTo(array[low])) > 0) {       
- 171.            
- 172.            swap(array, low, pivot);       
- 173.            pivot = low;       
- 174.        } else if ((array[pivot].compareTo(array[low])) <= 0) {       
- 175.            swap(array, low - 1, pivot);       
- 176.            pivot = low - 1;       
- 177.        }       
- 178.      
- 179.        
- 180.        return pivot;       
- 181.    }       
- 182.        
- 183.        
- 189.    public void swap(Integer[] array, int i, int j) {       
- 190.        if (i != j) {
- 191.            Integer tmp = array[i];       
- 192.            array[i] = array[j];       
- 193.            array[j] = tmp;       
- 194.        }       
- 195.    }     
- 196.        
- 197.    
- 201.    public static void main(String[] args) {       
- 202.        Integer[] intgArr = { 5, 9, 1, 4, 2, 6, 3, 8, 0, 7 };      
- 203.        QuickSort quicksort = new QuickSort();       
- 204.        quicksort.sort(intgArr,0,intgArr.length-1);    
- 205.        for(Integer intObj:intgArr){    
- 206.            System.out.print(intObj + " ");    
- 207.        }    
- 208.    }       
- 209.}       
 
- 01.
- 04.    
- 05.public class QuickSort {       
- 06.      
- 07.    
- 14.    
- 15.    public void sort(Integer[] array, int from, int end) {       
- 16.        quickSort(array, from, end);       
- 17.    }       
- 18.      
- 19.    
- 26.    private void quickSort(Integer[] array, int low, int high) {       
- 27.        
- 32.        if (low < high) {       
- 33.            
- 34.                
- 35.            
- 36.            int pivot = partition2(array, low, high);       
- 37.            
- 38.                
- 39.            
- 44.            quickSort(array, low, pivot - 1);       
- 45.            quickSort(array, pivot + 1, high);       
- 46.        }       
- 47.      
- 48.    }       
- 49.      
- 50.    
- 59.    private int partition1(Integer[] array, int low, int high) {       
- 60.        Integer pivotElem = array[low];
- 61.        
- 62.        int border = low;       
- 63.      
- 64.        
- 69.        for (int i = low + 1; i <= high; i++) {       
- 70.            
- 71.            if ((array[i].compareTo(pivotElem)) < 0) {       
- 72.                swap(array, ++border, i);
- 73.            }       
- 74.        }       
- 75.        
- 83.        swap(array, border, low);       
- 84.        return border;       
- 85.    }       
- 86.      
- 87.    
- 96.    private int partition2(Integer[] array, int low, int high) {       
- 97.        int pivot = low;
- 98.        
- 99.        while (true) {       
- 100.            if (pivot != high) {
- 101.                
- 102.                if ((array[high].compareTo(array[pivot])) < 0) {       
- 103.                    swap(array, high, pivot);       
- 104.                    
- 105.                    pivot = high;       
- 106.                } else {
- 107.                    high--;       
- 108.                }       
- 109.            } else {
- 110.                
- 111.                if ((array[low].compareTo(array[pivot])) > 0) {       
- 112.                    swap(array, low, pivot);       
- 113.                    
- 114.                    pivot = low;       
- 115.                } else {
- 116.                    low++;       
- 117.                }       
- 118.            }       
- 119.            if (low == high) {       
- 120.                break;       
- 121.            }       
- 122.        }       
- 123.        
- 124.        return pivot;       
- 125.    }       
- 126.      
- 127.    
- 136.    private int partition3(Integer[] array, int low, int high) {       
- 137.        int pivot = low;
- 138.        low++;       
- 139.        
- 140.        
- 141.      
- 142.        while (true) {       
- 143.            
- 144.            while (low < high) {       
- 145.                
- 146.                if ((array[low].compareTo(array[pivot])) >= 0) {       
- 147.                    break;       
- 148.                } else {
- 149.                    low++;       
- 150.                }       
- 151.            }       
- 152.      
- 153.            while (high > low) {       
- 154.                
- 155.                if ((array[high].compareTo(array[pivot])) < 0) {       
- 156.                    break;       
- 157.                } else {
- 158.                    high--;       
- 159.                }       
- 160.            }       
- 161.            
- 162.            if (low == high) {       
- 163.                break;       
- 164.            }       
- 165.      
- 166.            swap(array, low, high);       
- 167.        }       
- 168.      
- 169.        
- 170.        if ((array[pivot].compareTo(array[low])) > 0) {       
- 171.            
- 172.            swap(array, low, pivot);       
- 173.            pivot = low;       
- 174.        } else if ((array[pivot].compareTo(array[low])) <= 0) {       
- 175.            swap(array, low - 1, pivot);       
- 176.            pivot = low - 1;       
- 177.        }       
- 178.      
- 179.        
- 180.        return pivot;       
- 181.    }       
- 182.        
- 183.        
- 189.    public void swap(Integer[] array, int i, int j) {       
- 190.        if (i != j) {
- 191.            Integer tmp = array[i];       
- 192.            array[i] = array[j];       
- 193.            array[j] = tmp;       
- 194.        }       
- 195.    }     
- 196.        
- 197.    
- 201.    public static void main(String[] args) {       
- 202.        Integer[] intgArr = { 5, 9, 1, 4, 2, 6, 3, 8, 0, 7 };      
- 203.        QuickSort quicksort = new QuickSort();       
- 204.        quicksort.sort(intgArr,0,intgArr.length-1);    
- 205.        for(Integer intObj:intgArr){    
- 206.            System.out.print(intObj + " ");    
- 207.        }    
- 208.    }       
- 209.}       
 
 
六、归并排序
java代码实现:
- **   
- 02.归并排序:里面是一个递归程序,深刻理解之。   
- 03.*/    
- 04.public class MergeSort{       
- 05.      
- 06.    
- 13.    public void partition(Integer[] arr, int from, int end) {       
- 14.        
- 15.        if (from < end) {       
- 16.            
- 17.            int mid = (from + end) / 2;       
- 18.            partition(arr, from, mid);       
- 19.            partition(arr, mid + 1, end);       
- 20.            
- 21.            merge(arr, from, end, mid);       
- 22.        }       
- 23.    }       
- 24.      
- 25.    
- 34.    public void merge(Integer[] arr, int from, int end, int mid) {       
- 35.        Integer[] tmpArr = new Integer[10];    
- 36.        int tmpArrIndex = 0;
- 37.        int part1ArrIndex = from;
- 38.        int part2ArrIndex = mid + 1;
- 39.      
- 40.        
- 41.        
- 42.        while ((part1ArrIndex <= mid) && (part2ArrIndex <= end)) {       
- 43.            
- 44.            if (arr[part1ArrIndex] - arr[part2ArrIndex] < 0) {       
- 45.                
- 46.                
- 47.                
- 48.                tmpArr[tmpArrIndex++] = arr[part1ArrIndex++];       
- 49.            } else {       
- 50.                
- 51.                tmpArr[tmpArrIndex++] = arr[part2ArrIndex++];       
- 52.            }       
- 53.        }       
- 54.        
- 55.        
- 56.        
- 57.        while (part1ArrIndex <= mid) {       
- 58.            tmpArr[tmpArrIndex++] = arr[part1ArrIndex++];       
- 59.        }       
- 60.        while (part2ArrIndex <= end) {       
- 61.            tmpArr[tmpArrIndex++] = arr[part2ArrIndex++];       
- 62.        }       
- 63.      
- 64.        
- 65.        System.arraycopy(tmpArr, 0, arr, from, end - from + 1);       
- 66.    }       
- 67.      
- 68.    
- 72.    public static void main(String[] args) {       
- 73.        Integer[] intgArr = {5,9,1,4,2,6,3,8,0,7};       
- 74.        MergeSort insertSort = new MergeSort();       
- 75.        insertSort.partition(intgArr,0,intgArr.length-1);    
- 76.        for(Integer a:intgArr){    
- 77.            System.out.print(a + " ");    
- 78.        }    
- 79.    }       
- 80.}       
 
- **   
- 02.归并排序:里面是一个递归程序,深刻理解之。   
- 03.*/    
- 04.public class MergeSort{       
- 05.      
- 06.    
- 13.    public void partition(Integer[] arr, int from, int end) {       
- 14.        
- 15.        if (from < end) {       
- 16.            
- 17.            int mid = (from + end) / 2;       
- 18.            partition(arr, from, mid);       
- 19.            partition(arr, mid + 1, end);       
- 20.            
- 21.            merge(arr, from, end, mid);       
- 22.        }       
- 23.    }       
- 24.      
- 25.    
- 34.    public void merge(Integer[] arr, int from, int end, int mid) {       
- 35.        Integer[] tmpArr = new Integer[10];    
- 36.        int tmpArrIndex = 0;
- 37.        int part1ArrIndex = from;
- 38.        int part2ArrIndex = mid + 1;
- 39.      
- 40.        
- 41.        
- 42.        while ((part1ArrIndex <= mid) && (part2ArrIndex <= end)) {       
- 43.            
- 44.            if (arr[part1ArrIndex] - arr[part2ArrIndex] < 0) {       
- 45.                
- 46.                
- 47.                
- 48.                tmpArr[tmpArrIndex++] = arr[part1ArrIndex++];       
- 49.            } else {       
- 50.                
- 51.                tmpArr[tmpArrIndex++] = arr[part2ArrIndex++];       
- 52.            }       
- 53.        }       
- 54.        
- 55.        
- 56.        
- 57.        while (part1ArrIndex <= mid) {       
- 58.            tmpArr[tmpArrIndex++] = arr[part1ArrIndex++];       
- 59.        }       
- 60.        while (part2ArrIndex <= end) {       
- 61.            tmpArr[tmpArrIndex++] = arr[part2ArrIndex++];       
- 62.        }       
- 63.      
- 64.        
- 65.        System.arraycopy(tmpArr, 0, arr, from, end - from + 1);       
- 66.    }       
- 67.      
- 68.    
- 72.    public static void main(String[] args) {       
- 73.        Integer[] intgArr = {5,9,1,4,2,6,3,8,0,7};       
- 74.        MergeSort insertSort = new MergeSort();       
- 75.        insertSort.partition(intgArr,0,intgArr.length-1);    
- 76.        for(Integer a:intgArr){    
- 77.            System.out.print(a + " ");    
- 78.        }    
- 79.    }       
- 80.}       
 
七、堆排序(Heap Sort)
1. 基本思想:
  堆排序是一树形选择排序,在排序过程中,将R[1..N]看成是一颗完全二叉树的顺序存储结构,利用完全二叉树中双亲结点和孩子结点之间的内在关系来选择最小的元素。
2. 堆的定义: N个元素的序列K1,K2,K3,...,Kn.称为堆,当且仅当该序列满足特性:
       Ki≤K2i Ki ≤K2i+1(1≤ I≤ [N/2])
  堆实质上是满足如下性质的完全二叉树:树中任一非叶子结点的关键字均大于等于其孩子结点的关键字。例如序列10,15,56,25,30,70就是一个堆,它对应的完全二叉树如上图所示。这种堆中根结点(称为堆顶)的关键字最小,我们把它称为小根堆。反之,若完全二叉树中任一非叶子结点的关键字均大于等于其孩子的关键字,则称之为大根堆。
3. 排序过程:
堆排序正是利用小根堆(或大根堆)来选取当前无序区中关键字小(或最大)的记录实现排序的。我们不妨利用大根堆来排序。每一趟排序的基本操作是:将当前无序区调整为一个大根堆,选取关键字最大的堆顶记录,将它和无序区中的最后一个记录交换。这样,正好和直接选择排序相反,有序区是在原记录区的尾部形成并逐步向前扩大到整个记录区。
【示例】:对关键字序列42,13,91,23,24,16,05,88建堆 
java代码实现:
- 01.
- 04.public class HeapSort {       
- 05.      
- 06.    
- 13.    public void sort(Integer[] array, int from, int end) {       
- 14.        
- 15.        initialHeap(array, from, end);       
- 16.      
- 17.        
- 21.        for (int i = end - from + 1; i >= 2; i--) {       
- 22.            
- 23.            swap(array, from, i - 1);       
- 24.            
- 25.            adjustNote(array, 1, i - 1);       
- 26.        }       
- 27.      
- 28.    }       
- 29.      
- 30.    
- 39.    private void initialHeap(Integer[] arr, int from, int end) {       
- 40.        int lastBranchIndex = (end - from + 1) / 2;
- 41.        
- 42.        for (int i = lastBranchIndex; i >= 1; i--) {       
- 43.            adjustNote(arr, i, end - from + 1);       
- 44.        }       
- 45.    }       
- 46.      
- 47.    
- 54.    private void adjustNote(Integer[] arr, int parentNodeIndex, int len) {       
- 55.        int minNodeIndex = parentNodeIndex;       
- 56.        
- 57.        if (parentNodeIndex * 2 <= len) {       
- 58.            
- 59.            if ((arr[parentNodeIndex - 1].compareTo(arr[parentNodeIndex * 2 - 1])) < 0) {       
- 60.                minNodeIndex = parentNodeIndex * 2;
- 61.            }       
- 62.      
- 63.            
- 64.            if (parentNodeIndex * 2 + 1 <= len) {       
- 65.                
- 66.                if ((arr[minNodeIndex - 1].compareTo(arr[(parentNodeIndex * 2 + 1) - 1])) < 0) {       
- 67.                    minNodeIndex = parentNodeIndex * 2 + 1;
- 68.                }       
- 69.            }       
- 70.        }       
- 71.      
- 72.        
- 73.        if (minNodeIndex != parentNodeIndex) {       
- 74.            swap(arr, parentNodeIndex - 1, minNodeIndex - 1);       
- 75.            
- 76.            if (minNodeIndex * 2 <= len) {
- 77.                adjustNote(arr, minNodeIndex, len);       
- 78.            }       
- 79.        }       
- 80.    }       
- 81.        
- 82.            
- 88.    public void swap(Integer[] array, int i, int j) {       
- 89.        if (i != j) {
- 90.            Integer tmp = array[i];       
- 91.            array[i] = array[j];       
- 92.            array[j] = tmp;       
- 93.        }       
- 94.    }     
- 95.        
- 96.    
- 100.    public static void main(String[] args) {       
- 101.        Integer[] intgArr = { 5, 9, 1, 4, 2, 6, 3, 8, 0, 7 };      
- 102.        HeapSort heapsort = new HeapSort();       
- 103.        heapsort.sort(intgArr,0,intgArr.length-1);    
- 104.        for(Integer intObj:intgArr){    
- 105.            System.out.print(intObj + " ");    
- 106.        }    
- 107.    }       
- 108.      
- 109.}      
 
- 01.
- 04.public class HeapSort {       
- 05.      
- 06.    
- 13.    public void sort(Integer[] array, int from, int end) {       
- 14.        
- 15.        initialHeap(array, from, end);       
- 16.      
- 17.        
- 21.        for (int i = end - from + 1; i >= 2; i--) {       
- 22.            
- 23.            swap(array, from, i - 1);       
- 24.            
- 25.            adjustNote(array, 1, i - 1);       
- 26.        }       
- 27.      
- 28.    }       
- 29.      
- 30.    
- 39.    private void initialHeap(Integer[] arr, int from, int end) {       
- 40.        int lastBranchIndex = (end - from + 1) / 2;
- 41.        
- 42.        for (int i = lastBranchIndex; i >= 1; i--) {       
- 43.            adjustNote(arr, i, end - from + 1);       
- 44.        }       
- 45.    }       
- 46.      
- 47.    
- 54.    private void adjustNote(Integer[] arr, int parentNodeIndex, int len) {       
- 55.        int minNodeIndex = parentNodeIndex;       
- 56.        
- 57.        if (parentNodeIndex * 2 <= len) {       
- 58.            
- 59.            if ((arr[parentNodeIndex - 1].compareTo(arr[parentNodeIndex * 2 - 1])) < 0) {       
- 60.                minNodeIndex = parentNodeIndex * 2;
- 61.            }       
- 62.      
- 63.            
- 64.            if (parentNodeIndex * 2 + 1 <= len) {       
- 65.                
- 66.                if ((arr[minNodeIndex - 1].compareTo(arr[(parentNodeIndex * 2 + 1) - 1])) < 0) {       
- 67.                    minNodeIndex = parentNodeIndex * 2 + 1;
- 68.                }       
- 69.            }       
- 70.        }       
- 71.      
- 72.        
- 73.        if (minNodeIndex != parentNodeIndex) {       
- 74.            swap(arr, parentNodeIndex - 1, minNodeIndex - 1);       
- 75.            
- 76.            if (minNodeIndex * 2 <= len) {
- 77.                adjustNote(arr, minNodeIndex, len);       
- 78.            }       
- 79.        }       
- 80.    }       
- 81.        
- 82.            
- 88.    public void swap(Integer[] array, int i, int j) {       
- 89.        if (i != j) {
- 90.            Integer tmp = array[i];       
- 91.            array[i] = array[j];       
- 92.            array[j] = tmp;       
- 93.        }       
- 94.    }     
- 95.        
- 96.    
- 100.    public static void main(String[] args) {       
- 101.        Integer[] intgArr = { 5, 9, 1, 4, 2, 6, 3, 8, 0, 7 };      
- 102.        HeapSort heapsort = new HeapSort();       
- 103.        heapsort.sort(intgArr,0,intgArr.length-1);    
- 104.        for(Integer intObj:intgArr){    
- 105.            System.out.print(intObj + " ");    
- 106.        }    
- 107.    }       
- 108.      
- 109.}      
 
 
八、桶式排序
java代码实现:
- 01.
- 11.public class BucketSort {       
- 12.     public void sort(int[] keys,int from,int len,int max)       
- 13.        {       
- 14.            int[] temp=new int[len];       
- 15.            int[] count=new int[max];       
- 16.                   
- 17.                   
- 18.            for(int i=0;i<len;i++)       
- 19.            {       
- 20.                count[keys[from+i]]++;       
- 21.            }       
- 22.            
- 23.            for(int i=1;i<max;i++)       
- 24.            {       
- 25.                count[i]=count[i]+count[i-1];
- 26.            }       
- 27.                   
- 28.            System.arraycopy(keys, from, temp, 0, len);       
- 29.            for(int k=len-1;k>=0;k--)
- 30.            {       
- 31.                keys[--count[temp[k]]]=temp[k];
- 32.            }       
- 33.        }       
- 34.        
- 37.        public static void main(String[] args) {       
- 38.      
- 39.            int[] a={1,4,8,3,2,9,5,0,7,6,9,10,9,13,14,15,11,12,17,16};       
- 40.            BucketSort bucketSort=new BucketSort();       
- 41.            bucketSort.sort(a,0,a.length,20);
- 42.                   
- 43.                   
- 44.            for(int i=0;i<a.length;i++)       
- 45.            {       
- 46.                System.out.print(a[i]+",");       
- 47.            }       
- 48.      
- 49.        }       
- 50.      
- 51.      
- 52.}       
 
- 01.
- 11.public class BucketSort {       
- 12.     public void sort(int[] keys,int from,int len,int max)       
- 13.        {       
- 14.            int[] temp=new int[len];       
- 15.            int[] count=new int[max];       
- 16.                   
- 17.                   
- 18.            for(int i=0;i<len;i++)       
- 19.            {       
- 20.                count[keys[from+i]]++;       
- 21.            }       
- 22.            
- 23.            for(int i=1;i<max;i++)       
- 24.            {       
- 25.                count[i]=count[i]+count[i-1];
- 26.            }       
- 27.                   
- 28.            System.arraycopy(keys, from, temp, 0, len);       
- 29.            for(int k=len-1;k>=0;k--)
- 30.            {       
- 31.                keys[--count[temp[k]]]=temp[k];
- 32.            }       
- 33.        }       
- 34.        
- 37.        public static void main(String[] args) {       
- 38.      
- 39.            int[] a={1,4,8,3,2,9,5,0,7,6,9,10,9,13,14,15,11,12,17,16};       
- 40.            BucketSort bucketSort=new BucketSort();       
- 41.            bucketSort.sort(a,0,a.length,20);
- 42.                   
- 43.                   
- 44.            for(int i=0;i<a.length;i++)       
- 45.            {       
- 46.                System.out.print(a[i]+",");       
- 47.            }       
- 48.      
- 49.        }       
- 50.      
- 51.      
- 52.}       
 
九、基数排序
java代码实现:
- 01.
- 04.import java.util.Arrays;     
- 05.public class RadixSort {       
- 06.      
- 07.    
- 13.    public int digit(long x, long d) {       
- 14.      
- 15.        long pow = 1;       
- 16.        while (--d > 0) {       
- 17.            pow *= 10;       
- 18.        }       
- 19.        return (int) (x / pow % 10);       
- 20.    }       
- 21.      
- 22.    
- 30.    public long[] radixSortAsc(long[] arr) {       
- 31.        
- 32.        for (int d = 1; d <= getMax(arr); d++) {       
- 33.            
- 34.            long[] tmpArray = new long[arr.length];       
- 35.            
- 36.            int[] count = new int[10];       
- 37.            
- 38.            for (int i = 0; i < arr.length; i++) {       
- 39.                count[digit(arr[i], d)] += 1;       
- 40.            }       
- 41.            
- 48.            for (int i = 1; i < 10; i++) {       
- 49.                count[i] += count[i - 1];       
- 50.            }       
- 51.      
- 52.            
- 62.            for (int i = arr.length - 1; i >= 0; i--) {
- 63.                
- 64.                tmpArray[count[digit(arr[i], d)] - 1] = arr[i];       
- 65.                count[digit(arr[i], d)]--;       
- 66.            }       
- 67.      
- 68.            System.arraycopy(tmpArray, 0, arr, 0, tmpArray.length);       
- 69.        }       
- 70.        return arr;       
- 71.    }       
- 72.      
- 73.    
- 80.    public long[] radixSortDesc(long[] arr) {       
- 81.        for (int d = 1; d <= getMax(arr); d++) {       
- 82.            long[] tmpArray = new long[arr.length];       
- 83.            
- 84.            int[] count = new int[10];       
- 85.            
- 86.            
- 87.            for (int i = 0; i < arr.length; i++) {       
- 88.                count[9 - digit(arr[i], d)] += 1;       
- 89.            }       
- 90.      
- 91.            for (int i = 1; i < 10; i++) {       
- 92.                count[i] += count[i - 1];       
- 93.            }       
- 94.      
- 95.            for (int i = arr.length - 1; i >= 0; i--) {       
- 96.                tmpArray[count[9 - digit(arr[i], d)] - 1] = arr[i];       
- 97.                count[9 - digit(arr[i], d)]--;       
- 98.            }       
- 99.      
- 100.            System.arraycopy(tmpArray, 0, arr, 0, tmpArray.length);       
- 101.        }       
- 102.        return arr;       
- 103.    }       
- 104.      
- 105.    private int getMax(long[] array) {       
- 106.        int maxlIndex = 0;       
- 107.        for (int j = 1; j < array.length; j++) {       
- 108.            if (array[j] > array[maxlIndex]) {       
- 109.                maxlIndex = j;       
- 110.            }       
- 111.        }       
- 112.        return String.valueOf(array[maxlIndex]).length();       
- 113.    }       
- 114.      
- 115.    public static void main(String[] args) {       
- 116.        long[] ary = new long[] { 123, 321, 132, 212, 213, 312, 21, 223 };       
- 117.        RadixSort rs = new RadixSort();       
- 118.        System.out.println("升 - " + Arrays.toString(rs.radixSortAsc(ary)));       
- 119.      
- 120.        ary = new long[] { 123, 321, 132, 212, 213, 312, 21, 223 };       
- 121.        System.out.println("降 - " + Arrays.toString(rs.radixSortDesc(ary)));       
- 122.    }       
- 123.}      
 
- 01.
- 04.import java.util.Arrays;     
- 05.public class RadixSort {       
- 06.      
- 07.    
- 13.    public int digit(long x, long d) {       
- 14.      
- 15.        long pow = 1;       
- 16.        while (--d > 0) {       
- 17.            pow *= 10;       
- 18.        }       
- 19.        return (int) (x / pow % 10);       
- 20.    }       
- 21.      
- 22.    
- 30.    public long[] radixSortAsc(long[] arr) {       
- 31.        
- 32.        for (int d = 1; d <= getMax(arr); d++) {       
- 33.            
- 34.            long[] tmpArray = new long[arr.length];       
- 35.            
- 36.            int[] count = new int[10];       
- 37.            
- 38.            for (int i = 0; i < arr.length; i++) {       
- 39.                count[digit(arr[i], d)] += 1;       
- 40.            }       
- 41.            
- 48.            for (int i = 1; i < 10; i++) {       
- 49.                count[i] += count[i - 1];       
- 50.            }       
- 51.      
- 52.            
- 62.            for (int i = arr.length - 1; i >= 0; i--) {
- 63.                
- 64.                tmpArray[count[digit(arr[i], d)] - 1] = arr[i];       
- 65.                count[digit(arr[i], d)]--;       
- 66.            }       
- 67.      
- 68.            System.arraycopy(tmpArray, 0, arr, 0, tmpArray.length);       
- 69.        }       
- 70.        return arr;       
- 71.    }       
- 72.      
- 73.    
- 80.    public long[] radixSortDesc(long[] arr) {       
- 81.        for (int d = 1; d <= getMax(arr); d++) {       
- 82.            long[] tmpArray = new long[arr.length];       
- 83.            
- 84.            int[] count = new int[10];       
- 85.            
- 86.            
- 87.            for (int i = 0; i < arr.length; i++) {       
- 88.                count[9 - digit(arr[i], d)] += 1;       
- 89.            }       
- 90.      
- 91.            for (int i = 1; i < 10; i++) {       
- 92.                count[i] += count[i - 1];       
- 93.            }       
- 94.      
- 95.            for (int i = arr.length - 1; i >= 0; i--) {       
- 96.                tmpArray[count[9 - digit(arr[i], d)] - 1] = arr[i];       
- 97.                count[9 - digit(arr[i], d)]--;       
- 98.            }       
- 99.      
- 100.            System.arraycopy(tmpArray, 0, arr, 0, tmpArray.length);       
- 101.        }       
- 102.        return arr;       
- 103.    }       
- 104.      
- 105.    private int getMax(long[] array) {       
- 106.        int maxlIndex = 0;       
- 107.        for (int j = 1; j < array.length; j++) {       
- 108.            if (array[j] > array[maxlIndex]) {       
- 109.                maxlIndex = j;       
- 110.            }       
- 111.        }       
- 112.        return String.valueOf(array[maxlIndex]).length();       
- 113.    }       
- 114.      
- 115.    public static void main(String[] args) {       
- 116.        long[] ary = new long[] { 123, 321, 132, 212, 213, 312, 21, 223 };       
- 117.        RadixSort rs = new RadixSort();       
- 118.        System.out.println("升 - " + Arrays.toString(rs.radixSortAsc(ary)));       
- 119.      
- 120.        ary = new long[] { 123, 321, 132, 212, 213, 312, 21, 223 };       
- 121.        System.out.println("降 - " + Arrays.toString(rs.radixSortDesc(ary)));       
- 122.    }       
- 123.}      
 
 
十、几种排序算法的比较和选择 
1. 选取排序方法需要考虑的因素:
(1) 待排序的元素数目n;
(2) 元素本身信息量的大小;
(3) 关键字的结构及其分布情况;
(4) 语言工具的条件,辅助空间的大小等。
2. 小结:
(1) 若n较小(n <= 50),则可以采用直接插入排序或直接选择排序。由于直接插入排序所需的记录移动操作较直接选择排序多,因而当记录本身信息量较大时,用直接选择排序较好。
(2) 若文件的初始状态已按关键字基本有序,则选用直接插入或冒泡排序为宜。
(3) 若n较大,则应采用时间复杂度为O(nlog2n)的排序方法:快速排序、堆排序或归并排序。 快速排序是目前基于比较的内部排序法中被认为是最好的方法。
(4) 在基于比较排序方法中,每次比较两个关键字的大小之后,仅仅出现两种可能的转移,因此可以用一棵二叉树来描述比较判定过程,由此可以证明:当文件的n个关键字随机分布时,任何借助于"比较"的排序算法,至少需要O(nlog2n)的时间。
(5) 当记录本身信息量较大时,为避免耗费大量时间移动记录,可以用链表作为存储结构。
 
 
排序简介 
排序是数据处理中经常使用的一种重要运算,在计算机及其应用系统中,花费在排序上的时间在系统运行时间中占有很大比重;并且排序本身对推动算法分析的发展也起很大作用。目前已有上百种排序方法,但尚未有一个最理想的尽如人意的方法,本章介绍常用的如下排序方法,并对它们进行分析和比较。
1、插入排序(直接插入排序、折半插入排序、希尔排序);
2、交换排序(起泡排序、快速排序);
3、选择排序(直接选择排序、堆排序);
4、归并排序;
5、基数排序;
学习重点 
1、掌握排序的基本概念和各种排序方法的特点,并能加以灵活应用;
2、掌握插入排序(直接插入排序、折半插入排序、希尔排序)、交换排序(起泡排序、快速排序)、选择排序(直接选择排序、堆排序)、二路归并排序的方法及其性能分析方法;
3、了解基数排序方法及其性能分析方法。
排序(sort)或分类
     所谓排序,就是要整理文件中的记录,使之按关键字递增(或递减)次序排列起来。其确切定义如下:
  输入:n个记录R1,R2,…,Rn,其相应的关键字分别为K1,K2,…,Kn。
  输出:Ril,Ri2,…,Rin,使得Ki1≤Ki2≤…≤Kin。(或Ki1≥Ki2≥…≥Kin)。
1.被排序对象--文件
  被排序的对象--文件由一组记录组成。
  记录则由若干个数据项(或域)组成。其中有一项可用来标识一个记录,称为关键字项。该数据项的值称为关键字(Key)。
  注意:
     在不易产生混淆时,将关键字项简称为关键字。
2.排序运算的依据--关键字
     用来作排序运算依据的关键字,可以是数字类型,也可以是字符类型。
     关键字的选取应根据问题的要求而定。
【例】在高考成绩统计中将每个考生作为一个记录。每条记录包含准考证号、姓名、各科的分数和总分数等项内容。若要惟一地标识一个考生的记录,则必须用"准考证号"作为关键字。若要按照考生的总分数排名次,则需用"总分数"作为关键字。
排序的稳定性
     当待排序记录的关键字均不相同时,排序结果是惟一的,否则排序结果不唯一。
     在待排序的文件中,若存在多个关键字相同的记录,经过排序后这些具有相同关键字的记录之间的相对次序保持不变,该排序方法是稳定的;若具有相同关键字的记录之间的相对次序发生变化,则称这种排序方法是不稳定的。
  注意:
     排序算法的稳定性是针对所有输入实例而言的。即在所有可能的输入实例中,只要有一个实例使得算法不满足稳定性要求,则该排序算法就是不稳定的。
排序方法的分类
1.按是否涉及数据的内、外存交换分
     在排序过程中,若整个文件都是放在内存中处理,排序时不涉及数据的内、外存交换,则称之为内部排序(简称内排序);反之,若排序过程中要进行数据的内、外存交换,则称之为外部排序。
  注意:
     ① 内排序适用于记录个数不很多的小文件
     ② 外排序则适用于记录个数太多,不能一次将其全部记录放人内存的大文件。
2.按策略划分内部排序方法
     可以分为五类:插入排序、选择排序、交换排序、归并排序和分配排序。
排序算法分析
1.排序算法的基本操作
     大多数排序算法都有两个基本的操作:
  (1) 比较两个关键字的大小;
  (2) 改变指向记录的指针或移动记录本身。
  注意:
     第(2)种基本操作的实现依赖于待排序记录的存储方式。
2.待排文件的常用存储方式
(1) 以顺序表(或直接用向量)作为存储结构
    排序过程:对记录本身进行物理重排(即通过关键字之间的比较判定,将记录移到合适的位置)
(2) 以链表作为存储结构
  排序过程:无须移动记录,仅需修改指针。通常将这类排序称为链表(或链式)排序;
(3) 用顺序的方式存储待排序的记录,但同时建立一个辅助表(如包括关键字和指向记录位置的指针组成的索引表)
  排序过程:只需对辅助表的表目进行物理重排(即只移动辅助表的表目,而不移动记录本身)。适用于难于在链表上实现,仍需避免排序过程中移动记录的排序方法。
3.排序算法性能评价
(1) 评价排序算法好坏的标准
  评价排序算法好坏的标准主要有两条:
     ① 执行时间和所需的辅助空间
     ② 算法本身的复杂程度
(2) 排序算法的空间复杂度
  若排序算法所需的辅助空间并不依赖于问题的规模n,即辅助空间是O(1),则称之为就地排序(In-PlaceSou)。
  非就地排序一般要求的辅助空间为O(n)。
(3) 排序算法的时间开销
  大多数排序算法的时间开销主要是关键字之间的比较和记录的移动。有的排序算法其执行时间不仅依赖于问题的规模,还取决于输入实例中数据的状态。
文件的顺序存储结构表示
  #define n l00 //假设的文件长度,即待排序的记录数目
  typedef int KeyType; //假设的关键字类型
  typedef struct{ //记录类型
    KeyType key; //关键字项
    InfoType otherinfo;//其它数据项,类型InfoType依赖于具体应用而定义
   }RecType;
  typedef RecType SeqList[n+1];//SeqList为顺序表类型,表中第0个单元一般用作哨兵
  注意:
     若关键字类型没有比较算符,则可事先定义宏或函数来表示比较运算。
【例】关键字为字符串时,可定义宏"#define LT(a,b)(Stromp((a),(b))<0)"。那么算法中"a<b"可用"LT(a,b)"取代。若使用C++,则定义重载的算符"<"更为方便。
 
按平均时间将排序分为四类:
(1)平方阶(O(n2))排序
     一般称为简单排序,例如直接插入、直接选择和冒泡排序;
(2)线性对数阶(O(nlgn))排序
     如快速、堆和归并排序;
(3)O(n1+£)阶排序
     £是介于0和1之间的常数,即0<£<1,如希尔排序;
(4)线性阶(O(n))排序
     如桶、箱和基数排序。
各种排序方法比较
     简单排序中直接插入最好,快速排序最快,当文件为正序时,直接插入和冒泡均最佳。
影响排序效果的因素
     因为不同的排序方法适应不同的应用环境和要求,所以选择合适的排序方法应综合考虑下列因素:
  ①待排序的记录数目n;
  ②记录的大小(规模);
  ③关键字的结构及其初始状态;
  ④对稳定性的要求;
  ⑤语言工具的条件;
  ⑥存储结构;
  ⑦时间和辅助空间复杂度等。
不同条件下,排序方法的选择
(1)若n较小(如n≤50),可采用直接插入或直接选择排序。
     当记录规模较小时,直接插入排序较好;否则因为直接选择移动的记录数少于直接插人,应选直接选择排序为宜。
(2)若文件初始状态基本有序(指正序),则应选用直接插人、冒泡或随机的快速排序为宜;
(3)若n较大,则应采用时间复杂度为O(nlgn)的排序方法:快速排序、堆排序或归并排序。
     快速排序是目前基于比较的内部排序中被认为是最好的方法,当待排序的关键字是随机分布时,快速排序的平均时间最短;
     堆排序所需的辅助空间少于快速排序,并且不会出现快速排序可能出现的最坏情况。这两种排序都是不稳定的。
     若要求排序稳定,则可选用归并排序。但本章介绍的从单个记录起进行两两归并的  排序算法并不值得提倡,通常可以将它和直接插入排序结合在一起使用。先利用直接插入排序求得较长的有序子文件,然后再两两归并之。因为直接插入排序是稳定的,所以改进后的归并排序仍是稳定的。
4)在基于比较的排序方法中,每次比较两个关键字的大小之后,仅仅出现两种可能的转移,因此可以用一棵二叉树来描述比较判定过程。
     当文件的n个关键字随机分布时,任何借助于"比较"的排序算法,至少需要O(nlgn)的时间。
     箱排序和基数排序只需一步就会引起m种可能的转移,即把一个记录装入m个箱子之一,因此在一般情况下,箱排序和基数排序可能在O(n)时间内完成对n个记录的排序。但是,箱排序和基数排序只适用于像字符串和整数这类有明显结构特征的关键字,而当关键字的取值范围属于某个无穷集合(例如实数型关键字)时,无法使用箱排序和基数排序,这时只有借助于"比较"的方法来排序。
     若n很大,记录的关键字位数较少且可以分解时,采用基数排序较好。虽然桶排序对关键字的结构无要求,但它也只有在关键字是随机分布时才能使平均时间达到线性阶,否则为平方阶。同时要注意,箱、桶、基数这三种分配排序均假定了关键字若为数字时,则其值均是非负的,否则将其映射到箱(桶)号时,又要增加相应的时间。
(5)有的语言(如Fortran,Cobol或Basic等)没有提供指针及递归,导致实现归并、快速(它们用递归实现较简单)和基数(使用了指针)等排序算法变得复杂。此时可考虑用其它排序。
(6)本章给出的排序算法,输人数据均是存储在一个向量中。当记录的规模较大时,为避免耗费大量的时间去移动记录,可以用链表作为存储结构。譬如插入排序、归并排序、基数排序都易于在链表上实现,使之减少记录的移动次数。但有的排序方法,如快速排序和堆排序,在链表上却难于实现,在这种情况下,可以提取关键字建立索引表,然后对索引表进行排序。然而更为简单的方法是:引人一个整型向量t作为辅助表,排序前令t[i]=i(0≤i<n),若排序算法中要求交换R[i]和R[j],则只需交换t[i]和t[j]即可;排序结束后,向量t就指示了记录之间的顺序关系:
        R[t[0]].key≤R[t[1]].key≤…≤R[t[n-1]].key
  若要求最终结果是:
       R[0].key≤R[1].key≤…≤R[n-1].key
则可以在排序结束后,再按辅助表所规定的次序重排各记录,完成这种重排的时间是O(n)。