冒泡排序(java)

本文介绍了一种简单的排序算法——冒泡排序。通过不断比较并交换相邻元素的位置,将较大的元素逐步移动到序列的尾部。文章还提出了一种优化方案:当一次完整的遍历过程中没有发生元素交换时,则认为序列已经有序,可以提前结束排序过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

冒泡排序

按照排序序列从前向后,依次比较相邻元素的值,若发现逆序则交换,使较大的元素移动到序列的尾部。

优化:如果发现某次排序没有进行过交换,则证明当前为排序完成的序列,设置一个flag来判断元素是否进行过交换,从而减少不必要的比较。

一般来说,会进行

  1. n-1次大的循环
  2. 每一趟排序的次数从n-1次开始减小
  3. 发现某次循环中序列的顺序没有发生变化,则直接退出程序

代码实现:

package com.sortAlgorithm;

public class bubbleSort {
    public static void main(String[] args){
        int list[] = {3,9,11,10,4};

        //打印之前的列表
        for(int i = 0; i<list.length; i++) {
            System.out.print(list[i]+"  ");
        }
        System.out.println();

        //bubble
        bubbleSort bubbleSort = new bubbleSort();
        bubbleSort.bubble(list);

        //打印之后的列表
        for(int i = 0; i<list.length; i++) {
            System.out.print(list[i]+"  ");
        }
        System.out.println();
    }

    public void bubble(int list[]){
        int count = 0;
        int size = list.length;
        for (int i = 0; i<size-1; i++) {
            for (int j = 0; j < size - 1; j++) {
                if (list[j] > list[j + 1]) {
                    count++;
                    int temp;
                    temp = list[j + 1];
                    list[j + 1] = list[j];
                    list[j] = temp;
                }
            }
            if (count == 0) {
                System.out.println("提前结束了");
                return;
            } else {
                count = 0;
            }
        }
    }
}
### Java实现冒泡排序的方法及代码示例 以下是Java中实现冒泡排序的完整代码示例,展示了如何通过嵌套循环对数组进行排序: ```java public class BubbleSort { public static void bubbleSort(int[] array) { int n = array.length; for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (array[j] > array[j + 1]) { // 交换相邻元素的位置 int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } } public static void main(String[] args) { int[] array = {64, 34, 25, 12, 22, 11, 90}; bubbleSort(array); System.out.println("排序后的数组:"); for (int i : array) { System.out.print(i + " "); } } } ``` 上述代码定义了一个`bubbleSort`方法来实现冒泡排序算法。外部循环控制排序的轮数,内部循环负责比较和交换相邻的元素[^1]。如果当前元素大于下一个元素,则交换它们的位置。在`main`方法中,创建一个整数数组并调用`bubbleSort`方法对其进行排序,最后打印排序后的数组。 此外,还有一种稍有不同的实现方式,如下所示: ```java public class BubbleSortAlternative { public static void bubbleSort2(int[] a, int n) { for (int i = 0; i < n; i++) { for (int j = 1; j < n - i; j++) { if (a[j - 1] > a[j]) { // 交换 a[j-1] 和 a[j] int temp = a[j - 1]; a[j - 1] = a[j]; a[j] = temp; } } } } public static void main(String[] args) { int[] array = {66, 69, 0, 24, 12}; bubbleSort2(array, array.length); System.out.println("排序后的数组:"); for (int value : array) { System.out.print(value + " "); } } } ``` 此版本的冒泡排序通过从第二个元素开始比较,减少了部分冗余操作[^2]。 还有一种更详细的实现方式,能够展示每一轮排序后的结果: ```java public class BubbleSortDetailed { public static void main(String[] args) { int[] arr = {66, 69, 0, 24, 12}; int temp = 0; for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - 1 - i; j++) { if (arr[j] > arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } System.out.println("\n==第" + (i + 1) + "轮=="); for (int value : arr) { System.out.print(value + "\t"); } } } } ``` 该代码不仅实现了排序功能,还输出了每一轮排序后的数组状态,便于观察算法的工作过程[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值