Java各种排序算法汇总(冒泡,选择,归并,希尔及堆排序等)
程序员文章站
2024-03-07 08:01:38
本文实例汇总了java各种排序算法。分享给大家供大家参考,具体如下:
1. 冒泡排序:
public class sorttest {
public st...
本文实例汇总了java各种排序算法。分享给大家供大家参考,具体如下:
1. 冒泡排序:
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;i<a.length-1;i++){ for(int j=0;j<a.length-i-1;j++){ if(a[j]>a[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)); } }
2. 快速排序(无重复值):
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(i<j){ while(a[j]>a[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)); } }
3. 快速排序(可含重复值)
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(i<j){ while(a[j]>a[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)); } }
4. 堆排序
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;j<size-1;j++){ int tmp=a[0]; a[0]=a[size-1-j]; a[size-1-j]=tmp; createbigheap(a,0,size-2-j); } } private static void createbigheap(int[] a, int start, int end) { int tmp = a[start]; int j = 2*start+1; while(j<=end){ if (j<end && a[j]<a[j+1]){ j++; } if (a[j]>tmp){ 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)); } }
5. 插入排序
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<a.length-1;i++){ int n = i+1; int tmp = a[n]; for(int j=i;j>=0;j--){ if(tmp<a[j]){ a[n] = a[j]; n=j; } } if (a[n]!=tmp) a[n] = tmp; } } private static void show(int[] a) { system.out.println(arrays.tostring(a)); } }
6. 折半插入排序
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;i<a.length-1;i++){ int n = i+1; int tmp = a[n]; if (tmp>a[i]) continue; int low = 0; int high = i; int mid = (high+low)/2; while(high>=low){ mid = (high+low)/2; if(tmp<a[mid]){ high = mid -1; }else if(tmp>a[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)); } }
7. 希尔排序
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]<n; k++); while (--k >= 0){ gap = gaps[k]; for (i=gap; i<n; i++){ temp = a[i]; j = i; while (j>=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)); } }
8. 选择排序
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]<a[min]) min = j; } if (min!=i){ int tmp = a[i]; a[i] = a[min]; a[min] = tmp; } } } private static void show(int[] a) { system.out.println(arrays.tostring(a)); } }
9. 归并排序
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<left.length && j<right.length;){ if (left[i]<right[j]){ a[k++] = left[i++]; }else{ a[k++] = right[j++]; } } for(;i<left.length;i++){ a[k++] = left[i]; } for(;j<right.length;j++){ a[k++] = right[j]; } } private static int[] setvalue(int[] a, int start,int length) { int[] x = new int[length-start]; for (int i = 0; i < x.length; i++) { x[i] = a[start++]; } return x; } private static void show(int[] a) { system.out.println(arrays.tostring(a)); } }
汇总:
public class sortutil { public final static int desc = -1; public final static int asc = 1; /** * 冒泡排序 * @param a sort array * @param sort sortutil.asc,sortutil.desc */ public static void bubblesort(int[] a,int sort) { if (sort==asc) bubblesortasc(a); else bubblesortdesc(a); } public static void bubblesortasc(int[] a) { for(int i=0;i<a.length-1;i++){ for(int j=0;j<a.length-i-1;j++){ if(a[j]>a[j+1]){ int tmp = a[j]; a[j] = a[j+1]; a[j+1] = tmp; } } } } public static void bubblesortdesc(int[] a) { for(int i=0;i<a.length-1;i++){ for(int j=0;j<a.length-i-1;j++){ if(a[j]<a[j+1]){ int tmp = a[j]; a[j] = a[j+1]; a[j+1] = tmp; } } } } // ----------------华-丽-的-功-能-分割-线----------------------- /** * 快速排序(不允许有重复值) * * @param a sort array * @param sort sortutil.asc,sortutil.desc */ public static void quicknorepeatsort(int[] a,int sort) { if (sort==asc) quicknorepeatsortasc(a, 0, a.length-1); else quicknorepeatsortdesc(a, 0, a.length-1); } private static void quicknorepeatsortasc(int[] a, int start, int end) { if (start >= end) return; int i = start; int j = end; int index = start; while (i < j) { while (a[j] > a[index]) { j--; } index = swap(a, j, index); while (a[index] > a[i]) { i++; } index = swap(a, i, index); } quicknorepeatsortasc(a, start, index - 1); quicknorepeatsortasc(a, index + 1, end); } private static void quicknorepeatsortdesc(int[] a, int start, int end) { if (start >= end) return; int i = start; int j = end; int index = start; while (i < j) { while (a[j] < a[index]) { j--; } index = swap(a, j, index); while (a[index] < a[i]) { i++; } index = swap(a, i, index); } quicknorepeatsortdesc(a, start, index - 1); quicknorepeatsortdesc(a, index + 1, end); } /** * 快速排序(允许有重复值) * * @param a sort array * @param sort sortutil.asc,sortutil.desc */ public static void quicksort(int[] a,int sort) { if (sort==asc) quicksortasc(a, 0, a.length-1); else quicksortdesc(a, 0, a.length-1); } private static void quicksortasc(int[] a, int start, int end) { if (start >= end) return; int i = start; int j = end; int index = end; while (i < j) { while (a[j] > a[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); } } quicksortasc(a, start, index - 1); quicksortasc(a, index + 1, end); } private static void quicksortdesc(int[] a, int start, int end) { if (start >= end) return; int i = start; int j = end; int index = end; while (i < j) { while (a[j] < a[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); } } quicksortdesc(a, start, index - 1); quicksortdesc(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; } // ----------------华-丽-的-功-能-分割-线------------------ /** * 堆排序 * * @param a sort array * @param sort sortutil.asc,sortutil.desc */ public static void heapsort(int[] a,int sort){ if (sort==asc) heapsortasc(a); else heapsortdesc(a); } public static void heapsortasc(int[] a) { // 建立最大堆 int size = a.length; for (int i = size / 2 - 1; i >= 0; i--) { createbigheap(a, i, size - 1); } // 排序 for (int j = 0; j < size - 1; j++) { int tmp = a[0]; a[0] = a[size - 1 - j]; a[size - 1 - j] = tmp; createbigheap(a, 0, size - 2 - j); } } private static void createbigheap(int[] a, int start, int end) { int tmp = a[start]; int j = 2 * start + 1; while (j <= end) { if (j < end && a[j] < a[j + 1]) { j++; } if (a[j] > tmp) { a[start] = a[j]; start = j; j = 2 * j + 1; } else { break; } } a[start] = tmp; } public static void heapsortdesc(int[] a) { // 建立最小堆 int size = a.length; for (int i = size / 2 - 1; i >= 0; i--) { createsmallheap(a, i, size - 1); } // 排序 for (int j = 0; j < size - 1; j++) { int tmp = a[0]; a[0] = a[size - 1 - j]; a[size - 1 - j] = tmp; createsmallheap(a, 0, size - 2 - j); } } private static void createsmallheap(int[] a, int start, int end) { int tmp = a[start]; int j = 2 * start + 1; while (j <= end) { if (j < end && a[j] > a[j + 1]) { j++; } if (a[j] < tmp) { a[start] = a[j]; start = j; j = 2 * j + 1; } else { break; } } a[start] = tmp; } // ----------------华-丽-的-功-能-分割-线--------------------- /** * 插入排序 * * @param a sort array * @param sort sortutil.asc,sortutil.desc */ public static void insertsort(int[] a,int sort){ if (sort==asc){ insertsortasc(a); }else{ insertsortdesc(a); } } public static void insertsortasc(int[] a) { for (int i = 0; i < a.length - 1; i++) { int n = i + 1; int tmp = a[n]; for (int j = i; j >= 0; j--) { if (tmp < a[j]) { a[n] = a[j]; n = j; } } if (a[n] != tmp) a[n] = tmp; } } public static void insertsortdesc(int[] a) { for (int i = 0; i < a.length - 1; i++) { int n = i + 1; int tmp = a[n]; for (int j = i; j >= 0; j--) { if (tmp > a[j]) { a[n] = a[j]; n = j; } } if (a[n] != tmp) a[n] = tmp; } } // ----------------华-丽-的-功-能-分割-线-------------------- /** * 折半插入排序 * * @param a sort array * @param sort sortutil.asc,sortutil.desc */ public static void halfinsertsort(int[] a,int sort){ if (sort==asc){ halfinsertsortasc(a); }else{ halfinsertsortdesc(a); } } public static void halfinsertsortasc(int[] a) { for (int i = 0; i < a.length - 1; i++) { int n = i + 1; int tmp = a[n]; if (tmp > a[i]) continue; int low = 0; int high = i; int mid = (high + low) / 2; while (high >= low) { mid = (high + low) / 2; if (tmp < a[mid]) { high = mid - 1; } else if (tmp > a[mid]) { low = mid + 1; } else { low = mid; break; } } for (int j = n; j > mid; j--) { a[j] = a[j - 1]; } a[low] = tmp; } } public static void halfinsertsortdesc(int[] a) { for (int i = 0; i < a.length - 1; i++) { int n = i + 1; int tmp = a[n]; if (tmp < a[i]) continue; int low = 0; int high = i; int mid = (high + low) / 2; while (high >= low) { mid = (high + low) / 2; if (tmp > a[mid]) { high = mid - 1; } else if (tmp < a[mid]) { low = mid + 1; } else { low = mid; break; } } for (int j = n; j > mid; j--) { a[j] = a[j - 1]; } a[low] = tmp; } } // ----------------华-丽-的-功-能-分割-线---------------------- /** * 希尔排序 * * @param a sort array * @param sort sortutil.asc,sortutil.desc */ public static void shellsort(int[] a,int sort){ if (sort==asc){ shellsortasc(a,a.length); }else{ shellsortdesc(a,a.length); } } public static void shellsortasc(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] < n; k++) ; while (--k >= 0) { gap = gaps[k]; for (i = gap; i < n; i++) { temp = a[i]; j = i; while (j >= gap && a[j - gap] > temp) { a[j] = a[j - gap]; j = j - gap; } a[j] = temp; } } } public static void shellsortdesc(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] < n; k++) ; while (--k >= 0) { gap = gaps[k]; for (i = gap; i < n; i++) { temp = a[i]; j = i; while (j >= gap && a[j - gap] < temp) { a[j] = a[j - gap]; j = j - gap; } a[j] = temp; } } } // ----------------华-丽-的-功-能-分割-线--------------------- /** * 选择排序 * * @param a sort array * @param sort sortutil.asc,sortutil.desc */ public static void selectsort(int[] a,int sort){ if (sort==asc){ selectsortasc(a); }else{ selectsortdesc(a); } } public static void selectsortasc(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] < a[min]) min = j; } if (min != i) { int tmp = a[i]; a[i] = a[min]; a[min] = tmp; } } } public static void selectsortdesc(int[] a) { for (int i = 0; i < a.length - 1; i++) { int max = i; for (int j = i + 1; j < a.length; j++) { if (a[j] > a[max]) max = j; } if (max != i) { int tmp = a[i]; a[i] = a[max]; a[max] = tmp; } } } // ----------------华-丽-的-功-能-分割-线--------------------- /** * 归并排序 * * @param a sort array * @param sort sortutil.asc,sortutil.desc */ public static void mergesort(int[] a,int sort){ // 找出中间值 int mid = a.length / 2; // 申请空间存储中间索引以左的值 int[] left = setvalue(a, 0, mid); if (left.length > 1) {// 继续拆分左边,直到元素值为1个 mergesort(left,sort); } // 申请空间存储中间索引以右的值 int[] right = setvalue(a, mid, a.length); if (right.length > 1) {// 继续拆分右边,直到元素值为1个 mergesort(right,sort); } if (sort==asc){ mergeasc(a, left, right); }else{ mergedesc(a, left, right); } } private static void mergeasc(int[] a, int[] left, int[] right) { int i = 0, j = 0, k = 0; for (; i < left.length && j < right.length;) { if (left[i] < right[j]) { a[k++] = left[i++]; } else { a[k++] = right[j++]; } } for (; i < left.length; i++) { a[k++] = left[i]; } for (; j < right.length; j++) { a[k++] = right[j]; } } private static void mergedesc(int[] a, int[] left, int[] right) { int i = 0, j = 0, k = 0; for (; i < left.length && j < right.length;) { if (left[i] > right[j]) { a[k++] = left[i++]; } else { a[k++] = right[j++]; } } for (; i < left.length; i++) { a[k++] = left[i]; } for (; j < right.length; j++) { a[k++] = right[j]; } } private static int[] setvalue(int[] a, int start, int length) { int[] x = new int[length - start]; for (int i = 0; i < x.length; i++) { x[i] = a[start++]; } return x; } }
希望本文所述对大家java程序设计有所帮助。
推荐阅读
-
Java各种排序算法汇总(冒泡,选择,归并,希尔及堆排序等)
-
Java实现八个常用的排序算法:插入排序、冒泡排序、选择排序、希尔排序等
-
Java实现八个常用的排序算法:插入排序、冒泡排序、选择排序、希尔排序等
-
Java——(排序算法)冒泡、选择、插入、快排、归并、希尔、基数
-
排序算法总结(冒泡、选择、插入、希尔、快速、归并、基数、堆排序)
-
2020-11-16 数据结构与算法(5)冒泡、选择、插入、希尔、快速、归并排序及二分法查找
-
(代码+注释+动图+java)排序算法:冒泡,选择,插入,快排,归并,希尔
-
直接插入排序 希尔排序 冒泡排序 快速排序 直接选择排序 堆排序 归并排序 基数排序的算法分析和具体实现
-
【算法之八大排序(带图解、复杂度分析)】 优化版冒泡排序、选择排序、插入排序、希尔排序、快速排序、归并排序、基数排序、堆排序。