当前位置: 首页 > article >正文

Day60.算法训练

912. 排序数组

  • 归并排序

class Solution {

    public int[] sortArray(int[] nums) {
        int lo = 0;
        int hi = nums.length - 1;
        int[] assist = new int[nums.length];

        sortArray(nums, assist, lo, hi);
        return nums;
    }

    private void sortArray(int[] nums, int[] assist, int lo, int hi) {
        if (lo >= hi) {
            return;
        }

        int mid = lo + (hi - lo) / 2;

        sortArray(nums, assist, lo, mid);

        sortArray(nums, assist, mid + 1, hi);

        merge(nums, assist, lo, mid, hi);
    }

    private void merge(int[] nums, int[] assist, int lo, int mid, int hi) {
        int p1 = lo;
        int p2 = mid + 1;
        int i = lo;
        while (p1 <= mid && p2 <= hi) {
            if (lessCmp(nums[p1], nums[p2])) {
                assist[i++] = nums[p1++];
            } else {
                assist[i++] = nums[p2++];
            }
        }

        while (p1 <= mid) {
            assist[i++] = nums[p1++];
        }
        while (p2 <= hi) {
            assist[i++] = nums[p2++];
        }

        for (int index = lo; index <= hi; index++) {
            nums[index] = assist[index];
        }
    }

    private boolean lessCmp(Comparable a, Comparable b) {
        return a.compareTo(b) < 0;
    }
}

  • 快速排序

class Solution {

        public int[] sortArray(int[] nums) {
            int lo = 0;
            int hi = nums.length - 1;

            sortArray(nums, lo, hi);
            return nums;
        }

        private void sortArray(int[] nums, int lo, int hi) {
            if (lo >= hi) {
                return;
            }

            int partion = partion(nums, lo, hi);

            sortArray(nums, lo, partion - 1);

            sortArray(nums, partion + 1, hi);
        }

        private int partion(int[] nums, int lo, int hi) {
            int k = nums[lo];
            int left = lo;
            int right = hi + 1;
            while (true) {
                while (right > 0 && gtCmp(nums[--right], k)) {
                    if (right == lo) {
                        break;
                    }
                }

                while (left < hi && leCmp(nums[++left], k)) {
                    if (left == hi) {
                        break;
                    }
                }

                if (left == right) {
                    break;
                }
                exch(nums, left, right);
            }
            exch(nums, lo, right);
            return right;
        }

        private void exch(int[] nums, int left, int right) {
            int a = nums[left];
            nums[left] = nums[right];
            nums[right] = a;
        }

        private boolean leCmp(Comparable a, Comparable b) {
            return a.compareTo(b) < 0;
        }

        private boolean gtCmp(Comparable a, Comparable b) {
            return a.compareTo(b) > 0;
        }
    }


http://www.kler.cn/a/150128.html

相关文章:

  • 钉钉向广告低头
  • C++20 STL CookBook2 更强大的编译时 + 安全比较 + spaceship比较符
  • 前端Election
  • HbuildderX运行到手机或模拟器的Android App基座识别不到设备 mac
  • Java 实现接口幂等的九种方法:确保系统稳定性与数据一致性
  • 基于 Canal + Elasticsearch 的业务操作日志解决方案
  • 基于C#实现Prim算法
  • 不同路径 II(力扣LeetCode)动态规划
  • 荒野大镖客提示找不到emp.dll文件的5个修复方法-快速修复dll教程
  • ZYNQ_project:lcd_pic_400x400
  • springboot 返回problem+json
  • 【云备份】第三方库的认识与使用
  • go模版引擎的使用~~
  • 【c语言】二维数组的对角线对称交换
  • LeetCode 60. 排列序列【数学,逆康托展开】困难
  • ⑤【Sorted Set】Redis常用数据类型: ZSet [使用手册]
  • WordPress更改文章分类插件
  • CH01_适应设计模式
  • 网络安全如何自学?
  • 深圳市东星制冷机电受邀莅临2024国际生物发酵展,济南与您相约
  • Spring的依赖注入,依赖注入的基本原则,依赖注入的优势
  • 【Vulnhub 靶场】【Coffee Addicts: 1】【简单-中等】【20210520】
  • 01_原理-事件循环
  • docker (简介、dcoker详细安装步骤、容器常用命令)一站打包- day01
  • [密码学]DES
  • 二维数值型数组例题2