• 算法 数据结构 递归冒泡算法 java冒泡算法 优化递归冒泡 数据结构(九)


     使用递归算法实现冒泡:

     

    1. package com.nami.algorithm.study.day06;
    2. import java.util.Arrays;
    3. /**
    4. * beyond u self and trust u self.
    5. *
    6. * @Author: lbc
    7. * @Date: 2023-09-05 15:36
    8. * @email: 594599620@qq.com
    9. * @Description: keep coding
    10. */
    11. public class BubbleSort2 {
    12. // public static void sort(int[] target, int num) {
    13. // if (num == 0) {
    14. // return;
    15. // }
    16. // bubble(target, num-1);
    17. //
    18. // sort(target, num-1);
    19. // }
    20. //
    21. // private static void bubble(int[] target, int j) {
    22. // for (int i = 0; i < j; i++) {
    23. // if (target[i] > target[i + 1]) {
    24. // int temp = target[i];
    25. // target[i] = target[i+1];
    26. // target[i+1] = temp;
    27. // }
    28. // }
    29. // }
    30. public static void sort(int[] target) {
    31. bubble(target, target.length -1 );
    32. }
    33. private static void bubble(int[] target, int j) {
    34. if (j == 0) {
    35. return;
    36. }
    37. for (int i = 0; i < j; i++) {
    38. if (target[i] > target[i + 1]) {
    39. int temp = target[i];
    40. target[i] = target[i+1];
    41. target[i+1] = temp;
    42. }
    43. }
    44. bubble(target, j - 1);
    45. }
    46. public static void main(String[] args) {
    47. int[] test = new int[]{1, 54, 234, 675, 32432, 23, 78, 459, 354, 9, 344, 22, 46, 85, 236, 3278, 245, 83, 154, 2, 1, 34, 73, 23};
    48. int[] test2= new int[] {2,4,7,3,2,1};
    49. // sort(test, test.length);
    50. sort(test2);
    51. System.out.println(Arrays.toString(test2));
    52. }
    53. }

    优化数组稳定得情况, 减少无意义遍历,新增参数x, 标识是否发生了挪动,递归时使用x索引,非常巧妙。递归妙

    1. package com.nami.algorithm.study.day06;
    2. import java.util.Arrays;
    3. /**
    4. * beyond u self and trust u self.
    5. *
    6. * @Author: lbc
    7. * @Date: 2023-09-05 15:36
    8. * @email: 594599620@qq.com
    9. * @Description: keep coding
    10. */
    11. public class BubbleSort {
    12. public static void sort(int[] target) {
    13. bubble(target, target.length -1 );
    14. }
    15. private static void bubble(int[] target, int j) {
    16. if (j == 0) {
    17. return;
    18. }
    19. // 变换标识 索引i
    20. int x = 0;
    21. for (int i = 0; i < j; i++) {
    22. if (target[i] > target[i + 1]) {
    23. int temp = target[i];
    24. target[i] = target[i+1];
    25. target[i+1] = temp;
    26. x = i;
    27. }
    28. }
    29. bubble(target, x);
    30. }
    31. public static void main(String[] args) {
    32. int[] test = new int[]{1, 54, 234, 675, 32432, 23, 78, 459, 354, 9, 344, 22, 46, 85, 236, 3278, 245, 83, 154, 2, 1, 34, 73, 23};
    33. int[] test2= new int[] {2,4,7,3,2,1};
    34. // sort(test, test.length);
    35. sort(test2);
    36. System.out.println(Arrays.toString(test2));
    37. }
    38. }

  • 相关阅读:
    OpenCV 环境变量参考
    云原生微服务 第五章 Spring Cloud Netflix Eureka集成负载均衡组件Ribbon
    文生视频模型Sora刷屏的背后的数据支持
    [go 面试] Go Kit中读取原始HTTP请求体的方法
    Unity-UML类图讲解
    什么是微服务?与分布式又有什么区别?
    rrweb入门
    【Java集合框架】23 ——TreeMap 类
    面试阿里被吊打,158天吃透这“16个”核心技术栈,拿到了25k
    InnoDB常用锁总结(行锁、间隙锁、临键锁、表锁)
  • 原文地址:https://blog.csdn.net/qq_33919114/article/details/132695336