前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >冒泡排序和快速排序的java实现

冒泡排序和快速排序的java实现

作者头像
sanmutongzi
发布2020-03-04 15:34:05
4490
发布2020-03-04 15:34:05
举报
文章被收录于专栏:stream processstream process

转发请注明原创地址 http://www.cnblogs.com/dongxiao-yang/p/6264831.html

冒泡

代码语言:javascript
复制
    public static int[] bubble_sort(int[] arr) {
        int n = arr.length;
        for (int i = 0; i < n - 1; i++) {//每次循环代表一轮冒泡,一轮冒泡后顶端的数字一定是最大的
            for (int j = 0; j < n - 1 - i; j++) {
                if (arr[j] < (arr[j + 1])) {
                    int tmp = arr[j + 1];
                    arr[j + 1] = arr[j];
                    arr[j] = tmp;
                }

            }
        }

        return arr;

    }

快速排序1

代码语言:javascript
复制
    public static void quick_sort(int[] arr, int start, int end) {

        if (start >= end) {
            // System.out.println("qs finish");
            return;
        }
        int x = arr[start];
        int s = start;
        int e = end;

        while (s < e) {
            if (arr[e] < x) {
                for (; s < e; s++) {
                    if (arr[s] > x) {
                        int tmp = arr[s];
                        arr[s] = arr[e];
                        arr[e] = tmp;
                        // System.out.println("switch " + arr[s] + " " +
                        // arr[e]);
                        // System.out.println("s is " + s + " e is " + e);
                        break;
                    }
                }
            } else {
                e--;

            }
        }
        // System.out.println(" the end s is " + s + " e is " + e +
        // " arr[s] is "
        // + arr[s]+"  arr[start] is "+arr[start]);

        arr[start] = arr[s];

        arr[s] = x;


        System.out.println("quick_sort is "+Arrays.toString(arr));

        quick_sort(arr, start, s - 1);
        quick_sort(arr, s + 1, end);

    }

快速排序2代码优化

代码语言:javascript
复制
    public static void quick_sort2(int[] arr, int start, int end) {

        if (start < end) {

            int x = arr[start];
            int s = start;
            int e = end;

            while (s < e) { //退出循环代表一轮排序结束,首尾游标相遇
                while (s < e && (arr[e] >= x)) // 从右向左找第一个小于x的数
                {
                    e--;
                }

                while (s < e && (arr[s] <= x)) // 从右向左找第一个小于x的数
                {
                    s++;
                }

                if (s < e) {// 左右两个数交换
                    int tmp = arr[s];
                    arr[s] = arr[e];
                    arr[e] = tmp;

//                    System.out.println("switch " + arr[s] + " " + arr[e]);
//                    System.out.println("s is " + s + " e is " + e);
                }

            }

            arr[start] = arr[s]; //基准数归位到数组中正确位置

            arr[s] = x;

            System.out.println("quick_sort2 is "+Arrays.toString(arr));

            quick_sort2(arr, start, s - 1);
            quick_sort2(arr, s + 1, end);

        }

    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-01-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档