冒泡排序(Java语言)

视屏讲解地址:【手把手带你写十大排序】1.冒泡排序(Java语言)_哔哩哔哩_bilibili

代码:

java 复制代码
public class BubbleSort {
    public void swap(int[] array, int index1, int index2){
        array[index1] = array[index1] ^ array[index2];
        array[index2] = array[index1] ^ array[index2];
        array[index1] = array[index1] ^ array[index2];
    }
    public void sortFunction(int[] array){
        for (int i = 0; i < array.length - 1; i++) {
            int count = 0;
            for (int j = 0; j < array.length - i - 1; j++) {
                if(array[j] > array[j + 1]){
                    swap(array, j, j + 1);
                    count++;
                }
            }
            if(count == 0){
                break;
            }
        }
    }
}
相关推荐
zero_one_Machel6 分钟前
leetcode73矩阵置零
算法·leetcode·矩阵
【D'accumulation】24 分钟前
典型的MVC设计模式:使用JSP和JavaBean相结合的方式来动态生成网页内容典型的MVC设计模式
java·设计模式·mvc
青椒大仙KI1138 分钟前
24/9/19 算法笔记 kaggle BankChurn数据分类
笔记·算法·分类
试行39 分钟前
Android实现自定义下拉列表绑定数据
android·java
^^为欢几何^^41 分钟前
lodash中_.difference如何过滤数组
javascript·数据结构·算法
豆浩宇42 分钟前
Halcon OCR检测 免训练版
c++·人工智能·opencv·算法·计算机视觉·ocr
茜茜西西CeCe44 分钟前
移动技术开发:简单计算器界面
java·gitee·安卓·android-studio·移动技术开发·原生安卓开发
救救孩子把1 小时前
Java基础之IO流
java·开发语言
小菜yh1 小时前
关于Redis
java·数据库·spring boot·redis·spring·缓存
宇卿.1 小时前
Java键盘输入语句
java·开发语言