package xutao.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.StringReader; import java.util.Date; import java.util.Stack; public class Helloworld { public static void main(String[] args) { final int size = 10000; int[] array = new int[size]; array[0] = -1;//为方便堆排序所作 for(int i =1;i<size;i++) { array[i] = (int)(Math.random()*2*size); } Sort[] sorts = new Sort[8]; sorts[0] = new BubbleSort(); sorts[1] = new SelectionSort(); sorts[2] = new InsertionSort(); sorts[3] = new ShellSort(); sorts[4] = new MergeSort(); sorts[5] = new QuickSort(); sorts[6] = new QuickSort1(); sorts[7] = new HeapSort(); for(Sort sort : sorts) { int[] eachArray = new int[size]; System.arraycopy(array, 0, eachArray, 0, size); sort.nums = eachArray; } for(Sort sort : sorts) { double start = new Date().getTime(); sort.sort(); double end = new Date().getTime(); System.out.println(sort.toString() + "耗时: " + (end-start)); } } } abstract class Sort { public void show() { for(int num : nums) { System.out.println(num); } } protected void swap(int i,int j) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } public abstract void sort(); public int[] nums = new int[]{1,9,7,4,8,2,5,6,3}; } class BubbleSort extends Sort{ public void sort() { boolean flag = true;//有逆序 for(int i=0;i<nums.length-1&&flag;i++) { flag = false; for(int j=0;j<nums.length-1-i;j++) { if(nums[j] > nums[j+1]) { swap(j,j+1); flag = true; } } } } public String toString() { return "BubbleSort"; } } class SelectionSort extends Sort { public void sort() { for(int i =0; i< nums.length -1;i++) { //每次从i直到最后的区间里面选择最小的和i交换 swap(i, smallest(i)); } } private int smallest(int i) { int small = i; for(int k = i+1;k<nums.length; k++) { if(nums[k]<nums[small]) small = k; } return small; } public String toString() { return "SelectionSort"; } } class InsertionSort extends Sort { public void sort() { for(int i = 1;i<nums.length;i++) { //每次插入一个数字 insert(i); } } private void insert(int i) { for(;i>=1;i--) { if(nums[i]<nums[i-1]) swap(i,i-1); else break; } } public String toString() { return "InsertionSort"; } } class ShellSort extends Sort { public void sort() { int width = nums.length; do { width = width/3 + 1; insertionSortByStep(width); }while(width > 1); } private void insertionSortByStep(int width) { for(int i=0;i<width;i++) { insertionEachSubGroup(i,width); } } private void insertionEachSubGroup(int i,int width) { for(int k=i+width;k<nums.length;k+=width) { for(int current=k;current>=i+width;current-=width) { if(nums[current]<nums[current-width]) swap(current,current-width); else break; } } } public String toString() { return "ShellSort"; } } class HeapSort extends Sort { public void sort() { for(int i= nums.length/2;i>=1;i--) { HeapAdjust(i,nums.length-1); } } private void HeapAdjust(int i,int max) { int temp = nums[i]; for(int j = 2*i;j<=max;j*=2) { if(j<max && nums[j]<nums[j+1]) ++j; if(temp >= nums[j]) break; nums[i] = nums[j]; i = j; } nums[i] = temp; } public void show() { for(int i = nums.length-1;i>=1;i--) { System.out.println(nums[1]); swap(1,i); HeapAdjust(1,i-1); } } public String toString() { return "HeapSort"; } } class MergeSort extends Sort { public void sort() { int[] copyNums = new int[nums.length]; recursiveSort(nums,copyNums,0,nums.length-1); } private void recursiveSort(int[] nums,int[] copyNums,int low,int high) { if(low<high) { int middle = (low + high) / 2 ; recursiveSort(nums,copyNums,low,middle); recursiveSort(nums,copyNums,middle+1,high); merge(nums,copyNums,low,middle,high); } } private void merge(int[] nums,int[] copyNums,int low,int middle,int high) { for(int i = low;i <= high;i++) { copyNums[i] = nums[i]; } int realCurrent = low; int leftCurrent = low; int rightCurrent = middle + 1; while(leftCurrent <= middle && rightCurrent <= high) { if(copyNums[leftCurrent] < copyNums[rightCurrent]) { nums[realCurrent++] = copyNums[leftCurrent++]; } else { nums[realCurrent++] = copyNums[rightCurrent++]; } } while(leftCurrent<=middle) { nums[realCurrent++] = copyNums[leftCurrent++]; } while(rightCurrent<=high) { nums[realCurrent++] = copyNums[rightCurrent++]; } } public String toString() { return "MergeSort"; } } class QuickSort extends Sort { public void sort() { quickSort(nums,0,nums.length-1); } private void quickSort(int[] nums,int low,int high) { if(low<high) { int pivot = Partition(low,high); quickSort(nums,low,pivot-1); quickSort(nums,pivot+1,high); } } private int Partition(int low,int high) { int pivotKey = nums[low]; while(low<high) { while(low<high && nums[high]>=pivotKey) { high--; } swap(low,high); while(low<high && nums[low]<= pivotKey)//注意==号是需要的,否则将死循环 { low++; } swap(low,high); } return low; } public String toString() { return "QuickSort"; } } class QuickSort1 extends Sort { private final int MAX_LENGTH_INSERTION_SORT = 20; public void sort() { quickSort(nums,0,nums.length-1); } private void quickSort(int[] nums,int low,int high) { if( (high-low)>MAX_LENGTH_INSERTION_SORT) //优化1 { while(low < high) { int pivot = Partition(low,high); quickSort(nums,low,pivot-1); low = pivot + 1;//优化2 } } else { //插入排序 for(int i = low+1;i<=high;i++) { for(int k = i ;k>=low+1;k--) { if(nums[k]<nums[k-1]) swap(k,k-1); else break; } } } } private int Partition(int low,int high) { int middle = low + (high - low)/2; //优化4,优化枢轴 if(nums[low] > nums[high]) { swap(low,high); } if(nums[middle] > nums[high]) { swap(low,high); }//high最大 if(nums[low] < nums[middle]) { swap(low,middle); }//保证low是中间的 int pivotKey = nums[low]; while(low<high) { while(low<high && nums[high]>=pivotKey) { high--; } nums[low] = nums[high];//优化3,用来回替换代替swap while(low<high && nums[low]<= pivotKey)//注意==号是需要的,否则将死循环 { low++; } nums[high] = nums[low]; } nums[low] = pivotKey; return low; } public String toString() { return "QuickSort1"; } }