package net.okren.java; import java.util.*; import java.io.*; public class SortTest{ //插入排序; public static void insertionSort(Comparable[] data){ System.out.println("insertSort() ..."); int j; for(int p = 1; p < data.length; p++){ Comparable temp = data[p]; for(j = p; j > 0 && temp.compareTo(data[j - 1]) < 0; j--){ data[j] = data[j - 1]; } data[j] = temp; } } //希尔排序; public static void shellSort(Comparable[] data){ System.out.println("shellSort() ..."); int j; for(int gap = data.length / 2; gap > 0; gap /= 2){ for(int i = gap; i < data.length; i++){ Comparable temp = data[i]; for(j = i; j >= gap && temp.compareTo(data[j - gap]) < 0; j -= gap){ data[j] = data[j - gap]; } data[j] = temp; } } } //堆排序; public static void heapSort(Comparable[] data){ System.out.println("heapSort() ..."); //buildHeap, for(int i = data.length / 2; i >= 0; i--){ shiftDown(data, i, data.length); } //deleteMax, for(int i = data.length - 1; i > 0; i--){ swap(data, 0, i); shiftDown(data, 0, i); } } private static void shiftDown(Comparable[] data, int i, int n){ int child; Comparable temp; for(temp = data[i]; leftChild(i) < n; i = child){ child = leftChild(i); if(child != n - 1 && data[child].compareTo(data[child + 1]) < 0){ child++; } if(temp.compareTo(data[child]) < 0){ data[i] = data[child]; }else{ break; } } data[i] = temp; } private static int leftChild(int i){ return 2 * i + 1; } //冒泡排序 public static void bubbleSort(Comparable[] data){ System.out.println("bubbleSort() ..."); for(int i = data.length - 1; i > 0; i--){ boolean isChanged = false; for(int j = 0; j < i; j++){ if(data[j].compareTo(data[j + 1]) > 0){ Comparable temp = data[j]; data[j] = data[j + 1]; data[j + 1] = temp; isChanged = true; } } if(!isChanged) break; } } //快速排序; public static void quickSort(Comparable[] data){ System.out.println("quickSort() ..."); quickSort(data, 0, data.length - 1); } private static void quickSort(Comparable[] data, int left, int right){ if(left + 10 <= right){ Comparable pivot = middle3(data, left, right);//执行三数中值分割法, int i = left, j = right - 1; for(;;){ while(data[++i].compareTo(pivot) < 0){} while(data[--j].compareTo(pivot) > 0){} if(i < j){ swap(data, i, j); }else{ break; } } swap(data, i, right - 1); quickSort(data, left, i -1); quickSort(data, i + 1, right); }else{ System.out.println("insertionSort() ..."); insertionSort(data); } } private static Comparable middle3(Comparable[] data, int left, int right){ int center = (left + right) / 2; if(data[center].compareTo(data[left]) < 0){ swap(data, left, center); } if(data[right].compareTo(data[left]) < 0){ swap(data, left, right); } if(data[right].compareTo(data[center]) < 0){ swap(data, center, right); } swap(data, center, right - 1); return data[right - 1]; } private static void swap(Comparable[] data, int i, int j){ Comparable temp = data[i]; data[i] = data[j]; data[j] = temp; } //归并排序; public static void mergeSort(Comparable[] data){ System.out.println("mergeSort() ..."); Comparable[] tempData = new Comparable[data.length]; mergeSort(data, tempData, 0, data.length - 1); } private static void mergeSort(Comparable[] data, Comparable[] tempData, int left, int right){ if(left < right){ int center = (left + right) / 2; mergeSort(data, tempData, left, center); mergeSort(data, tempData, center + 1, right); merge(data,tempData, left, center + 1, right); } } private static void merge(Comparable[] data, Comparable[] tempData, int leftPos, int rightPos, int rightEnd){ int leftEnd = rightPos - 1; int tempPos = leftPos; int num = rightEnd - leftPos + 1; while(leftPos <= leftEnd && rightPos <= rightEnd){ if(data[leftPos].compareTo(data[rightPos]) <= 0){ tempData[tempPos++] = data[leftPos++]; }else{ tempData[tempPos++] = data[rightPos++]; } } while(leftPos <= leftEnd){ tempData[tempPos++] = data[leftPos++]; } while(rightPos <= rightEnd){ tempData[tempPos++] = data[rightPos++]; } for(int i = 0; i < num; i++, rightEnd--){ data[rightEnd] = tempData[rightEnd]; } } // 构建初始数据 public static Comparable[] getData(int num){ Comparable[] data = new Comparable[num]; Random r = new Random(); for(int i = 0; i < data.length; i++){ data[i] = r.nextInt(num); } return data; } //从键盘输入 构建初始数据; public static Comparable[] getInputData(){ System.out.println("eg.: 3,6,1,2,7,2,6, ..."); try{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String line = in.readLine(); StringTokenizer st = new StringTokenizer(line, ","); Comparable[] data = new Comparable[st.countTokens()]; int i = 0; while(st.hasMoreTokens()){ data[i++] = Integer.parseInt(st.nextToken()); } return data; }catch(IOException e){ e.printStackTrace(); return null; } } public static void main(String[] args){ //Comparable[] data = getData(20); Comparable[] data = getInputData(); //排序前输出 for(int i = 0; i < data.length; i++){ System.out.print(data[i] + ", "); } System.out.println(); //排序 //insertionSort(data); //shellSort(data); //bubbleSort(data); //mergeSort(data); //quickSort(data); heapSort(data); //排序后输出 for(int i = 0; i < data.length; i++){ System.out.print(data[i] + ", "); } System.out.println(); } }