首页 > Java > java教程 > 正文

java冒泡排序的几种常见写法

小老鼠
发布: 2024-01-04 16:39:21
原创
1224人浏览过
常见写法:1、基本冒泡排序;2、改进后的冒泡排序:由于每次外层循环都会将最大的数移到正确的位置,所以内层循环的次数可以减少,从而提高效率;3、插入排序与冒泡排序结合:这种写法借鉴了插入排序的思想,通过将已排序的元素逐步向前移动,使未排序的元素逐渐有序。这种方法称为“鸡尾酒排序”。

java冒泡排序的几种常见写法

本教程操作系统:windows10系统、Dell G3电脑。

冒泡排序是一种简单的排序算法,它重复地遍历待排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。遍历数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。

以下是几种常见的冒泡排序的Java实现:

1、基本冒泡排序:

立即学习Java免费学习笔记(深入)”;

java

public static void bubbleSort(int[] arr) {  
    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]) {  
                // swap arr[j+1] and arr[j]  
                int temp = arr[j];  
                arr[j] = arr[j + 1];  
                arr[j + 1] = temp;  
            }  
        }  
    }  
}
登录后复制

2、改进后的冒泡排序:由于每次外层循环都会将最大的数移到正确的位置,所以内层循环的次数可以减少,从而提高效率。

java

public static void bubbleSort(int[] arr) {  
    for (int i = 0; i < arr.length - 1; i++) {  
        boolean swapped = false;  
        for (int j = 0; j < arr.length - 1 - i; j++) {  
            if (arr[j] > arr[j + 1]) {  
                // swap arr[j+1] and arr[j]  
                int temp = arr[j];  
                arr[j] = arr[j + 1];  
                arr[j + 1] = temp;  
                swapped = true;  
            }  
        }  
        // If no two elements were swapped by inner loop, then the array is sorted  
        if (!swapped) break;  
    }  
}
登录后复制

3、插入排序与冒泡排序结合:这种写法借鉴了插入排序的思想,通过将已排序的元素逐步向前移动,使未排序的元素逐渐有序。这种方法称为“鸡尾酒排序”。

java

public static void cocktailSort(int[] arr) {  
    int n = arr.length;  
    boolean swapped; // to flag if any swap has been made in a pass  
    for (int i = 0; i < n - 1; i++) {  
        swapped = false; // assume this pass will do nothing  
        for (int j = 0; j < n - 1 - i; j++) { // start with the second last element and go to the last one  
            if (arr[j] > arr[j + 1]) { // if the current element is greater than the next one, swap them  
                int temp = arr[j]; // swap elements  
                arr[j] = arr[j + 1];  
                arr[j + 1] = temp;  
                swapped = true; // flag to indicate a swap has been made in this pass  
            }  
        }  
        // if no two elements were swapped in this pass, then the array is sorted, so we can stop the sorting process  
        if (!swapped) break; // no swaps means the array is sorted, so we can stop the sorting process. This optimization is not required for correctness, but it can help in practice. If the array is already sorted, the outer loop will keep making passes without making any swaps, so we can stop early. This optimization can be applied to any version of bubble sort
登录后复制

以上就是java冒泡排序的几种常见写法的详细内容,更多请关注php中文网其它相关文章!

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号