简体   繁体   English

Quicksort不排序长度为2的数组

[英]Quicksort not sorting arrays with length 2

I am trying to implement a part of quicksort where I call a method called splitPoint. 我正在尝试实现quicksort的一部分,我调用一个名为splitPoint的方法。 SplitPoint will use the first index of the array as the pivot value and the pivot will move to the center of the array. SplitPoint将使用数组的第一个索引作为透视值,并且数据透视图将移动到数组的中心。 It will return the index of the new index of the pivot. 它将返回pivot的新索引的索引。 However, If I have an array of length 2 and it is in descending, such as [2, 1], it fails to sort. 但是,如果我有一个长度为2的数组并且它正在降序,例如[2,1],则无法排序。 The method works for everything else though. 该方法适用于其他所有方法。 I think that if this does not work, my quicksort as a whole will not work. 我认为,如果这不起作用,我的快速通道整体将无法正常工作。

 public int splitPoint(int[] a, int first, int last){

    int splitPoint = a[first];
    int low = first + 1;
    int high = last - 1;

    int temp; //holds the temp val for swapping values

    while(low < high){
        while(a[low] <= splitPoint && low != last && high > low){    
            low++;  
            //System.out.println("While loop 1 tracer");
            }
        while(a[high] > splitPoint && high >= first && high >= low){
            high--;
            //System.out.println("While loop 2 tracer");
        }

        if(low <= high){
            temp = a[low];
            a[low] = a[high];
            a[high] = temp;
            low++;
            high++;
        }

        System.out.println(Arrays.toString(a)); // tracer

    }

    a[first] = a[high];
    a[high] = splitPoint;

    return high;
}

Easy answer is to walk through your code. 简单的回答是遍历您的代码。

Presuming your call looks like: 假设你的电话看起来像:

splitPoint({2, 1}, 0, 1); splitPoint({2,1},0,1);

int splitPoint = a[first];  // Will hold a value of 2.
int low = first + 1;   //low = 0 + 1 = 1.
int high = last - 1;  // high = 1 - 1 = 0.

int temp; //holds the temp val for swapping values

while(low < high)  //<== here. low = 1.  high = 0.  low > high, test is false--loop is NOT performed.

a[first] = a[high];  // a[0] = a[0] = 2.
a[high] = splitPoint;  //a[0] = 2

return high;  //returns 0.

So, in short, your problem is in your initialization of low and high. 因此,简而言之,您的问题在于初始化低和高。

If you check, you will see that for an input such as [5, 0, 3] it returns [0, 5, 3]. 如果你检查,你会看到输入如[5,0,3],它返回[0,5,3]。 So there is a problem even for larger arrays. 因此即使对于较大的阵列也存在问题。

A somewhat revised version of the code should work ok: 一个稍微修改的代码版本应该可以正常工作:

static int split(int[] array, final int first, final int last) {
    int low = first;
    int high = last;

    int splitPoint = first;
    while (low < high) {
        while (array[high] > array[low] && high > low) {
            high--;
        }
        while (array[low] <= array[high] && high > low) {
            low++;
        }
        if (low < high) {
            int tmp = array[low];
            array[low] = array[high];
            array[high] = tmp;
            if (low == splitPoint) {
                low++;
                splitPoint = high;
            } else {
                high--;
                splitPoint = low;
            }
        }
    }
    return splitPoint;
}

Example: 例:

public static void main(String[] args) {
    int[] array = new int[]{5, 0, 3};
    System.out.println(split(array, 0, array.length - 1));
    System.out.println(Arrays.toString(array));
}

Output: 输出:

2
[3, 0, 5]

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

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