Java中几种排序算法

怼烎@ 2023-06-04 06:54 85阅读 0赞

1、冒泡排序算法
通过多次比较(相邻两个数)和交换来实现排序

  1. public class bubble {
  2. public static void bubbleSort(int[] a) {
  3. int temp;
  4. for (int i = 1; i < a.length; i++) {
  5. //将相邻两个数进行比较,较大的数往后冒泡
  6. for (int j = 0; j < a.length - i; j++) {
  7. if (a[j] > a[j + 1]) {
  8. //交换相邻两个数
  9. temp=a[j];
  10. a[j]=a[j+1];
  11. a[j+1]=temp;
  12. }
  13. }
  14. }
  15. }
  16. public static void main(String[] args) {
  17. int[] mao= { 213,123,342,543,12,67,98,320,421,4,15,54,27,34,17};
  18. System.out.print("排序前的数组为:\n"); //输出排序前的数组
  19. for(int i=0;i<mao.length;i++)
  20. {
  21. System.out.print(mao[i]+" ");
  22. }
  23. System.out.print("\n");
  24. bubbleSort(mao); //排序操作
  25. System.out.print("排序后的数组为:\n");
  26. for(int i=0;i<mao.length;i++)
  27. {
  28. System.out.print(mao[i]+" "); //输出排序后的数组
  29. }
  30. System.out.print("\n");
  31. }
  32. }

2、直接插入排序
通过对未排序的数据执行逐个插入至合适的位置而完成排序

  1. public class straight{
  2. public static void straightInsertion(int[] arr) {
  3. int current;//要插入的数
  4. //从1开始第一次一个数不需要排序
  5. for (int i = 1; i < arr.length; i++) {
  6. current = arr[i];
  7. int j = i - 1; //序列元素个数
  8. //从后往前循环,将大于当前插入数的向后移动
  9. while (j >= 0 && arr[j] > current) {
  10. arr[j + 1] = arr[j]; //元素向后移动
  11. j--;
  12. }
  13. arr[j + 1] = current; //找到位置,插入当前元素
  14. }
  15. }
  16. }

3、快速选择排序
通过多次比较和交换来实现排序。首先设定一个分界值,将所有数值与分界值比较,左小右大,比较和交换数据值进而完成排序

  1. public class quick{
  2. static void quickSort(int[] arr,int left,int right) {
  3. int f,t,rtemp,ltemp;
  4. ltemp=left;
  5. rtemp=right;
  6. f=arr[(left+right)/2]; //分界值
  7. while(ltemp<rtemp)
  8. {
  9. while(arr[ltemp]<f)
  10. {
  11. ++ltemp;
  12. }
  13. while(arr[rtemp]>f)
  14. {
  15. --rtemp;
  16. }
  17. if(ltemp<=rtemp)
  18. {
  19. t=arr[ltemp];
  20. arr[ltemp]=arr[rtemp];
  21. arr[rtemp]=t;
  22. rtemp--;
  23. ltemp++;
  24. }
  25. }
  26. if(left<rtemp)
  27. {
  28. quickSort(arr,left,ltemp-1); //递归调用
  29. }
  30. if(ltemp<right)
  31. {
  32. quickSort(arr,rtemp+1,right); //递归调用
  33. }
  34. }
  35. }

4、希尔排序,又称Shell排序或缩小增量排序
(1)将有n个元素的数组分成n/2个数字序列,第1个数据和第n/2+1个数据为一对。
(2)一次循环使每一个序列对排好顺序。
(3)然后,再变为n/4个序列,再次排序。
(4)不断重复上述过程,随着序列减少最后变为一个,也就完成了整个排序。

  1. public class shell{
  2. static void shellSort(int[] a){
  3. int h,temp,x=0;
  4. for(int r=a.length/2;r>=1;r/= 2) //划组排序
  5. {
  6. for(int i=r;i<a.length;i++)
  7. {
  8. temp=a[i];
  9. int j=i-r;
  10. while(j>=0 && temp<a[j])
  11. {
  12. a[j+r]=a[j];
  13. j-=r;
  14. }
  15. a[j+r]=temp;
  16. }
  17. x++;
  18. }
  19. }
  20. }

