java常用四种排序源代码
2015-05-21 20:05
169 查看
选择排序public class ChooseSort { publicstatic voidmain(String[] args) { int[]x = { 2, 332, 16, 575, 203, 4, 23, 11, 345, 32 }; ChooseSort cs = new ChooseSort(); cs.selectSort(x); for(int i = 0; i < x.length; i++) { System.out.print(x[i] + " "); } } publicstatic voidselectSort(int[] a) { intminIndex = 0; inttemp = 0; if((a == null) || (a.length == 0)) return; for(int i = 0; i < a.length; i++) { minIndex = i;// 无序区的最小数据数组下标 for(int j = i + 1; j < a.length; j++) {// 在无序区中找到最小数据并保存其数组下标 if (a[j] < a[minIndex]) { minIndex = j; } } if(minIndex != i) {// 如果不是无序区的最小值位置不是默认的第一个数据,则交换之。 temp = a[i]; a[i] = a[minIndex]; a[minIndex] = temp; } } }}
插入排序public class InsertSort { publicstatic voidmain(String[] args) { inta[]={ 2, 332, 16, 575, 203, 4, 23, 11, 345, 32 };; InsertSort is=new InsertSort(); is.sort(a); for(int i=0;i<a.length;i++){ System.out.print(a[i]+" "); } } publicvoid sort(intobj[]){ for(int j=1;j<obj.length;j++){ intkey=obj[j]; inti=j-1; while(i>=0&&obj[i]>key){ obj[i+1]=obj[i]; i--; } obj[i+1]=key; } }} 冒泡排序public class BubbleSort { publicvoid bubbleSort(int a[]){ intn=a.length; for(int i=0;i<n-1;i++){ for(int j=0;j<n-i-1;j++){ if(a[j]>a[j+1]){ int temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } publicstatic voidmain(String[] args){ BubbleSort bs=new BubbleSort(); inta[]={ 2, 332, 16, 575, 203, 4, 23, 11, 345, 32 }; bs.bubbleSort(a); for(int m = 0; m < a.length; m++) { System.out.print(a[m] + " "); } }} 快速排序public class QuickSort { publicstatic voidmain(String[] args) { int[]strVoid = { 2, 332,16, 575, 203, 4, 23, 11, 345, 32 }; QuickSort sort = new QuickSort(); sort.quickSort(strVoid, 0, strVoid.length - 1); for(int m = 0; m < strVoid.length; m++) { System.out.print(strVoid[m] + " "); } } publicvoid quickSort(int strDate[], intleft, int right) { inti, j; i = left; j = right; intmiddle = strDate[(i)]; while(i < j) { inttempDate = 0; while(i < j && strDate[j] >= middle) { j = j - 1; } tempDate = strDate[j]; strDate[j] = strDate[i]; strDate[i] = tempDate; while(i < j && strDate[i] < middle) { i = i + 1; } tempDate = strDate[j]; strDate[j] = strDate[i]; strDate[i] = tempDate; } strDate[i] = middle; if(left < i - 1) { quickSort(strDate, left, i -1); } if(right > i + 1) { quickSort(strDate, i + 1,right); } }}
插入排序public class InsertSort { publicstatic voidmain(String[] args) { inta[]={ 2, 332, 16, 575, 203, 4, 23, 11, 345, 32 };; InsertSort is=new InsertSort(); is.sort(a); for(int i=0;i<a.length;i++){ System.out.print(a[i]+" "); } } publicvoid sort(intobj[]){ for(int j=1;j<obj.length;j++){ intkey=obj[j]; inti=j-1; while(i>=0&&obj[i]>key){ obj[i+1]=obj[i]; i--; } obj[i+1]=key; } }} 冒泡排序public class BubbleSort { publicvoid bubbleSort(int a[]){ intn=a.length; for(int i=0;i<n-1;i++){ for(int j=0;j<n-i-1;j++){ if(a[j]>a[j+1]){ int temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } publicstatic voidmain(String[] args){ BubbleSort bs=new BubbleSort(); inta[]={ 2, 332, 16, 575, 203, 4, 23, 11, 345, 32 }; bs.bubbleSort(a); for(int m = 0; m < a.length; m++) { System.out.print(a[m] + " "); } }} 快速排序public class QuickSort { publicstatic voidmain(String[] args) { int[]strVoid = { 2, 332,16, 575, 203, 4, 23, 11, 345, 32 }; QuickSort sort = new QuickSort(); sort.quickSort(strVoid, 0, strVoid.length - 1); for(int m = 0; m < strVoid.length; m++) { System.out.print(strVoid[m] + " "); } } publicvoid quickSort(int strDate[], intleft, int right) { inti, j; i = left; j = right; intmiddle = strDate[(i)]; while(i < j) { inttempDate = 0; while(i < j && strDate[j] >= middle) { j = j - 1; } tempDate = strDate[j]; strDate[j] = strDate[i]; strDate[i] = tempDate; while(i < j && strDate[i] < middle) { i = i + 1; } tempDate = strDate[j]; strDate[j] = strDate[i]; strDate[i] = tempDate; } strDate[i] = middle; if(left < i - 1) { quickSort(strDate, left, i -1); } if(right > i + 1) { quickSort(strDate, i + 1,right); } }}
相关文章推荐
- Java 常用四种排序源代码
- java 实现四种常用排序
- java四种常用排序冒泡排序选择排序插入排序快速排序
- 四种常用排序方法的基本思想和PHP实现源代码
- Java map 详解 - 用法、遍历、排序、常用API等
- JAVA四种基本排序的总结
- Java中解析XML的四种常用方法(3)_JDOM解析
- java常用排序方法
- 一步步学习数据结构和算法之常用排序效率分析及java实现
- java 遍历arrayList常用的四种方法
- Java更新XML的四种常用方法简介
- Java更新XML的四种常用方法简介
- JAVA中运用数组的四种排序方法
- java读取配置文件常用的四种方式
- Java更新XML的四种常用方法简介 (转)
- Java里面线程池,及常用四种线程池的使用
- Java中一些常用的排序程序代码
- java 常用排序
- Java中解析XML的四种常用方法(1)_DOM解析
- JAVA四种基本排序的总结