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 &amp;&amp; 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 &gt;= 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 &gt; 0 &amp;&amp; gtCmp(nums[--right], k)) {
                    if (right == lo) {
                        break;
                    }
                }

                while (left < hi &amp;&amp; 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;
        }
    }

原文地址:https://blog.csdn.net/u010523789/article/details/134699309

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_10139.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注