标签:bsp date min end amp multiple 希尔 基数排序 lag
/** * ┏┓ ┏┓ * ┏┛┻━━━┛┻┓ * ┃ ┃ * ┃ ━ ┃ * ┃ ┳┛ ┗┳ ┃ * ┃ ┃ * ┃ ┻ ┃ * ┃ ┃ * ┗━┓ ┏━┛ * ┃ ┃神兽保佑 * ┃ ┃代码无BUG! * ┃ ┗━━━┓ * ┃ ┣┓ * ┃ ┏┛ * ┗┓┓┏━┳┓┏┛ * ┃┫┫ ┃┫┫ * ┗┻┛ ┗┻┛ * * @Description * @Author 晓波 * @Date 2019/02/18 12:21 */ public class SortUtil { //冒泡排序 public static void buddle(int[] a){ int len=a.length; int flag=len; while (flag>0){ flag=0; for (int i=1;i<len;i++){ if (a[i-1]>a[i]){ int temp=a[i]; a[i]=a[i-1]; a[i-1]=temp; flag=i; } } len=flag; } } //选择排序 public static void select(int[] a){ for (int i=0;i<a.length-1;i++){ int min=i; for (int j=i+1;j<a.length;j++){ if (a[min]>a[j]){ min=j; } } if (min!=i){ int temp=a[min]; a[min]=a[i]; a[i]=temp; } } } //插入排序 public static void insert(int[] a){ for (int i=1;i<a.length;i++){ int temp=a[i]; int j=i; while (j>0&&a[j-1]>temp){ a[j]=a[j-1]; j--; } a[j]=temp; } } //快速排序 public static void quick(int[] a,int start,int end){ if (start>=end){ return; } int left=start; int right=end; int partition=a[start]; while (left<right){ while (left<right&&a[right]>=partition){ right--; } while (left<right&&a[left]<=partition){ left++; } if (left<right){ int temp=a[left]; a[left]=a[right]; a[right]=temp; } } a[start]=a[left]; a[left]=partition; quick(a,start,left-1); quick(a,left+1,end); } //希尔排序 public static void shell(int[] a){ for (int i=a.length/2;i>0;i=i/2){ for (int j=i;j<a.length;j++){ int k=j; while (k-i>=0&&a[k]<a[k-i]){ int temp=a[k-i]; a[k-i]=a[k]; a[k]=temp; k=k-i; } } } } //初始化堆 public static void buildMaxHesp(int[] a,int lastIndex){ for (int i=(lastIndex-1)/2;i>=0;i--){ int k=i; int childIndex=2*k+1; if (childIndex+1<=lastIndex&&a[childIndex]<a[childIndex+1]){ childIndex++; } if (a[k]<a[childIndex]){ int temp=a[k]; a[k]=a[childIndex]; a[childIndex]=temp; } } } //堆排序 public static void heap(int[] a){ for (int i=0;i<a.length-1;i++){ buildMaxHesp(a,a.length-1-i); int temp=a[0]; a[0]=a[a.length-1-i]; a[a.length-1-i]=temp; } } //合并两个有序序列 public static void merge(int[] a,int start,int mid,int end){ int[] temp=new int[a.length]; int i=start; int j=mid+1; int k=start; while (i<=mid&&j<=end){ if (a[i]<a[j]){ temp[k++]=a[i++]; }else { temp[k++]=a[j++]; } } while (i<=mid){ temp[k++]=a[i++]; } while (j<=end){ temp[k++]=a[j++]; } for (int n=start;n<=end;n++){ a[n]=temp[n]; } } //归并排序 public static void merge(int[] a,int start,int end){ if (start<end){ int mid=(start+end)/2; merge(a,start,mid); merge(a,mid+1,end); merge(a,start,mid,end); } } //基数排序 public static void radix(int[] a,int radix){ int multiple=1; int[][] b=new int[10][a.length]; int[] count=new int[a.length]; int digit; for (int i=1;i<=radix;i++){ for (int temp:a){ digit=(temp/multiple)%10; b[digit][count[digit]++]=temp; } int k=0; for (int m=0;m<10;m++){ if (count[m]==0){ continue; } for (int n=0;n<count[m];n++){ a[k++]=b[m][n]; } count[m]=0; } multiple*=10; } } }
标签:bsp date min end amp multiple 希尔 基数排序 lag
原文地址:https://www.cnblogs.com/xiaobo520/p/10402495.html