X Tutup
public class Solution { //------------冒泡排序算法--------------------------------- public void bubbleSort(int[] testCase){ if(testCase==null) return; int N = testCase.length; for(int i=N-1;i>=1;i--){ //冒泡到达的位置 for(int j=0;jtestCase[j+1]){ swap(testCase,j,j+1); } } } } //------------插入排序--------------------------------- public void insertSort(int[] testCase){ if(testCase==null) return; int N = testCase.length; for(int i=1;i=0;j--){ if(temp<=testCase[j]){ testCase[j+1] = testCase[j]; }else{ break; } } testCase[j+1] = temp; } } //------------选择排序--------------------------------- public void selectSort(int[] testCase){ if(testCase==null) return; int N = testCase.length; for(int i=0;i=end){ return ; } int p = partition3(testCase,start,end); quickSort(testCase,start,p-1); quickSort(testCase,p+1,end); } //找到分割线的位置 public int partition(int[] testCase,int start,int end){ int l = start,r=end; //以第一个元素作为主元 int temp = testCase[start]; while(l=temp){ r--; }else{ testCase[l] = testCase[r]; break; } } //从左到右找到第一个大于tenp的值给r while(l=1;i--){ swap(testCase,0,i); //长度此时改为i-1 sink(testCase,0,i-1); } } //上浮函数 public void swim(int[] testCase,int k,int N){ while((k-1)/2>=0 && testCase[k]>testCase[(k-1)/2]){ swap(testCase,k,(k-1)/2); k = (k-1)/2; } } //下沉 public void sink(int[] testCase,int k,int N){ while(2*k+1<=N){ int j = 2*k+1; if(j+1<=N && testCase[j]testCase[j]){ break; }else{ swap(testCase,k,j); k = j; } } } //------------归并排序--------------------------------- public void mergeSort(int[] testCase){ if(testCase==null) return; int N = testCase.length; int l =0,r = N-1; mergeSort(testCase,l,r); } //每一次将数组分为两部分[l,m][m+1,r],然后合并 public void mergeSort(int[] testCase,int l,int r){ if(l>=r)return; int middle = (l+r)/2; mergeSort(testCase,l,middle); mergeSort(testCase,middle+1,r); merge(testCase,l,middle,r); } //合并数组[l,m][m+1,r] public void merge(int[] testCase,int l,int m,int r){ int N = r-l+1; if(N==1) return ; int index1 = l; int index2 = m+1; int indexT = 0; int[] temp = new int[N]; while(index1<=m && index2<=r){ if(testCase[index1]
X Tutup