博客
关于我
【算法】排序-冒泡排序 (java实现)
阅读量:541 次
发布时间:2019-03-09

本文共 866 字,大约阅读时间需要 2 分钟。

package com.billkang.algorithm.sort;import java.util.Arrays;/** * 冒泡排序 *  * @author Kangbin  * @date 2018-11-29 */public class BubbleSort {    public void bubbleSort(int[] arr) {        // 冒泡排序的基本思想是每次通过比较相邻元素并交换,逐步将最大的元素"冒"到数组的最后        for (int i = 1; i < arr.length; i++) {            // 每次遍历数组的长度减少一个元素,因为最大的元素已经排到了最后            for (int j = 0; j < arr.length - i; j++) {                // 比较当前元素与下一个元素                if (arr[j] > arr[j + 1]) {                    // 交换两个元素的位置                    int temp = arr[j];                    arr[j] = arr[j + 1];                    arr[j + 1] = temp;                }            }        }    }    public static void main(String[] args) {        int[] arr = {9, 8, 7, 6, 5, 4, 3, 2, 1};        // 开始执行冒泡排序        new BubbleSort().bubbleSort(arr);        // 输出排序后的数组        System.out.println(Arrays.toString(arr));    }}

转载地址:http://dfeiz.baihongyu.com/

你可能感兴趣的文章
python:Could not find platform independent libraries <prefix>解决方法
查看>>
Python:datetime tzinfo 时区名称文档
查看>>
Python:deepcopy(list) vs new_list = old_list[:]
查看>>
Python:Google API - 从消息中获取 mimeTypes
查看>>
Python:Google API - 从消息中获取 mimeTypes
查看>>
Python:subprocess.call,stdout to file,stderr to file,在屏幕上实时显示stderr
查看>>
Python:TypeError:无法连接“str“和“int“对象
查看>>
Python:XPath 在 ElementTree 中不可用
查看>>
Python:从 FTP 服务器下载文件
查看>>
python:从列表中创建元组列表
查看>>
Python:从嵌套列表中删除重复项
查看>>
Python:从间隔映射到值
查看>>
Python:使用 shell=False 的子进程调用不起作用
查看>>
Python:修改 PATH 环境变量的平台无关方式
查看>>
Python:假与 0
查看>>
Python:函数使用
查看>>
Python:函数进阶
查看>>
Python:在整数的二进制表示中查找最长的二进制间隙
查看>>
Python:如何从二进制转换为 base 64 并返回?
查看>>
Python:如何以非阻塞方式读取子进程的标准输出
查看>>