九種排序演算法總結與Java實現
阿新 • • 發佈:2019-02-01
一、九種排序演算法總結
平均時間複雜度O(n^2): 氣泡排序、選擇排序、插入排序
平均時間複雜度O(nln): 快速排序、歸併排序、堆排序
時間複雜度介於O(nlgn)和O(n^2):希爾排序
時間複雜度O(n+k):計數排序
時間複雜度O(d(n+k)):基數排序
穩定排序:氣泡排序、插入排序、歸併排序、計數排序、基數排序
不穩定排序:選擇排序、快速排序、堆排序、希爾排序
二、九種排序演算法Java實現
package sort; import java.util.Arrays; public class Sort { static void bubbleSort(int[] array) { if(array.length<1) return; for(int i=array.length-1;i>0;i--) { for(int j=0;j<i;j++) { if(array[j]>array[j+1]) { int temp=0; temp=array[j+1]; array[j+1]=array[j]; array[j]=temp; } } } } static void selectionSort(int[] array) { if(array.length<1) return; for(int i=0;i<array.length;i++) { int currentMin=array[i]; int current=i; for(int j=i;j<array.length;j++) { if(array[j]<currentMin) { currentMin=array[j]; current=j; } } if(current!=i) { int temp=array[i]; array[i]=array[current]; array[current]=temp; } } } //部分有序,未排序的部分逐次比較 static void insertSort(int[] array) { int j=0; for(int i=0;i<array.length-1;i++) { int temp=array[i+1]; for( j=i;j>=0;j--) { if(temp>=array[j]) { //array[j+1]=temp; break; } array[j+1]=array[j]; //if(j==0) array[j]=temp; } array[j+1]=temp;//equals to the two lines above } } //多了一層迴圈,步長逐漸減小 static void shellSort(int[] array) { if(array.length<2) return; int step=array.length; while(true) { step=step/2; for(int i=step;i<array.length;i=i+step) { int temp=array[i]; for(int j=i-step;j>=0;j=j-step) { if(temp>=array[j]) { array[j+step]=temp; break; } array[j+step]=array[j]; if(j<step) array[j]=temp; } } if(step==1) break; } } //merge sort static void mergeSort(int[] array,int start,int end) { if(start>=end) return; else { int middle=start+(end-start)/2; mergeSort(array,start,middle); mergeSort(array,middle+1,end); //process of merge int[] l=new int[middle-start+1]; int[] r=new int[end-middle]; System.arraycopy(array, start, l, 0, middle-start+1); System.arraycopy(array, middle+1, r, 0, end-middle); int posl=0,posr=0; for(int i=start;i<=end;i++) { if(posl<l.length&&posr<r.length) { if(l[posl]<=r[posr]) { array[i]=l[posl++]; } else { array[i]=r[posr++]; } } else { if(posl<l.length) { array[i]=l[posl++]; } else { array[i]=r[posr++]; } } } } } //recursion+partition static void quickSort(int[] array,int p,int q) { if(p<q) { //partition int middle=0; int std=array[q]; int i=p,j=q-1; while(i<=j) { while(i<q&&array[i]<=std) i++; while(j>0&&array[j]>std) j--; if(i>=j) break; int temp1=array[i]; array[i]=array[j]; array[j]=temp1; } int temp2=array[q]; array[q]=array[i]; array[i]=temp2; middle=i; quickSort(array,p,middle-1); quickSort(array,middle+1,q); } } //suppose that only array[i] doesn't satisfy array[parent[i]]>=array[i] except root node. static void maintainHeap(int[] array,int heapSize,int i) { int largest=i; if((2*i+1)<heapSize&&array[2*i+1]>array[i]) largest=2*i+1; if((2*i+2)<heapSize&&array[2*i+2]>array[largest]) largest=2*i+2; if(largest!=i) { int temp=array[i]; array[i]=array[largest]; array[largest]=temp; maintainHeap(array,heapSize,largest); } } static void buildHeap(int[] array) { for(int i=array.length/2;i>=0;i--) { maintainHeap(array,array.length,i); } } //build and maintain biggest heap static void heapSort(int[] array) { if(array.length<2) return; buildHeap(array); for(int i=array.length-1;i>0;i--) { int temp=array[i]; array[i]=array[0]; array[0]=temp; maintainHeap(array,i,0); } } static void countingSort(int[] array,int k) { if(array.length<2) return; int[] count=new int[k]; int[] out=new int[array.length]; int i=0; for(i=0;i<k;i++) count[i]=0; for(i=0;i<array.length;i++) count[array[i]]=count[array[i]]+1;//count[i]:the total number of i in array[]. for(i=1;i<k;i++) count[i]=count[i]+count[i-1];//count[i]:the number of number that less or equals to i for(i=array.length-1;i>=0;i--) { out[count[array[i]]-1]=array[i]; count[array[i]]=count[array[i]]-1; } System.arraycopy(out, 0, array, 0, array.length); } //LSD static void radixSort(int[] array,int d) { for(int i=0;i<d;i++) { int[] count=new int[10]; int[] temp_array=new int[array.length]; int[] out=new int[array.length]; int j=0,mod=0; for(j=0;j<array.length;j++) { mod=(int)Math.pow(10,i); temp_array[j]=(array[j]/mod)%10; } for(j=0;j<array.length;j++) count[temp_array[j]]=count[temp_array[j]]+1; for(j=1;j<10;j++) count[j]=count[j]+count[j-1]; for(j=array.length-1;j>=0;j--) { out[count[temp_array[j]]-1]=array[j]; count[temp_array[j]]=count[temp_array[j]]-1; } for(j=0;j<array.length;j++) { array[j]=out[j]; out[j]=0; } } } public static void main(String[] args) { int[] marray1={2,8,7,1,3,5,6,4}; bubbleSort(marray1); System.out.println("BubbleSort: "+Arrays.toString(marray1)); int[] marray2={2,8,7,1,3,5,6,4}; selectionSort(marray2); System.out.println("SelectionSort: "+Arrays.toString(marray2)); int[] marray3={2,8,7,1,3,5,6,4}; insertSort(marray3); System.out.println("InsertSort: "+Arrays.toString(marray3)); int[] marray4={2,8,7,1,3,5,6,4}; shellSort(marray4); System.out.println("ShellSort: "+Arrays.toString(marray4)); int[] marray5={4,4,4,4};//{8,10,3,5,7,4,6,1,9,2}; mergeSort(marray5,0,marray5.length-1); System.out.println("MergeSort: "+Arrays.toString(marray5)); int[] marray6={8,8,8,8,8};//{2,8,7,1,3,5,6,4}; quickSort(marray6,0,marray6.length-1); System.out.println("QuickSort: "+Arrays.toString(marray6)); int[] marray7={10,12,2,5}; heapSort(marray7); System.out.println("heapSort: "+Arrays.toString(marray7)); int[] marray8={10,12,2,5,1,1,1,1,12,13,13}; countingSort(marray8,20);//0~19 System.out.println("CountingSort: "+Arrays.toString(marray8)); int[] marray9={999,123,31,24,56,5,888};//must be positive radixSort(marray9,3);//0~999 System.out.println("RadixSort: "+Arrays.toString(marray9)); } }