爲了筆試,用了一下午準備了下各類排序算法的java實現。給你們提供個方便,把代碼都貼出來,算法的具體過程之後再補。java
冒泡排序算法
1 package sort; 2 3 public class BubbleSort { 4 public static void swap(int[] source,int x,int y){ 5 int temp=source[y]; 6 source[y]=source[x]; 7 source[x]=temp; 8 } 9 /* 10 * 每次將最大的氣泡沉底,須要(n-1)趟掃描,每次掃描(n-i)次 11 * 複雜度O(n2) 12 * 穩定 13 */ 14 public static void sort(int[] source){ 15 for(int i=source.length-1;i>0;i--){ 16 for(int j=0;j<i;j++){ 17 if(source[j]>source[j+1]) 18 swap(source, j, j+1); 19 } 20 } 21 } 22 23 public static void main(String[] args) { 24 int[] source={9,3,6,2,1,8,4,5}; 25 sort(source); 26 for (int i = 0; i < source.length; i++) { 27 System.out.print(source[i]); 28 } 29 } 30 }
改進冒泡排序shell
1 package sort; 2 3 public class AdvancedBubbleSort { 4 public static void swap(int[] source,int x,int y){ 5 int temp=source[y]; 6 source[y]=source[x]; 7 source[x]=temp; 8 } 9 10 public static void sort(int[] source){ 11 for(int i=source.length-1;i>0;i--){ 12 boolean exchange=false; 13 for(int j=0;j<i;j++){ 14 if(source[j]>source[j+1]){ 15 swap(source, j, j+1); 16 exchange=true; 17 } 18 } 19 if(!exchange)//若是本輪沒有發生交換,則終止排序 20 return; 21 } 22 } 23 24 public static void main(String[] args) { 25 int[] source={9,3,6,2,1,8,4,5}; 26 sort(source); 27 for (int i = 0; i < source.length; i++) { 28 System.out.print(source[i]); 29 } 30 } 31 }
選擇排序數組
1 package sort; 2 3 public class SelectionSort { 4 public static void swap(int[] source,int x,int y){ 5 int temp=source[y]; 6 source[y]=source[x]; 7 source[x]=temp; 8 } 9 //第n輪掃描選擇沒有排序的序列(即n後邊的)中最小的數值與第n處交換 10 //複雜度O(n2) 11 //不穩定 12 public static void sort(int[] source){ 13 for(int i=0;i<source.length-1;i++){ 14 for(int j=i+1;j<source.length;j++){ 15 if(source[j]<source[i]) 16 swap(source, i, j); 17 } 18 } 19 } 20 21 public static void main(String[] args) { 22 int[] source={9,3,6,2,1,8,4,5,0,7}; 23 sort(source); 24 for (int i = 0; i < source.length; i++) { 25 System.out.print(source[i]); 26 } 27 } 28 }
插入排序ui
1 package sort; 2 3 public class InsertSort { 4 public static void swap(int[] source,int x,int y){ 5 int temp=source[y]; 6 source[y]=source[x]; 7 source[x]=temp; 8 } 9 /* 10 * 注意和選擇排序的區別,選擇排序是每次在剩下沒排序序列中選擇一個最小的放到當前位置 11 * 插入排序是每次將當前位置(待排序)的放到前邊已排序序列中的合適位置 12 * 複雜度O(n2):這麼想,要掃描n-1輪,每輪n-i次 13 * 適合數據量級小於千 14 */ 15 public static void sort(int[] source){ 16 for(int i=0;i<source.length-1;i++){ 17 for(int j=i+1;j>0;j--){ 18 if(source[j]<source[j-1]) 19 swap(source, j, j-1); 20 else 21 break; 22 } 23 } 24 } 25 26 public static void main(String[] args) { 27 int[] source={9,3,6,2,1,8,4,5,0,7}; 28 sort(source); 29 for (int i = 0; i < source.length; i++) { 30 System.out.print(source[i]); 31 } 32 } 33 }
Shell排序spa
1 package sort; 2 3 public class ShellSort { 4 public static void swap(int[] source,int x,int y){ 5 int temp=source[y]; 6 source[y]=source[x]; 7 source[x]=temp; 8 } 9 /*所謂的分組插入排序,開始時分組多,組內數據少,直接插入排序較快; 10 * 逐漸分組少,組內數據量大,但組內數據接近有序狀態,因此整體快於直接插入排序 11 * O(n2)? 12 * 不穩定 13 */ 14 public static void shellPass(int[] source, int d){ 15 for(int i=d;i<source.length;i++){//from 0 16 if(source[i]<source[i-d]){ 17 swap(source, i, i-d); 18 } 19 } 20 } 21 22 public static void sort(int[] source){ 23 int increment=source.length/2+1; //保證首次分兩組才能保證充分排序 24 while(increment>1){ 25 shellPass(source, increment); 26 increment=increment/3+1; //除以3保證不會再增量等於2時無限循環 27 if(increment==1) //保證最後增量=1,單獨處理 28 shellPass(source, 1); 29 } 30 } 31 32 public static void main(String[] args) { 33 int[] source={49,38,65,97,76,13,27,49,55,4,18,0,74,06}; 34 sort(source); 35 for (int i = 0; i < source.length; i++) { 36 System.out.print(source[i]+" "); 37 } 38 } 39 }
二分排序code
1 package sort; 2 3 public class BinSort { 4 /*不要與二分搜索混淆,只是對直接插入排序的改進,每一次在已排序的序列中使用二分查找找到合適位置插入當前待排序那個數值 5 * 注意找到位置後該位置所有後移,因爲數組實現,因此後移時是倒着進行,這時當前待排序數值會被抹掉,因此temp記住它 6 * 查找O(nlog(n)),元素移動次數O(n2),故時間O(n2) 7 */ 8 public static void sort(int[] source){ 9 int low,high,mid; 10 int temp; 11 for(int i=0;i<source.length;i++){ 12 temp=source[i]; 13 low=0; 14 high=i-1; 15 while(low<=high){ 16 mid=(low+high)/2; 17 if(source[mid]>temp){ 18 high=mid-1;//要排序元素在已經排過序的數組左邊 19 }else{ 20 low=mid+1; 21 } 22 } 23 for(int j=i-1;j>high;j--){//找到了要插入的位置,而後將這個位置之後的全部元素向後移動 24 source[j+1]=source[j]; 25 } 26 source[high+1]=temp; 27 } 28 29 } 30 31 public static void main(String[] args) { 32 int[] source={49,38,65,97,76,13,27,49,55,4,18,0,74,06}; 33 sort(source); 34 for (int i = 0; i < source.length; i++) { 35 System.out.print(source[i]+" "); 36 } 37 } 38 }
快速排序blog
1 package sort; 2 3 public class QuickSort { 4 /*第一趟將起始位置值做爲基準,從後向前掃描,找出小於povit的值,替換左邊值 5 * 馬上從左邊開始掃描,查找大於povit的值,替換右邊,知道將全部值分到povit兩邊 6 * 不穩定 7 * 平均時間複雜度O(nlogn),最差複雜度O(n2) 8 */ 9 public static void sort(int[] source, int low, int high){ 10 int h=high; 11 int l=low; 12 int povit=source[low]; 13 14 while(l<h){ 15 16 while(l<h&&source[h]>=povit){ 17 h--; 18 System.out.println("h="+h); 19 } 20 21 if(l<h){ 22 int temp=source[h]; 23 source[h]=source[l]; 24 source[l]=temp; 25 l++;//加的是左邊,由於只能保證左邊加入一個不大於povit的值 26 } 27 28 while(l<h&&source[l]<=povit){ 29 l++; 30 } 31 32 if(l<h){ 33 int temp=source[h]; 34 source[h]=source[l]; 35 source[l]=temp; 36 h--;//同理,減得右邊 37 } 38 } 39 System.out.print("l="+(l+1)+"h="+(h+1)+"povit="+povit+"\n"); 40 //此時必定有l=h 41 if(l>low) 42 sort(source, low, h-1); 43 if(h<high) 44 sort(source, h+1, high); 45 } 46 47 public static void main(String[] args) { 48 int[] source={49,38,65,97,76,13,27,49,55,4,18,0,74,06}; 49 sort(source,0,source.length-1); 50 for (int i = 0; i < source.length; i++) { 51 System.out.print(source[i]+" "); 52 } 53 } 54 }
歸併排序排序
1 package sort; 2 3 public class MergeSort { 4 5 //將有二個有序數列a[first,...,mid]和a[mid+1,...,last]合併。 6 public static void mergeArray(int a[], int first, int mid, int last, int temp[]){ 7 int i=first; 8 int j=mid+1; 9 int k=0; 10 11 while(i<=mid&&j<=last){ 12 if(a[i]<a[j]) 13 temp[k++]=a[i++]; 14 else 15 temp[k++]=a[j++]; 16 } 17 18 while(i<=mid){ 19 temp[k++]=a[i++]; 20 } 21 while(j<=last){ 22 temp[k++]=a[j++]; 23 } 24 25 for(i=0;i<k;i++)//copy 26 a[first+i]=temp[i]; 27 } 28 29 /*先分割再合併,當數組只有一個元素時爲有序,有序數組之間合併獲得有序數組 30 * 穩定 31 * O(nlogn) 32 */ 33 public static void mergeSort(int a[],int first,int last,int temp[]){ 34 if(first<last){ 35 int mid=(first+last)/2; 36 mergeSort(a, first, mid, temp);//左邊有序 37 mergeSort(a, mid+1, last, temp);//右邊有序 38 mergeArray(a, first, mid, last, temp); //再將二個有序數列合併 39 } 40 } 41 42 public static void main(String[] args) { 43 int[] source={49,38,65,97,76,13,27,49,55,4,18,0,74,06}; 44 int[] temp=new int[source.length]; 45 mergeSort(source,0,source.length-1,temp); 46 for (int i = 0; i < source.length; i++) { 47 System.out.print(source[i]+" "); 48 } 49 } 50 }