详解冒泡、插入、选择三种排序- 2021-0-12 21:25- 算法


冒泡排序

冒泡排序其实是说算法的执行过程像冒泡泡一样,将数据从左到右或是从右到左的浮动过去。冒泡算法是通过比较数组中两两相临的数值大小,如果是结果是逆向的则交换两值的位置,重复这个过程知道数组最终有序。

代码实现

  1. /**
  2. * 冒泡算法
  3. * 比较数组中相邻两个值的大小,并交换位置,将最小或最大的数分别放置在数组的首位或者末尾
  4. *
  5. * @author Bai
  6. * @date 2021/8/22 10:49
  7. */
  8. public class BubbleSort extends BaseSort {
  9. /**
  10. * 冒泡算法:升序排序
  11. *
  12. * @param arr
  13. */
  14. @Override
  15. void ascSort (int[] arr) {
  16. //边界
  17. if (null == arr || arr.length < 2) {
  18. return;
  19. }
  20. //倒循环:缩小每次循环的范围,去除已经排序完成的位置
  21. for (int e = arr.length; e > 0; e--) {
  22. //正循环: 主要是用于排序
  23. for (int i = 0; i < e - 1; i++) {
  24. //相邻两个值进行比较 ,如果i位置的值大于i+1位置的值,则进行交换
  25. if (arr[i] > arr[i + 1]) {
  26. swap(arr, i, i + 1);
  27. }
  28. }
  29. }
  30. }
  31. /**
  32. * 冒泡算法:正序排序
  33. *
  34. * @param arr
  35. */
  36. @Override
  37. void descSort (int[] arr) {
  38. //边界
  39. if (null == arr || arr.length < 2) {
  40. return;
  41. }
  42. //倒循环:缩小每次循环的范围,去除已经排序完成的位置
  43. for (int e = arr.length; e > 0; e--) {
  44. //正循环: 主要是用于排序
  45. for (int i = 0; i < e - 1; i++) {
  46. //相邻两个值进行比较 ,如果i位置的值大于i+1位置的值,则进行交换
  47. if (arr[i] < arr[i + 1]) {
  48. swap(arr, i, i + 1);
  49. }
  50. }
  51. }
  52. }
  53. }

时间复杂度O(n²)&空间复杂度O(1)

冒泡算法的平均时间复杂度是O(n²),最坏时间复杂度也是O(n²),而最好的时间复杂度是O(n)。冒泡算法需要循环两次来实现这个功能,第一次倒循环:缩小每次循环的范围,去除已经排序完成的位置;第二次正循环:实现相邻下标数据比较,并交换位置。循环一次的时间复杂度为O(n),最坏情况下就是两次循环都要执行一遍,所以冒泡排序最坏的时间复杂度为O(n²)。

最好时间复杂度O(n)

最好时间复杂度O(n)的成立是建立在 数组本身已经是有序的基础上,只需要执行一遍最外层的循环,即可结束。使用一个变量记录内层循环的执行状态,是否进行了swap操作,如果循环一轮下来并未进行交换操作,那么就可以认为这个数组已经是有序的。

  1. /**
  2. * 冒泡算法:算法最优写法
  3. * 算法最好情况下时间复杂度是O(n),也就是数组本身已经是有序的状态下,只需要对数组进行一次循环,也就是O(n)
  4. *
  5. * @param arr
  6. */
  7. public static void bestSort (int[] arr) {
  8. //边界
  9. if (null == arr || arr.length < 2) {
  10. return;
  11. }
  12. //倒循环:缩小每次循环的范围,去除已经排序完成的位置
  13. for (int e = arr.length; e > 0; e--) {
  14. //初始假设这个数组是有序
  15. boolean orderly = true;
  16. //正循环: 主要是用于排序
  17. for (int i = 0; i < e - 1; i++) {
  18. //相邻两个值进行比较 ,如果i位置的值大于i+1位置的值,则进行交换
  19. if (arr[i] > arr[i + 1]) {
  20. swap(arr, i, i + 1);
  21. orderly = false;
  22. }
  23. }
  24. //循环一次后,如果数组数组是有序的,则无须再排序
  25. if (orderly) {
  26. break;
  27. }
  28. }
  29. }

