• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
duolaimi
博客园    首页    新随笔    联系   管理    订阅  订阅

冒泡排序

冒泡排序

思路:首先确定外层循环次数,再确定里面循环次数,里面的循环次数会少一点,因为已经循环出一个最大的或者最小的了,不需要再次循环了。

具体实现代码如下:

public class ArrayTest {
    public static void main(String[] args) {
        int[] ints = {1,34,23,456,55,33};
        int[] sort = sort(ints);
        System.out.println(Arrays.toString(sort));
    }
    public static int[] sort(int[] array){
        int temp;
        for (int i = 0; i < array.length-1; i++) {
            boolean flag = false;
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j]<array[j+1]){
                    temp = array[j];
                    array[j] = array[j+1];
                    array[j+1]=temp;
                    flag = true;
                }
            }
            if(flag==false){
                break;
            }
        }
        return array;
    }
}
posted @ 2020-07-03 12:32  duolaimi  阅读(84)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3