博客
关于我
【算法】排序-冒泡排序 (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/

你可能感兴趣的文章
php验证邮箱是否有效
查看>>
PHP高性能分布式应用服务器框架-SwooleDistributed
查看>>
PHP高效、轻量级表格数据处理库 OpenSpout
查看>>
R 数据缺失的处理
查看>>
php,nginx重启
查看>>
php:$_ENV 和 getenv区别
查看>>
PHP:cURL error 60: SSL certificate unable to get local issuer certificate
查看>>
PHP:PDOStatement::bindValue参数类型php5和php7问题
查看>>
Q媒体播放器.如何播放具有多个音频的视频?
查看>>
pickle
查看>>
Pickle thread.lock(Pymongo)
查看>>
pickle模块
查看>>
qYKVEtqdDg
查看>>
pid控制
查看>>
PID控制介绍-ChatGPT4o作答
查看>>
PID控制器数字化
查看>>
Qwen-VL项目使用指南
查看>>
PIESDKDoNet二次开发配置注意事项
查看>>
PIGS POJ 1149 网络流
查看>>
PIL Image对图像进行点乘,加上常数(等像素操作)
查看>>