本篇内容主要讲解“各种排序算法的编写教程”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“各种排序算法的编写教程”吧!
西塞山ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!
冒泡排序:
public class SortTest { public static void main(String[] args) { int[] a = {345,7,32,5,4,-1,3,12,23,110,45645,321,456,78,-1,78,78,32,444,345}; show(a); bubbleSort(a); show(a); } private static void bubbleSort(int[] a) { for(int i=0;ia[j+1]){ int tmp = a[j]; a[j] = a[j+1]; a[j+1] = tmp; } } } } private static void show(int[] a) { System.out.println(Arrays.toString(a)); } }
快速排序(无重复值):
public class SortTest { public static void main(String[] args) { int[] a = {345,7,32,5,4,3,12,23,110}; show(a); quickSort(a,0,a.length-1); show(a); } private static void quickSort(int[] a, int start, int end) { if (start>=end) return; int i=start; int j=end; int index = start; while(ia[index]){ j--; } index = swap(a,j,index); while(a[index]>a[i]){ i++; } index = swap(a,i,index); } quickSort(a, start, index-1); quickSort(a, index+1, end); } private static int swap(int[] a, int n, int index) { int tmp = a[n]; a[n] = a[index]; a[index] = tmp; return n; } private static void show(int[] a) { System.out.println(Arrays.toString(a)); } }
快速排序(可含重复值)
public class SortTest { public static void main(String[] args) { int[] a = {345,7,32,5,4,-1,3,12,23,110,45645,321,456,78,-1,78,78,32,345}; show(a); quickSort2(a,0,a.length-1); show(a); } private static void quickSort2(int[] a, int start, int end) { if (start>=end) return; int i=start; int j=end; int index = end; while(ia[index]){ j--; } if (j!=index && a[j]==a[index]){ index = swap(a,--j,index); }else{ index = swap(a,j,index); } while(a[index]>a[i]){ i++; } if (i!=index && a[i]==a[index]){ index = swap(a,++i,index); }else{ index = swap(a,i,index); } } quickSort2(a, start, index-1); quickSort2(a, index+1, end); } private static int swap(int[] a, int n, int index) { int tmp = a[n]; a[n] = a[index]; a[index] = tmp; return n; } private static void show(int[] a) { System.out.println(Arrays.toString(a)); } }
堆排序
public class SortTest { public static void main(String[] args) { int[] a = {345,7,32,5,4,-1,3,12,23,110,45645,321,456,78,-1,78,78,32,444,345}; show(a); heapSort(a); show(a); } private static void heapSort(int[] a) { //建立最大堆 int size = a.length; for(int i=size/2-1;i>=0;i--){ createBigHeap(a,i,size-1); } //排序 for(int j=0;jtmp){ a[start] = a[j]; start = j; j = 2*j+1; }else{ break; } } a[start] = tmp; } private static void show(int[] a) { System.out.println(Arrays.toString(a)); } }
插入排序
public class SortTest { public static void main(String[] args) { int[] a = {345,7,32,5,4,-1,3}; show(a); insertSort(a); show(a); } private static void insertSort(int[] a) { for(int i=0;i=0;j--){ if(tmp 折半插入排序
public class SortTest { public static void main(String[] args) { int[] a = {345,7,7,345,2,2,7,2,7,23,2,345,7,32,5,4,-1,3,7,2,3,2,3,4,2,1,2,4,5,3,345,3,2}; show(a); insertSort2(a); show(a); } private static void insertSort2(int[] a) { for(int i=0;ia[i]) continue; int low = 0; int high = i; int mid = (high+low)/2; while(high>=low){ mid = (high+low)/2; if(tmpa[mid]){ low = mid + 1; } else{ low=mid; break; } } for(int j=n;j>mid;j--){ a[j] = a[j-1]; } a[low] = tmp; } } private static void show(int[] a) { System.out.println(Arrays.toString(a)); } } 希尔排序
public class SortTest { public static void main(String[] args) { int[] a = {345,7,32,5,4,-1,3,2,3,5,7,8,90,1}; show(a); shellSort(a); show(a); } private static void shellSort(int[] a) { shellSort(a,a.length); } private static void shellSort (int[] a, int n){ int i, j, k, temp, gap; int[] gaps = { 1,5,13,43,113,297,815,1989,4711,11969,27901,84801, 213331,543749,1355339,3501671,8810089,21521774, 58548857,157840433,410151271,1131376761,2147483647 }; for (k=0; gaps[k]= 0){ gap = gaps[k]; for (i=gap; i =gap && a[j-gap]>temp){ a[j] = a[j-gap]; j = j-gap; } a[j] = temp; } } } private static void show(int[] a) { System.out.println(Arrays.toString(a)); } } 选择排序
public class SortTest { public static void main(String[] args) { int[] a = {345,7,32,5,4,-1}; show(a); selectSort(a); show(a); } private static void selectSort(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[j]归并排序
public class SortTest { public static void main(String[] args) { int[] a = {345,7,32,5,4,-1,3,2,3,5,7,8,90,1,432,1}; show(a); mergeSort(a); show(a); } private static void mergeSort(int[] a) { //找出中间值 int mid = a.length/2; //申请空间存储中间索引以左的值 int[] left = setValue(a,0,mid); if (left.length>1){//继续拆分左边,直到元素值为1个 mergeSort(left); } //申请空间存储中间索引以右的值 int[] right = setValue(a,mid,a.length); if (right.length>1){//继续拆分右边,直到元素值为1个 mergeSort(right); } //将左右值合并 merge(a,left,right); } private static void merge(int[] a , int[] left, int[] right) { int i=0,j=0,k=0; for(;i到此,相信大家对“各种排序算法的编写教程”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
分享标题:各种排序算法的编写教程
文章链接:http://cdxtjz.com/article/jdecjj.html