快速排序不排序

发布于 2024-08-22 04:35:29 字数 1371 浏览 3 评论 0原文

所以我试图创建一个快速排序方法,但是,它没有正确排序。这是我的输入和输出
原始数组:
80.0 10.0 50.0 70.0 60.0 90.0 20.0 30.0 40.0 0.0
排序数组:
0.0 30.0 20.0 80.0 40.0 60.0 70.0 10.0 90.0 50.0

我尝试将 for 循环更改为 for(int i = left; i < right; i++)
但现在输出是:
0.0 20.0 30.0 40.0 80.0 10.0 60.0 90.0 70.0 50.0

    public static void sort(double[] a)
    {
        quickSort(a, 0, a.length-1);
    }

    public static void quickSort(double [] a, int left, int right)
    {
        if (left < right)
        {
            int pivotIndex = (left+right)/2;
            int pos = partition(a,left,right,pivotIndex);
            quickSort(a,left,pos-1);
            quickSort(a,pos+1,right);
        }
    }

    private static int partition(double [] a, int left,int right,int pivotIndex)
    {
        double temp = a[pivotIndex];
        a[pivotIndex] = a[right];
        a[right] = temp;
        int pos = left;//represents boundary between small and large elements
        for(int i = left; i < right-1; i++)
        {
            if (a[i] <= a[pivotIndex])
            {
                double temp2 = a[i];
                a[i] = a[pos];
                a[pos] = temp2;
                pos++;
            }
        }
        double temp3 = a[pivotIndex];
        a[pivotIndex] = a[pos];
        a[pos] = temp3;
        return pos;
    }

So I am trying to create a quicksort method, however, it is not sorting properly. Heres my input and output
Original array:
80.0 10.0 50.0 70.0 60.0 90.0 20.0 30.0 40.0 0.0
Sorted array:
0.0 30.0 20.0 80.0 40.0 60.0 70.0 10.0 90.0 50.0

I tried changing the for loop to for(int i = left; i < right; i++)
but now the output is:
0.0 20.0 30.0 40.0 80.0 10.0 60.0 90.0 70.0 50.0

    public static void sort(double[] a)
    {
        quickSort(a, 0, a.length-1);
    }

    public static void quickSort(double [] a, int left, int right)
    {
        if (left < right)
        {
            int pivotIndex = (left+right)/2;
            int pos = partition(a,left,right,pivotIndex);
            quickSort(a,left,pos-1);
            quickSort(a,pos+1,right);
        }
    }

    private static int partition(double [] a, int left,int right,int pivotIndex)
    {
        double temp = a[pivotIndex];
        a[pivotIndex] = a[right];
        a[right] = temp;
        int pos = left;//represents boundary between small and large elements
        for(int i = left; i < right-1; i++)
        {
            if (a[i] <= a[pivotIndex])
            {
                double temp2 = a[i];
                a[i] = a[pos];
                a[pos] = temp2;
                pos++;
            }
        }
        double temp3 = a[pivotIndex];
        a[pivotIndex] = a[pos];
        a[pos] = temp3;
        return pos;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昔日梦未散 2024-08-29 04:35:29

这就是您想要做的:

private static void swap(double[] a, int i, int j) {
    double t = a[i];
    a[i] = a[j];
    a[j] = t;
}

private static int partition(double [] a, int left,int right,int pivotIndex)
{
    swap(a, pivotIndex, right);
    int pos = left;//represents boundary between small and large elements
    for(int i = left; i < right; i++)
    {
        if (a[i] < a[right])
        {
            swap(a, i, pos);
            pos++;
        }
    }
    swap(a, right, pos);
    return pos;
}

我通过使用辅助 swap 方法使代码更加清晰。原始代码中有 3 个错误:

  • 循环边界上的一次性错误
  • 您使用了错误的索引来获取循环中的枢轴元素
  • 您在循环后交换了错误索引处的元素

This is what you want to do:

private static void swap(double[] a, int i, int j) {
    double t = a[i];
    a[i] = a[j];
    a[j] = t;
}

private static int partition(double [] a, int left,int right,int pivotIndex)
{
    swap(a, pivotIndex, right);
    int pos = left;//represents boundary between small and large elements
    for(int i = left; i < right; i++)
    {
        if (a[i] < a[right])
        {
            swap(a, i, pos);
            pos++;
        }
    }
    swap(a, right, pos);
    return pos;
}

I made the code clearer by having a helper swap method. You had 3 bugs in the original code:

  • The one-off error on the loop boundary
  • You're using the wrong index to get the pivot element in the loop
  • You swapped elements at the wrong indices after the loop
愿与i 2024-08-29 04:35:29

更改

for(int i = left; i < right-1; i++)

for(int i = left; i < right; i++)

change

for(int i = left; i < right-1; i++)

to

for(int i = left; i < right; i++)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文