排序算法
选择排序
1 public class Selection { 2 3 public void Selection(){ 4 int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51}; 5 for(int i=0;i<a.length;i++){ 6 for(int j=i;j<a.length;j++){ 7 if(a[i]>a[j]){ 8 int temp; 9 temp=a[i]; 10 a[i]=a[j]; 11 a[j]=temp; 12 } 13 14 } 15 } 16 for(int i=0;i<a.length;i++){ 17 System.out.print(a[i]+" "); 18 } 19 20 }21 }
插入排序
public class InsertSort { public static void insertSort(int[] a) { for(int i=1;i<a.length;i++){ for(int j=i;j>0 && a[j]<a[j-1];j--){ int temp=a[j]; a[j]=a[j-1]; a[j-1]=temp; } } } public static void main(String[] args) { int[] array = {49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51,2}; SortUtil util = new SortUtil(); util.print(array); insertSort(array); util.print(array); } }
希尔排序
package sort; public class ShellSort { public static void Shell(int[] a){ int n = a.length; int h = 1; while(h<n/3) h=3*h+1; while(h>=1){ for(int i=h;i<n;i++){ for(int j=i;j>=h && a[j]<a[j-h];j-=h){ int temp=a[j]; a[j]=a[j-h]; a[j-h]=temp; } } h=h/3; } } public static void main(String arg[]){ int[] array={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51,1,}; SortUtil sort =new SortUtil(); sort.print(array); ShellSort.Shell(array); sort.print(array); } }
归并排序
package sort; public class Merge { private static int[] aux; public static void sort(int[] a){ aux = new int[a.length]; sort(a, 0, a.length-1); } private static void sort(int[] a,int lo,int hi){ if(lo>=hi) return; int mid=lo+(hi-lo)/2; sort( a, lo, mid); sort( a, mid+1, hi); Merge(a,lo,mid,hi); } private static void Merge(int[] a,int lo,int mid,int hi){ int i=lo,j=mid+1; for(int k=lo;k<=hi;k++) aux[k]=a[k]; for(int k=lo;k<=hi;k++){ if(i>mid) a[k]=aux[j++]; else if(j>hi) a[k]=aux[i++]; else if(aux[i]<aux[j]) a[k]=aux[i++]; else a[k]=aux[j++]; } } public static void main(String[] args) { int[] array = {49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51,2}; SortUtil util = new SortUtil(); util.print(array); sort(array); util.print(array); } }
快速排序
public class Quick { public static void sort(int[ ] a){ //StdRandom.shuffle sort(a,0,a.length -1); } private static void sort(int[] a, int lo, int hi) { if(hi<=lo) return; int j = partition(a,lo,hi); sort(a,lo,j-1); sort(a,j+1,hi); } private static int partition(int[] a, int lo, int hi) { int v = a[lo]; int i=lo,j=hi+1; while(true){ while(v>a[++i]) if(i==hi) break; while(v<a[--j]) if(j==lo) break; if(i>=j) break; int temp=a[j]; a[j]=a[i]; a[i]=temp; } int temp = a[j]; a[j]=a[lo]; a[lo]=temp; return j; } public static void main(String[] args) { int[] array = {49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51,2}; SortUtil util = new SortUtil(); util.print(array); sort(array); util.print(array); } }
冒泡排序
public class BubbleSort{ public static void Bubble(){ int score[] = {67, 69, 75, 87, 89, 90, 99, 100}; for (int i = 0; i < score.length -1; i++){ //最多做n-1趟排序 for(int j = 0 ;j < score.length - i - 1; j++){ //对当前无序区间score[0......length-i-1]进行排序 //(j的范围很关键,这个范围是在逐步缩小的) if(score[j] > score[j + 1]){ //把小的值交换到后面 int temp = score[j]; score[j] = score[j + 1]; score[j + 1] = temp; } } System.out.print("第" + (i + 1) + "次排序结果:"); for(int a = 0; a < score.length; a++){ System.out.print(score[a] + "\t"); } System.out.println(""); } System.out.print("最终排序结果:"); for(int a = 0; a < score.length; a++){ System.out.print(score[a] + "\t"); } } public static void main(String[] args){ BubbleSort.Bubble(); } }
posted on 2016-12-13 12:16 Jessehuang 阅读(112) 评论(0) 收藏 举报
浙公网安备 33010602011771号