编程语言
首页 > 编程语言> > Java 中的icode9就地合并排序

Java 中的icode9就地合并排序

作者:互联网

普通就地合并排序

时间复杂度: O(N^2)O(N 2)


class PlainInPlaceMergeSort {
    private void swap(int[] nums, int left, int right) {
        int temp = nums[left];
        nums[left] = nums[right];
        nums[right] = temp;
    }

    private void inPlaceMerge(int[] nums, int start, int mid, int end) {
        int leftPointer = start;
        int rightPointer = mid + 1;
        while (leftPointer <= mid && rightPointer <= end) {
            if (nums[leftPointer] <= nums[rightPointer]) {
                leftPointer++;
            } else {
                int currentRightElement = nums[rightPointer];
                int currentRightPointer = rightPointer;
                while (currentRightPointer > leftPointer) {
                    nums[currentRightPointer] = nums[currentRightPointer - 1];
                    currentRightPointer--;
                }
                nums[leftPointer] = currentRightElement;
                leftPointer++;
                rightPointer++;
                mid++;
            }
        }
    }

    private int getMid(int start, int end) {
        return start + (end - start) / 2;
    }

    private void inPlaceMergeSortHelper(int[] nums, int start, int end) {
        if(end - start < 2) {
            return;
        }
        int mid = getMid(start, end);
        inPlaceMergeSortHelper(nums, start, mid);
        inPlaceMergeSortHelper(nums, mid + 1, end);
        inPlaceMerge(nums, start, mid, end);
    }

    public void inPlaceMergeSort(int[] nums) {
        inPlaceMergeSortHelper(nums, 0, nums.length - 1);
    }
}

外壳排序 就地混合合并排序

时间复杂度: O(N*(logN)^2)O(N∗(logN))2


class ShellSortMixedInplaceMergeSort {
    private void swap(int[] nums, int left, int right) {
        int temp = nums[left];
        nums[left] = nums[right];
        nums[right] = temp;
    }

    private void inPlaceMerge(int[] nums, int start, int end) {
        int length = end - start + 1;
        int comparisonRange = getComparisonRange(length);
        while (comparisonRange >= 1) {
            int pointer = start;
            while (pointer + comparisonRange <= end) {
                if (nums[pointer] > nums[pointer + comparisonRange]) {
                    swap(nums, pointer, pointer + comparisonRange);
                }
                pointer++;
            }
            if(comparisonRange == 1) {
                break;
            }
            comparisonRange = getComparisonRange(comparisonRange);
        }
    }

    private int getComparisonRange(int previousLength) {
        return (int) Math.ceil((double) previousLength / 2);
    }

    private int getMid(int start, int end) {
        return start + (end - start) / 2;
    }

    private void inPlaceMergeSortHelper(int[] nums, int start, int end) {
        if (end - start < 1) {
            return;
        }
        int mid = getMid(start, end);
        inPlaceMergeSortHelper(nums, start, mid);
        inPlaceMergeSortHelper(nums, mid + 1, end);
        inPlaceMerge(nums, start, end);
    }

    public void inPlaceMergeSort(int[] nums) {
        inPlaceMergeSortHelper(nums, 0, nums.length - 1);
    }
}

标签:Javascript,函数,实际数组,传递,icode9
来源: