选择排序、插入排序、希尔排序与归并排序

绝地灬酷狼 2022-09-25 13:19 316阅读 0赞

(1)选择排序

  1. public class Sortexample {
  2. public void exch(int[] a,int i,int j){
  3. int temp = a[i];
  4. a[i] = a[j];
  5. a[j] = temp;
  6. }
  7. public void selection(int[] a){
  8. int len = a.length;
  9. for(int i = 0;i < len;i++){
  10. int minindex = i;
  11. for(int j = i + 1;j < len;j++){
  12. if(a[minindex] > a[j])
  13. minindex = j;
  14. }
  15. if(minindex != i){
  16. exch(a,i,minindex);
  17. }
  18. }
  19. }
  20. public static void main(String[] args) {
  21. // TODO Auto-generated method stub
  22. Sortexample example = new Sortexample();
  23. int[] a = new int[]{
  24. 1,3,4,2,5,8,7,6};
  25. example.selection(a);
  26. for(int i = 0;i < a.length; i++)
  27. System.out.println(a[i]);
  28. }
  29. }

运行轨迹:
这里写图片描述

(2)插入排序

  1. public class Sortexample {
  2. public void exch(int[] a,int i,int j){
  3. int temp = a[i];
  4. a[i] = a[j];
  5. a[j] = temp;
  6. }
  7. public void insertsort(int[] a){
  8. int len = a.length;
  9. for(int i = 1; i < len; i++){
  10. for(int j = i;j > 0;j--){
  11. if(a[j] < a[j-1]){
  12. exch(a,j-1,j);
  13. }
  14. }
  15. }
  16. }
  17. public static void main(String[] args) {
  18. // TODO Auto-generated method stub
  19. Sortexample example = new Sortexample();
  20. int[] a = new int[]{
  21. 1,3,4,2,5,8,7,6};
  22. example.insertsort(a);
  23. for(int i = 0;i < a.length; i++)
  24. System.out.println(a[i]);
  25. }
  26. }

运行轨迹如图:
这里写图片描述
插入排序对于部分有序的数组十分高效,也很适合小规模的数组

选择排序与插入排序对比:

这里写图片描述
(3)希尔排序

  1. public class Sortexample {
  2. public void exch(int[] a,int i,int j){
  3. int temp = a[i];
  4. a[i] = a[j];
  5. a[j] = temp;
  6. }
  7. public void shellsort(int[] a){
  8. int len = a.length;
  9. int h = 1;
  10. while(h < len) h = 3*h+1;
  11. while(h >= 1){
  12. for(int i = h;i < len ;i++){
  13. for(int j = i;j >= h;j-=h){
  14. if(a[j]<a[j-h]){
  15. exch(a,j-h,j);
  16. }
  17. }
  18. }
  19. h=h/3;
  20. }
  21. }
  22. public static void main(String[] args) {
  23. // TODO Auto-generated method stub
  24. Sortexample example = new Sortexample();
  25. int[] a = new int[]{
  26. 1,3,4,2,5,8,7,6};
  27. example.shellsort(a);
  28. for(int i = 0;i < a.length; i++)
  29. System.out.println(a[i]);
  30. }
  31. }

运行轨迹:
这里写图片描述
(4)归并排序

自顶向下的归并

  1. public class Sortexample {
  2. private static int[] aux;//用于归并时存储
  3. //将两个有序数组进行归并
  4. public void mergesort(int[] a){
  5. aux = new int[a.length];
  6. sort(a,0,a.length-1);
  7. }
  8. public void merge(int[]a,int lo,int mid,int hi){
  9. int i = lo;
  10. int j = mid + 1;
  11. for(int k = lo;k <= hi;k++)//注意k与hi可以取等
  12. aux[k] = a[k];
  13. for(int k = lo;k <= hi;k++){
  14. if(i > mid)
  15. a[k] = aux[j++];
  16. else if(j > hi)
  17. a[k] =aux[i++];
  18. else if(aux[j] < aux[i])
  19. a[k] = aux[j++];
  20. else
  21. a[k] = aux[i++];
  22. }
  23. }
  24. public void sort(int[] a,int lo,int hi){
  25. if(hi<=lo) return;
  26. int mid = (lo + hi)/2;
  27. sort(a,lo,mid);
  28. sort(a,mid+1,hi);
  29. merge(a,lo,mid,hi);
  30. }
  31. public static void main(String[] args) {
  32. // TODO Auto-generated method stub
  33. Sortexample example = new Sortexample();
  34. int[] a = new int[]{
  35. 1,3,4,2,5,8,7,6};
  36. example.mergesort(a);
  37. System.out.println(Arrays.toString(a));
  38. }
  39. }

这里写图片描述

自底向上的归并排序:

  1. public class MergeBU{
  2. private static int[] aux;//归并所需的辅助数组
  3. public static void sort(int[] a){
  4. int N = a.length;
  5. aux = new int[N];
  6. for (int sz=1;sz<N;sz=sz+sz){
  7. for(int lo=0;lo<N-sz;lo+=sz+sz){
  8. merge(a,lo,lo+sz-1,Math.min(lo+sz+sz-1,N-1));//merge方法与自顶向下一致,都是合并两个有序数组
  9. }
  10. }
  11. }
  12. }

归并排序的优化:用不同的方法处理小规模问题能改进大多数递归算法的性能,因为递归会使小规模问题中方法的调用过于频繁,所以改进对它们的处理方法可以改进整个算法。使用插入排序处理小规模的子数组(比如长度小于15)一般可以将归并排序的运行时间缩短%10-%15.

发表评论

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

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

相关阅读