排序算法

2020-12-13 16:58

阅读:551

标签:算法   ddl   i++   选择排序   middle   static   int start   end   turn   

public static void kuaipai(int[] arr, int lift, int right) {//快速排序
int l = lift;
int r = right;
int p = arr[(lift + right) / 2];
int temp = 0;
while (l while (arr[l] l++;
}
while (arr[r] > p) {
r--;
}
if (l >= r) {
break;
}
temp = arr[r];
arr[r] = arr[l];
arr[l] = temp;

if (arr[r] == p) {
l++;
}
if (arr[l] == p) {
r--;
}
}
if (l == r) {
l += 1;
r -= 1;
}
if (lift kuaipai(arr, lift, r);
}
if (l kuaipai(arr, l, right);
}
}
----------------------------------------------------------------------
//给定三个颜色的数字,只有0,1,2,然后排序
public static int[] sort1(int[] arr){

int zero=-1;
int two=arr.length;
for(int i=0;i if(arr[i]==1){
i++;
}else if(arr[i]==0){
int a=arr[i];
arr[i]=arr[++zero];
arr[zero]=a;
}else{
int a=arr[i];
arr[i]=arr[--two];
arr[two]=a;


}
}
return arr;
}

///插入排序
public static int[] inster(int[] arr){

for(int i=1;i int a=arr[i];
int j=i-1;
while (j>=0&&arr[j]>a){
arr[j+1]=arr[j];
j--;

}
arr[j+1]=a;
}

return arr;
}

//冒泡排序
public static int[] maopao(int[] arr){

for (int i=0;i for(int j=0;j if(arr[j]>arr[j+1]){
int temp=arr[j+1];
arr[j+1]=arr[j];
arr[j]=temp;
}
}
}
return arr;
}
//选择排序
public static int[] xuanze(int[] arr){

for (int i=0;i for (int j=i+1;j if(arr[i]>arr[j]){
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
return arr;
}

public static void sort2(int[] arr,int start,int end){
if(start int l=start;
int r=end;
int middle=(l+r)/2;
sort2(arr,l,middle);
sort2(arr,middle+1,l);
merger(arr, start,middle, end);
}
}
private static void merger(int[] arr,int start,int middle,int end){

int[] temp=new int[end-start+1];
int l=start;
int r=middle;
int k=0;
while(l if (arr[l] temp[k++]=arr[l++];
}else {
temp[k++]=arr[r++];
}
if(l while (l temp[k++]=arr[l++];
}
}
if(r while (r temp[k++]=arr[r++];
}
}
}
for (int i=0;i arr[start++]=temp[i];
}
}


排序算法

标签:算法   ddl   i++   选择排序   middle   static   int start   end   turn   

原文地址:https://www.cnblogs.com/shanbaoxin/p/11622605.html


评论


亲,登录后才可以留言!