Java 实现常见排序算法

Java 实现常见排序算法

1. 综述

  复习常见排序算法,用Java实现。

 

2. 代码

  1 package cn.edu.tju.scs;
  2 
  3 public class Sort {
  4     public static void main(String [] args){
  5         
  6         int[] intArray = {3, 5, 1, 4, 7, 9, 8, 2, 6};
  7         System.out.print("排序前:");print(intArray);
  8         long beginTime = System.nanoTime();
  9         
 10 //        bubbleSort(intArray);
 11 //        selectSort(intArray);
 12 //        insertionSort(intArray);
 13 //        mergeSort(intArray);
 14 //        quickSort(intArray);
 15 //        heapSort(intArray);
 16 
 17         long endTime = System.nanoTime();    
 18         System.out.print("排序后:");print(intArray); 
 19         System.out.println("用时:" + (endTime - beginTime) + "ns");
 20     }
 21 
 22     private static void heapSort(int[] intArray) {
 23         // build Max Heap
 24         for (int i = intArray.length - 1; i >= 0; i--)
 25             maxHeapify(intArray, intArray.length - 1, i);
 26         
 27         for (int i = intArray.length - 1; i > 0; i--) {
 28             swap(intArray, 0, i);
 29             maxHeapify(intArray, i - 1, 0);
 30         }
 31     }
 32 
 33     private static void maxHeapify(int[] intArray, int lastNode, int rootNode) {
 34         int lchild = 2 * rootNode + 1;
 35         int rchild = lchild + 1;
 36 
 37         // has two children
 38         while (lchild < lastNode) {
 39             if (intArray[rootNode] >= intArray[lchild] && intArray[rootNode] >= intArray[rchild])
 40                 return;
 41             
 42             if (intArray[lchild] >= intArray[rchild]) {
 43                 swap(intArray, lchild, rootNode);
 44                 rootNode = lchild;
 45             } else {
 46                 swap(intArray, rchild, rootNode);
 47                 rootNode = rchild;
 48             }
 49             lchild = 2 * rootNode + 1;
 50             rchild = lchild + 1;
 51         }
 52         // has only left child
 53         if (lchild == lastNode) {
 54             if (intArray[rootNode] < intArray[lchild])
 55                 swap(intArray, lchild, rootNode);
 56         }
 57     }
 58 
 59     private static void quickSort(int[] intArray) {
 60         quickSort(intArray, 0, intArray.length - 1);
 61     }
 62 
 63     private static void quickSort(int[] intArray, int begin, int end) {
 64         if (end - begin < 1) return;
 65         if (end - begin < 2){
 66             if (intArray[end] < intArray[begin])
 67                 swap(intArray, begin, end);
 68             return;
 69         }
 70         // partition begin
 71         int left = begin;
 72         int right = end;
 73         int x = intArray[left];
 74         while (left < right) {
 75             while (intArray[right] >= x && left < right) 
 76                 right--;
 77             if (left < right){
 78                 intArray[left] = intArray[right];
 79                 left++;
 80             }
 81             
 82             while (intArray[left] < x && left < right) 
 83                 left++;
 84             if (left < right){
 85                 intArray[right] = intArray[left];
 86                 right--;
 87             }
 88         }
 89         intArray[right] = x;
 90         // partition end
 91         quickSort(intArray, begin, right - 1);
 92         quickSort(intArray, right + 1, end);
 93     }
 94 
 95     private static void mergeSort(int[] intArray) {
 96         mergeSort(intArray, 0, intArray.length - 1);
 97     }
 98 
 99     private static void mergeSort(int[] intArray, int begin, int end) {
100         if (end - begin < 2){
101             // sort 2 number
102             if (intArray[begin] > intArray[end])
103                 swap(intArray, begin, end);
104         } else {
105             int mid = (begin + end) / 2;
106             mergeSort(intArray, begin, mid);
107             mergeSort(intArray, mid + 1, end);
108             merge(intArray, begin, mid, end);
109         }
110     }
111 
112     private static void merge(int[] intArray, int begin, int mid, int end) {
113         int [] tmpArray = new int[end - begin + 1];
114         int iter1 = begin;
115         int iter2 = mid + 1;
116         int i = 0;
117         while (iter1 <= mid && iter2 <= end) {
118             if (intArray[iter2] < intArray[iter1]) {
119                 tmpArray[i] = intArray[iter2];
120                 iter2++;
121                 i++;
122             } else {
123                 tmpArray[i] = intArray[iter1];
124                 iter1 ++;
125                 i++;
126             }                
127         }
128         if (iter1 > mid){
129             for (int j = iter2; j <= end; j++) {
130                 tmpArray[i++] = intArray[j];
131             }
132         }
133         if (iter2 > end){
134             for (int j = iter1; j <= mid; j++){
135                 tmpArray[i++] = intArray[j];
136             }
137         }
138         for (i = 0; i < tmpArray.length; i++)
139             intArray[begin + i] = tmpArray[i];
140     }
141 
142     private static void insertionSort(int[] intArray) {
143         
144         for (int i = 1; i < intArray.length; i++) {
145             int flag = i;
146             if (intArray[flag] >= intArray[flag - 1])
147                 continue;
148             int temp = intArray[flag]; // temp need to inserted into the sequence
149             if (intArray[flag] < intArray[0]) {
150                 // move all items one back
151                 for (int j = flag; j > 0; j--) {
152                     intArray[j] = intArray[j - 1];
153                 }
154                 intArray[0] = temp;
155             } else {    // need insertion
156                 int left = 0, right = flag - 1, mid = 0;
157                 while (right - left != 1) {
158                     mid = (right + left) / 2;
159                     if (intArray[mid] <= temp)
160                         left = mid;
161                     else 
162                         right = mid;                        
163                 }
164                 for (int j = flag; j > right; j--){
165                     intArray[j] = intArray[j - 1];
166                 }
167                 intArray[right] = temp;
168             }
169         }
170     }
171 
172     private static void selectSort(int[] intArray) {
173         int tmp = 0;
174         for (int i = 0; i < intArray.length; i++) {
175             tmp = intArray[i];
176             for (int j = i + 1; j < intArray.length; j++)
177                 if (tmp > intArray[j]) {
178                     int temp = tmp;
179                     tmp = intArray[j];
180                     intArray[j] = temp;
181                 }
182             intArray[i] = tmp;
183         }        
184     }
185 
186     private static void print(int[] intArray) {
187         for(int i : intArray) {
188             System.out.print(i + " ");
189         }
190         System.out.println();
191     }
192 
193     private static void bubbleSort(int[] intArray) {
194         for(int i = 0; i < intArray.length; i++)
195             for(int j = 0; j < intArray.length - 1 - i; j++)
196                 if(intArray[j] > intArray[j + 1])
197                     swap(intArray, j, j + 1);
198     }
199 
200     private static void swap(int[] intArray, int i, int j) {
201         int tmp = intArray[i];
202         intArray[i] = intArray[j];
203         intArray[j] = tmp;
204     }
205 }

 

posted @ 2016-08-08 09:31  永哼哼  阅读(168)  评论(0编辑  收藏  举报