5、堆排序
构造堆结构、堆排序输出来实现排序

  1. public class pratice {
  2. public static void heapSort(int a[],int n)
  3. {
  4. int i,j,h,k;
  5. int t;
  6. for(i=n/2-1;i>=0;i--) //将a[0,n-1]建成大根堆
  7. {
  8. while(2*i+1<n) //第i个结点有右子树
  9. {
  10. j=2*i+1 ;
  11. if((j+1)<n)
  12. {
  13. if(a[j]<a[j+1]) //右左子树小于右子树,则需要比较右子树
  14. j++; //序号增加1,指向右子树
  15. }
  16. if(a[i]<a[j]) //比较i与j为序号的数据
  17. {
  18. t=a[i]; //交换数据
  19. a[i]=a[j];
  20. a[j]=t;
  21. i=j ; //堆被破坏,需要重新调整
  22. }
  23. else //比较左右子结点均大则堆未破坏,不再需要调整
  24. {
  25. break;
  26. }
  27. }
  28. }
  29. //输出构成的堆
  30. System.out.print("原数据构成的堆:");
  31. for(h=0;h<n;h++)
  32. {
  33. System.out.print(" "+a[h]); //输出
  34. }
  35. System.out.print("\n");
  36. for(i=n-1;i>0;i--)
  37. {
  38. t=a[0]; //与第i个记录交换
  39. a[0] =a[i];
  40. a[i] =t;
  41. k=0;
  42. while(2*k+1<i) //第i个结点有右子树
  43. {
  44. j=2*k+1 ;
  45. if((j+1)<i)
  46. {
  47. if(a[j]<a[j+1]) //右左子树小于右子树,则需要比较右子树
  48. {
  49. j++; //序号增加1,指向右子树
  50. }
  51. }
  52. if(a[k]<a[j]) //比较i与j为序号的数据
  53. {
  54. t=a[k]; //交换数据
  55. a[k]=a[j];
  56. a[j]=t;
  57. k=j ; //堆被破坏,需要重新调整
  58. }
  59. else //比较左右子结点均大则堆未破坏,不再需要调整
  60. {
  61. break;
  62. }
  63. }
  64. }
  65. }
  66. }

6、归并排序
两两合并,进行比较、交换来实现排序

  1. public class marge {
  2. public static void mergeOne(int a[],int b[],int n,int len){ //完成一遍合并的函数
  3. int i,j,k,s,e;
  4. s=0;
  5. while(s+len<n)
  6. {
  7. e=s+2*len-1;
  8. if(e>=n) //最后一段可能少于len个结点
  9. {
  10. e=n-1;
  11. }
  12. //相邻有序段合并
  13. k=s;
  14. i=s;
  15. j=s+len;
  16. while(i<s+len && j<=e) //如果两个有序表都未结束时,循环比较
  17. {
  18. if(a[i]<=a[j]) //如果较小的元素复制到数组b中
  19. {
  20. b[k++]=a[i++];
  21. }
  22. else
  23. {
  24. b[k++]=a[j++];
  25. }
  26. }
  27. while(i<s+len) //未合并的部分复制到数组b中
  28. {
  29. b[k++]=a[i++];
  30. }
  31. while(j<=e)
  32. {
  33. b[k++]=a[j++]; //未合并的部分复制到数组b中
  34. }
  35. s=e+1; //下一对有序段中左段的开始下标
  36. }
  37. if(s<n) //将剩余的一个有序段从数组A中复制到数组b中
  38. {
  39. for(;s<n;s++)
  40. {
  41. b[s]=a[s];
  42. }
  43. }
  44. }
  45. static void mergeSort(int a[],int n) //合并排序
  46. {
  47. int h,count,len,f;
  48. count=0; //排序步骤
  49. len=1; //有序序列的长度
  50. f=0; //变量f作标志
  51. int[] p=new int[n];
  52. while(len<n)
  53. {
  54. if(f==1) //交替在A和P之间合并
  55. {
  56. mergeOne(p,a,n,len); //p合并到a
  57. }
  58. else
  59. {
  60. mergeOne(a,p,n,len); //a合并到p
  61. }
  62. len=len*2; //增加有序序列长度
  63. f=1-f; //使f值在0和1之间切换
  64. count++;
  65. System.out.printf("第"+count+"步排序结果:"); //输出每步排序的结果
  66. for(h=0;h<SIZE;h++)
  67. {
  68. System.out.printf(" "+a[h]); //输出
  69. }
  70. System.out.print("\n");
  71. }
  72. if(f==1) //如果进行了排序
  73. {
  74. for(h=0;h<n;h++) //将内存p中的数据复制回数组a
  75. {
  76. a[h]=p[h];
  77. }
  78. }
  79. }
  80. }

拓展:几种算法的比较
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,85人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java经典排序算法

    对一个排序算法来说,一般从如下3个方面衡量算法的优劣: 时间复杂度:主要是分析关键字的比较次数和记录的移动次数。 空间复杂度:分析排序算法中需要多少辅助内存。 稳定