六大经典排序算法(Java版):冒泡、选择、插入、希尔、快速、归并
2016-08-14 14:33
681 查看
/** * @author Darren * @Date 2016-10-17 */ public class Main { public static void main(String[] args) { int[] ars = {2, 5, 9, 13, 28, 79, 2, 87}; System.out.println("Original Array:"); myPrintArray(ars); int[] ars1 = myBubbleSort(ars); System.out.println("BubbleSort:"); myPrintArray(ars1); int[] ars2 = myChooseSort(ars); System.out.println("ChooseSort:"); myPrintArray(ars2); int[] ars3 = myInsertSort(ars); System.out.println("InsertSort:"); myPrintArray(ars3); int[] ars4 = myShellSort(ars); System.out.println("ShellSort:"); myPrintArray(ars4); myFastSort(ars, 0,ars.length-1); System.out.println("FastSort:"); myPrintArray(ars); int[] ars11 = {2, 5, 9, 87, 28, 79, 2, 13}; myMergeSort(ars11, 0, ars11.length-1); System.out.println("MergeSort:"); myPrintArray(ars11); } /** * @param ars the input array */ public static void myPrintArray(int[] ars) { for(int a: ars) { System.out.print(a+" "); } System.out.println(); } /** * @param ars the input array * @return ars the bubble sorted array */ private static int[] myBubbleSort(int[] ars) { int i, j, temp; for(i=0; i<ars.length; i++) { for(j=ars.length-1; j>i; j--) { if(ars[j]<ars[j-1]) { temp = ars[j]; ars[j] = ars[j-1]; ars[j-1] = temp; } } } return ars; } /** * @param ars the input array * @return ars the sorted array */ public static int[] myChooseSort(int[] ars) { int i, j, index, temp; for(i=0; i<ars.length; i++) { index = i; for(j=i+1; j<ars.length; j++) { if(ars[j]<ars[index]) { index = j; } } temp = ars[index]; ars[index] = ars[i]; ars[i] = temp; } return ars; } /** * @param ars the input array * @return ars the sorted array */ public static int[] myInsertSort(int[] ars) { int i, j, ai; for(i=1; i<ars.length; i++) { ai = ars[i]; for(j=i-1; j>=0; j--) { if(ars[j]>ai) { ars[j+1] = ars[j]; }else { break; } } ars[j+1] = ai; } return ars; } /** * @param ars the input array * @return ars the sorted array */ public static int[] myShellSort(int[] ars) { int i, j, ai, d=1; while(9*d<ars.length) { d=3*d+1; } while(d>0) { for(i=d; i<ars.length; i++) { ai = ars[i]; for(j=i-d; j>=0; j-=d) { if(ars[j]>ai) { ars[j+d] = ars[j]; }else { break; } } ars[j+d] = ai; } d/=3; } return ars; } /** * @param ars the input array * @param start the start index of ars * @param end the end index of ars * @param basicValue the compared value of fast sorting method * @return ars the sorted array */ public static void myFastSort(int[] ars, int start, int end) { if(start<end) { int i=start, j=end, basicValue=ars[start]; while(i<j) { while(i<j && ars[j]>=basicValue) { j--;} if(i<j) { ars[i] = ars[j];} while(i<j && ars[i]<=basicValue) { i++;} if(i<j) { ars[j] = ars[i];} } ars[i] = basicValue; myFastSort(ars, start, i-1); myFastSort(ars, j+1, end); } } /** * @param ars the input array * @param start the start index of ars * @param end the end index of ars * @param middle the middle index of merge sorting method * @return ars the sorted array */ public static void myMergeSort(int[] ars, int start, int end) { if(start<end) { int middle = (start+end)/2; myMergeSort(ars, start, middle); myMergeSort(ars, middle+1, end); merge(ars, start, middle, end); } } public static void merge(int[] ars, int start, int middle, int end) { int i=start, j=middle+1, k=0; int[] temp = new int[end-start+1]; while(i<=middle && j<=end) { temp[k++] = ars[i]<=ars[j] ? ars[i++]:ars[j++]; } if(i<=middle && j>end) { System.arraycopy(ars, i, ars, start+k, middle-i+1); } System.arraycopy(temp, 0, ars, start, k); } }
相关文章推荐
- 《冒泡,选择,插入,归并,希尔,快速》排序算法java实现一览
- 排序算法Java描述:选择、冒泡、插入、希尔、归并、快速及三向切分快速排序
- 基本的排序算法(选择、插入、冒泡、希尔、归并、堆、快速)
- Java 基础排序算法(冒泡,比较,插入,选择,希尔,堆,归并)
- 排序算法大集合,插入、选择、冒泡、希尔、堆、归并、快速排序,总有一款适合你
- C++排序算法总结(冒泡、插入、选择、希尔、归并、快速、堆)
- 常见内部排序算法 简单数组实现与分析(快速(偶原创partition函数,望众高手指正)、归并、希尔、插入、选择、冒泡)
- 易解排序算法 - 上 (java写:冒泡-插入-选择-希尔-归并、五种排序算法)
- 七大排序算法(冒泡,选择,插入,二分法排序,希尔,快速,合并,堆排序)的java实现
- Java-十种内部排序实现(选择,冒泡,插入,希尔,堆,归并,快速,基数,计数,桶)及代码下载
- 10种排序算法总结(冒泡、选择、插入、希尔、归并、快速、堆、拓扑、锦标赛、基数)
- 10种排序算法总结(冒泡、选择、插入、希尔、归并、快速、堆、拓扑、锦标赛、基数)
- java实现七大排序(插入,冒泡,选择,快速,堆,希尔,归并)
- 七种排序算法总结(冒泡、插入、选择、希尔、归并、堆、快速)
- python(三)6种排序算法性能比较(冒泡、选择、插入、希尔、快速、归并)
- 常用排序算法总结(插入、冒泡、选择、希尔、快速、归并、堆)
- Java语言实现九大排序算法(快速、归并、堆、选择、插入、计数、基数、希尔、冒泡)
- 排序算法——希尔 冒泡 快速 插入 归并 基数
- 【转】排序算法复习(Java实现)(一): 插入,冒泡,选择,Shell,快速排序
- 6种排序算法的简洁实现:冒泡、选择、插入、归并、快速、堆