测试

  1. /**
  2. * 算法测试类
  3. * @author Bai
  4. * @date 2021/9/11 14:17
  5. */
  6. public abstract class BaseSort {
  7. private Integer forCnt = 100000;
  8. public BaseSort () {
  9. }
  10. public BaseSort (Integer forCnt) {
  11. this.forCnt = forCnt;
  12. }
  13. /**
  14. * 升序排序
  15. *
  16. * @param arr
  17. */
  18. abstract void ascSort (int[] arr);
  19. /**
  20. * 降序排序
  21. *
  22. * @param arr
  23. */
  24. abstract void descSort (int[] arr);
  25. public void ascForCheck () {
  26. for (int i = 0; i < forCnt; i++) {
  27. //升序
  28. int[] arr = getArr();
  29. int[] arr1 = arr.clone();
  30. ascSort(arr);
  31. ascCheck(arr, arr1);
  32. }
  33. }
  34. public void descForCheck () {
  35. for (int i = 0; i < forCnt; i++) {
  36. //升序
  37. int[] arr = getArr();
  38. int[] arr1 = arr.clone();
  39. descSort(arr);
  40. descCheck(arr, arr1);
  41. }
  42. }
  43. public static void run (BaseSort baseSort) {
  44. baseSort.ascForCheck();
  45. baseSort.descForCheck();
  46. }
  47. /**
  48. * 该方法只使用 两个变量不是同一内存,否则会出现问题
  49. *
  50. * @param arr
  51. * @param i
  52. * @param j
  53. */
  54. public static void swap (int[] arr, int i, int j) {
  55. int tmp = arr[i];
  56. arr[i] = arr[j];
  57. arr[j] = tmp;
  58. }
  59. public static int[] getArr () {
  60. Random random = new Random();
  61. int[] arr = new int[random.nextInt(1000)];
  62. for (int i = 0; i < arr.length; i++) {
  63. arr[i] = random.nextInt(10000);
  64. }
  65. return arr;
  66. }
  67. public static void ascCheck (int[] arr, int[] arr1) {
  68. Arrays.sort(arr1);
  69. check(arr, arr1);
  70. }
  71. public static void descCheck (int[] arr, int[] arr1) {
  72. Integer[] trans = trans(arr1);
  73. Arrays.sort(trans, Collections.reverseOrder());
  74. check(arr, trans(trans));
  75. }
  76. public static void check (int[] arr, int[] arr1) {
  77. for (int i = 0; i < arr.length; i++) {
  78. if (arr[i] != arr1[i]) {
  79. System.out.println("↓----排序失败----↓");
  80. System.out.println(JSONObject.toJSONString(arr));
  81. System.out.println(JSONObject.toJSONString(arr1));
  82. System.out.println("↑----排序失败----↑");
  83. break;
  84. }
  85. }
  86. }
  87. public static Integer[] trans (int[] arr1) {
  88. Integer[] integers = new Integer[arr1.length];
  89. for (int i = 0; i < arr1.length; i++) {
  90. integers[i] = arr1[i];
  91. }
  92. return integers;
  93. }
  94. public static int[] trans (Integer[] arr1) {
  95. int[] integers = new int[arr1.length];
  96. for (int i = 0; i < arr1.length; i++) {
  97. integers[i] = arr1[i];
  98. }
  99. return integers;
  100. }
  101. public static void print (Object o) {
  102. System.out.println(JSONObject.toJSONString(o));
  103. }
  104. }
  1. public static void main (String[] args) {
  2. run(new BubbleSort());
  3. //最好情况下排序,时间复杂度是O(n)
  4. int[] best = getArr();
  5. Arrays.sort(best);
  6. bestSort(best);
  7. }

插入排序

插入排序的基本思想是将一个记录插入到有序表中,其实跟我们打牌时整理纸牌的顺序有点相似,假设第一张纸牌是有序的,将第二张与第一张有序的纸牌进行比较并整理,重复这个过程,将第三张纸牌与前两张进行比较寻找合适的位置插入,以此类推完成排序。

代码实现

  1. package com.learn.test.algorithm;
  2. /**
  3. * 插入算法
  4. * 比较像整理纸牌,是对未排序的数据,在有序队列中从后向前扫描找到合适位置插入。
  5. *
  6. * @author Bai
  7. * @date 2021/8/22 13:53
  8. */
  9. public class InsertionSort extends BaseSort {
  10. /**
  11. * 升序排序
  12. *
  13. * @param arr
  14. */
  15. @Override
  16. void ascSort (int[] arr) {
  17. if (null == arr || arr.length < 2) {
  18. return;
  19. }
  20. for (int i = 1; i < arr.length; i++) {
  21. for (int j = i; j > 0 && arr[j] < arr[j - 1]; j--) {
  22. swap(arr, j, j - 1);
  23. }
  24. }
  25. }
  26. @Override
  27. void descSort (int[] arr) {
  28. if (null == arr || arr.length < 2) {
  29. return;
  30. }
  31. for (int i = 1; i < arr.length; i++) {
  32. for (int j = i - 1; j >= 0 && arr[j] < arr[j + 1]; j--) {
  33. swap(arr, j, j + 1);
  34. }
  35. }
  36. }
  37. public static void main (String[] args) {
  38. run(new InsertionSort());
  39. //优化算法check
  40. for (int i = 0; i < 100000; i++) {
  41. int[] arr = getArr();
  42. int[] clone = arr.clone();
  43. ascSortOp(arr);
  44. ascCheck(arr, clone);
  45. }
  46. }
  47. }

时间复杂度O(n²)&空间复杂度O(1)

