Java Bubble排序示例

冒泡排序是最简单的排序算法,它比较前两个元素,如果第一个大于第二个元素,则将它们交换,然后继续进行(比较和交换)下一对相邻元素。 然后,它再次从前两个元素开始,进行比较,交换,直到不再需要交换为止。

1.解释

#unsorted data -> [99, 88, 55, 77, 1, 66]

#1 iteration.
#1.1 -> [ 99 , 88 , 55, 77, 1, 66] -> [ 88 , 99 , 55, 77, 1, 66]
#1.2 -> [88, 99 , 55 , 77, 1, 66] -> [88, 55 , 99 , 77, 1, 66]
#1.3 -> [88, 55, 99 , 77 , 1, 66] -> [88, 55, 77 , 99 , 1, 66]
#1.4 -> [88, 55, 77, 99 , 1 , 66] -> [88, 55, 77, 1 , 99 , 66]
#1.5 -> [88, 55, 77, 1, 99 , 66 ] -> [88, 55, 77, 1, 66 , 99 ]

#2 iteration.
#2.1 -> [ 88 , 55 , 77, 1, 66, 99] -> [ 55 , 88 , 77, 1, 66, 99]
#2.2 -> [55, 88 , 77 , 1, 66, 99] -> [55, 77 , 88 , 1, 66, 99]
#2.3 -> [55, 77, 88 , 1 , 66, 99] -> [55, 77, 1 , 88 , 66, 99]
#2.4 -> [55, 77, 1, 88 , 66 , 99] -> [55, 77, 1, 66 , 88 , 99]

#3 iteration.
#3.1 -> [ 55 , 77 , 1, 66, 88, 99] -> [55, 77, 1, 66, 88, 99] {no swap}
#3.2 -> [55, 77 , 1 , 66, 88, 99] -> [55, 1 , 77 , 66, 88, 99]
#3.3 -> [55, 1, 77 , 66 , 88, 99] -> [55, 1, 66 , 77 , 88, 99]

#4 iteration.
#4.1 -> [ 55 , 1 , 66, 77, 88, 99] -> [ 1 , 55 , 66, 77, 88, 99]
#4.2 -> [ 1 , 55 , 66, 77, 88, 99] -> [1, 55, 66, 77, 88, 99] {no swap}

#5 iteration.
#5.1 -> [ 1 , 55 , 66, 77, 88, 99] -> is_sorted = true, break;

这是Java气泡排序实现。

public static void sort(int[] input) {

        int inputLength = input.length;
        int temp;
        boolean is_sorted;

        for (int i = 0; i < inputLength; i++) {

            is_sorted = true;

            for (int j = 1; j < (inputLength - i); j++) {

                if (input[j - 1] > input[j]) {
                    temp = input[j - 1];
                    input[j - 1] = input[j];
                    input[j] = temp;
                    is_sorted = false;
                }

            }

            // is sorted? then break it, avoid useless loop.
            if (is_sorted) break;

            System.out.println("\n");
            
        }
        
    }

2. Java Bubble排序示例

一个完整的示例演示了如何使用冒泡排序算法对简单数据集进行排序,支持升序或降序。

BubbleSortExample.java
package com.mkyong;

import java.util.Arrays;
import java.util.stream.Collectors;

public class BubbleSortExample{

    public static void main(String[] args) {

        int[] array = {99, 88, 55, 77, 1, 66};

        System.out.print("unsorted data: ");
        printArray(array);

        System.out.print("ascending order: "); //1,55,66,77,88,99
        bubble_sort(array);

        printArray(array);

        System.out.print("descending order: "); //99,88,77,66,55,1
        bubble_sort(array, false);

        printArray(array);

    }

    private static void bubble_sort(int[] input) {
        bubble_sort(input, true);
    }

    private static void bubble_sort(int[] input, boolean ascending) {

        int inputLength = input.length;
        int temp;
        boolean is_sorted;

        for (int i = 0; i < inputLength; i++) {

            is_sorted = true;

            for (int j = 1; j < (inputLength - i); j++) {

                if (ascending) {
                    if (input[j - 1] > input[j]) {
                        temp = input[j - 1];
                        input[j - 1] = input[j];
                        input[j] = temp;
                        is_sorted = false;
                    }
                } else {
                    if (input[j - 1] < input[j]) {
                        temp = input[j - 1];
                        input[j - 1] = input[j];
                        input[j] = temp;
                        is_sorted = false;
                    }

                }

            }

            // is sorted? then break it, avoid useless loop.
            if (is_sorted) break;

        }

    }

    private static void printArray(int[] data) {
        String result = Arrays.stream(data)
                .mapToObj(String::valueOf)
                .collect(Collectors.joining(","));
        System.out.println(result);
    }

}

输出量

unsorted data: 99,88,55,77,1,66
ascending order: 1,55,66,77,88,99
descending order: 99,88,77,66,55,1

参考文献

  1. 不同种类的排序
  2. 维基百科排序算法

翻译自: https://mkyong.com/java/java-bubble-sort-example/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值