1、冒泡
int []arr={45,76,99,45,76,24,31,76,2,99}; for (int i = 0,t; i <arr.length-1 ; i++) { for (int j = 0; j <arr.length-1-i ; j++) { if(arr[j]>arr[j+1]){ //若前面的值比后面的大,则交换值 t=arr[j+1]; arr[j+1]=arr[j]; arr[j]=t; } } } for (int i : arr) { System.out.println(i); }2、选择排序
int []arr={45,76,99,45,76,24,31,76,2,99}; for (int i = 0,MaxIx,MaxValIx,t; i <arr.length-1 ; i++) { MaxValIx=0; MaxIx=arr.length-1-i; for (int j = 1; j <=MaxIx ; j++) { if(arr[j]>arr[MaxValIx]){ MaxValIx=j; } } if (MaxValIx!= MaxIx) { t=arr[MaxIx]; arr[MaxIx]=arr[MaxValIx]; arr[MaxValIx]=t; } } for (int i : arr) { System.out.println(i); }3、插入排序
int []arr={45,76,99,45,76,24,31,76,2,99}; for (int i = 1,t,j; i <arr.length ; i++) { if (arr[i]>arr[i-1]) continue; t=arr[i]; for (j = i-1; j>=0&&t<arr[j] ; j--) { arr[j+1]=arr[j]; } arr[j+1]=t; } for (int i : arr) { System.out.println(i); }4、希尔排序
int []arr={45,76,99,45,76,24,31,76,2,99}; int step=arr.length; while((step=step/2)>=2){ for (int i = 0,t; i+step < arr.length; i++) { if (arr[i+step]<arr[i]) { t=arr[i+step]; arr[i+step]=arr[i]; arr[i]=t; } } } for (int i = 1,t,j; i < arr.length; i++) { if (arr[i]>arr[i-1]) continue; t=arr[i]; for (j = i-1; j>=0&&t<arr[j]; j--) { arr[j+1]=arr[j]; } arr[j+1]=t; } for (int i : arr) { System.out.println(i); }5、桶排序
int []arr={45,76,99,45,76,24,31,76,2,99}; final int U=10; int [][]bucket=new int[U][arr.length]; int []ixs=new int[U]; for (int i = 1,t,count; ; i*=10) { count=0; for (int j = 0; j < arr.length; j++) { count = (t=arr[j]/i)>=1 ? ++count:count; bucket[t%=U][ixs[t]++]=arr[j]; } if (count==0) break; for (int j = 0,ix=0; j < bucket.length; j++) { for (int k = 0; k <ixs[j] ; k++) { arr[ix++]=bucket[j][k]; } } for (int j = 0; j<ixs.length; j++) { ixs[j]=0; } } for (int i : arr) { System.out.println(i); }