插入排序跟冒泡算法一样,也是循环执行两次,它的复杂度也是O(n²),因为没有使用额外的空间,所以空间复杂度也是O(n)。上面这一版的算法其实还是可以进行优化一下的, 现在执行过程中会进行很多次的交换增加了时复杂度,下面优化使用变量来替代swap(),减少时间复杂度。

  1. /**
  2. * 算法优化
  3. * 使用临时变量替代swap操作
  4. *
  5. * @param arr
  6. */
  7. public static void ascSortOp (int[] arr) {
  8. if (null == arr || arr.length < 2) {
  9. return;
  10. }
  11. for (int i = 1; i < arr.length; i++) {
  12. //记录最左边需要交换的下标
  13. int lastMinIndex = i;
  14. for (int j = i - 1; j >= 0 && arr[i] < arr[j]; j--) {
  15. lastMinIndex = j;
  16. }
  17. if (lastMinIndex != i) {
  18. //交换i位置的数据到lastMinIndex位置,将lastMinIndex与i之间的数集体后移一位
  19. int tem = arr[i];
  20. for (int j = i; j > lastMinIndex; j--) {
  21. arr[j] = arr[j - 1];
  22. }
  23. arr[lastMinIndex] = tem;
  24. }
  25. }
  26. }

选择排序

选择排序的思想很简单,就是每次在未排序的序列中找到最大或最小的值,放到序列的起始位置,接着继续在剩下未排序的序列中寻找最大或最小的值放到已排序序列的末尾,重复这个过程,直到完成排序。

代码实现

  1. package com.learn.test.algorithm;
  2. import java.util.Arrays;
  3. /**
  4. * 选择排序
  5. * 每次寻找未排序的数字中最小或最大的数值,放到未排序的数值首位或是末尾
  6. *
  7. * @author Bai
  8. * @date 2021/8/22 13:20
  9. */
  10. public class SelectionSort extends BaseSort {
  11. /**
  12. * 升序
  13. */
  14. @Override
  15. void ascSort (int[] arr) {
  16. if (null == arr || arr.length < 2) {
  17. return;
  18. }
  19. for (int i = 0; i < arr.length - 1; i++) {
  20. //每次循环之后最小值
  21. int minIndex = i;
  22. for (int j = i + 1; j < arr.length; j++) {
  23. minIndex = arr[j] < arr[minIndex] ? j : minIndex;
  24. }
  25. swap(arr, i, minIndex);
  26. }
  27. }
  28. /**
  29. * 降序
  30. */
  31. @Override
  32. void descSort (int[] arr) {
  33. if (null == arr || arr.length < 2) {
  34. return;
  35. }
  36. for (int i = 0; i < arr.length - 1; i++) {
  37. //每次循环之后最大值
  38. int maxIndex = i;
  39. for (int j = i + 1; j < arr.length; j++) {
  40. maxIndex = arr[j] > arr[maxIndex] ? j : maxIndex;
  41. }
  42. swap(arr, i, maxIndex);
  43. }
  44. }
  45. /**
  46. * 升序
  47. */
  48. public static void ascSortV2 (int[] arr) {
  49. if (null == arr || arr.length < 2) {
  50. return;
  51. }
  52. int end = arr.length - 1;
  53. for (int i = 0; i < end; i++) {
  54. //每次循环之后最小值
  55. int minIndex = i;
  56. int minMax = i;
  57. for (int j = i + 1; j < end + 1; j++) {
  58. minIndex = arr[j] < arr[minIndex] ? j : minIndex;
  59. minMax = arr[j] > arr[minMax] ? j : minMax;
  60. }
  61. if (i == minMax && end == minIndex) {
  62. swap(arr, minIndex, minMax);
  63. } else if (i == minMax) {
  64. swap(arr, end, minMax);
  65. if (i != minIndex) {
  66. swap(arr, i, minIndex);
  67. }
  68. } else if (minIndex != minMax) {
  69. if (i != minIndex) {
  70. swap(arr, i, minIndex);
  71. }
  72. if (end != minMax) {
  73. swap(arr, end, minMax);
  74. }
  75. }
  76. end--;
  77. }
  78. }
  79. public static void main (String[] args) {
  80. run(new SelectionSort());
  81. //同时找出 最大 最小 进行选择
  82. v2();
  83. }
  84. private static void v2 () {
  85. for (int i = 0; i < 100000; i++) {
  86. //升序
  87. int[] arr = getArr();
  88. int[] arr1 = arr.clone();
  89. ascSortV2(arr);
  90. Arrays.sort(arr1);
  91. check(arr, arr1);
  92. }
  93. }
  94. }

三种算法的区别

时间复杂度

从时间复杂度上来说三者的平均时间复杂度都是O(n²),最坏的时间复杂度也是O(n²),而冒泡和插入的最好时间复杂度则都是O(n²)

空间复杂度

三者的空间复杂度都是O(1),没有使用额外的空间

稳定度

选择排序是不稳的,冒泡排序和插入排序是稳定的