本文最后更新于400 天前,其中的信息可能已经过时,如有错误请发送邮件到3368129372@qq.com
手搓一下试试
冒泡排序
public void maopao(int[] array){
int n=array.length;
for(int i=0;i<n;i++){
j=n-1;
while(j>=i+1){
if(a[j]<a[j-1]){
int temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
j--;
}
}
}
归并排序
public int[] guibing(int[] array){
if(array.length==1)return array;
int n = array.length;
int[] left = Arrays.copyOfRange(array,0,n/2);
int[] right = Arrays.copyOfRange(array,n/2,n);
left = guibing(left);
right = guibing(right);
int[] ans = merge(left,right);
return ans;
}
public static int[] merge(int[] left, int[] right){
int n1 = left.length;
int n2 = right.length;
int i=0;
int j=0;
int[] ans = new int[n1+n2];
int k=0;
while(i!=n1&&j!=n2){
if(left[i]<=right[j]){
ans[k]=left[i];
i++;
}else {
ans[k]=right[j];
j++;
}
k++;
}
if(i==n1){
while(j<n2){
ans[k]=right[j];
j++;
k++;
}
}else {
while(i<n1){
ans[k]=left[i];
i++;
k++;
}
}
return ans;
}