简体   繁体   English

QuickSort Java实施问题

[英]QuickSort Java implementation issue

I'm trying to implement a QuickSort algorithm as a homework at the university, but I just fail to understand where there is a mistake in my code. 我正在尝试将QuickSort算法作为大学的一项家庭作业,但是我只是不明白我的代码中哪里有错误。 I suppose it's a logical mistake, I think I swap my pivot wrongly. 我认为这是一个逻辑错误,我认为我错误地交换了我的数据透视表。 I could really use some help, thank you in advance. 我真的可以使用一些帮助,在此先感谢您。 There is the code: 有代码:

public class QuickSort {
    private int [] array;

    public QuickSort(int [] array){
        this.array = array;
    }

    public void sort(){
        partition(0, array.length - 1);
    }

    public void partition(int start, int end){
        if (end - start < 2){
            return;
        }
        int pivot_index = end;
        int i = start;
        int j = end - 1;
        while (i < j){
            //both elements are not in the right place
            if(array[i] > array[pivot_index] && array[j] <= array[pivot_index]){
                swap(array, i, j);
                i++;
                j--;
            }
            //the element on the left is not in place
            else if (array[i] > array[pivot_index] && array[j] > array[pivot_index]){
                j--;
            }
            //the element on the right is not in place
            else if (array[i] < array[pivot_index] && array[j] < array[pivot_index]){
                i++;
            }
            //both elements are in place
            else {
                i++;
                j--;
            }
        }
        if (array[i] > array[pivot_index]){
            swap(array, pivot_index, i);
            pivot_index = i;
        }
        partition(start, pivot_index - 1);
        partition(pivot_index + 1, end);
    }

    private static void swap(int [] tab, int index1, int index2){
        int temp = tab[index1];
        tab[index1] = tab[index2];
        tab[index2] = temp;
    }
}

Solution one 解决方案一

The idea is iterating through the array to check whether the current element is smaller than the last one (the pivot), if yes swap with the first one that is not (index is lastSmaller + 1). 想法是遍历数组以检查当前元素是否小于最后一个元素(枢轴),如果是,则与不存在的第一个元素交换(索引为lastSmaller + 1)。

private void partition(int start, int end) {
    if (start >= end) {
        return;
    }

    int lastSmallest = start - 1;
    for (int i = start; i < end; i++) {
        if (array[i] < array[end])
            swap(++lastSmallest, i);
    }

    swap(++lastSmallest, end);
    partition(start, lastSmallest - 1);
    partition(lastSmallest + 1, end);
}

Solution two 解决方案二

I think this is what you want to implement. 我认为这是您想要实现的。 Iterate through the array, skip all the elements in good place on the left and right. 遍历数组,跳过所有元素的左右位置。 Swap the two in wrong place. 在错误的地方交换两个。

    private void partition(int start, int end) {
        if (end <= start) {
            return;
        }
        int k = end;
        int i = start;
        int j = end - 1;
        while (i < j) {
            // left is in place
            while (i < j && array[i] < array[k]) {
                i++;
            }
            // right is in place
            while (i < j && array[j] >= array[k]) {
                j--;
            }

            // both are not good
            if (i < j) {
                // swap
                int temp = array[i];
                array[i] = array[j];
                array[j] = temp;

                i++;
                j--;
            }
        }

        // swap left and pivot
        if (array[i] >= array[k]) {
            int temp = array[i];
            array[i] = array[k];
            array[k] = temp;
        }
        partition(start, i - 1);
        partition(i + 1, end);
}

The reason why your solution does not work is that when you find both are not in place, you swap them and continue to partition the rest of the array. 解决方案无法正常工作的原因是,当您发现两者都没有到位时,请交换它们并继续对阵列的其余部分进行分区。 However, you can not guarantee what you have swapped does not violate the rule. 但是,您不能保证交换的内容不违反规则。 Therefore, you need to skip all the elements in place on both side first then swap. 因此,您需要先跳过位于两侧的所有元素,然后再交换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM