Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# CLRS快速排序-工作不正常_C#_Algorithm_Quicksort - Fatal编程技术网

C# CLRS快速排序-工作不正常

C# CLRS快速排序-工作不正常,c#,algorithm,quicksort,C#,Algorithm,Quicksort,我正在尝试实现经典CLRS书中给出的快速排序算法。我在我的C#程序中一行一行地实现了它。但输出是未排序的。以下是我的全部代码以及输出: using System; namespace clrs { class MainClass { public static void Main (string[] args) { int[] numbers = { 2, 1, 4 }; Console.Writ

我正在尝试实现经典CLRS书中给出的快速排序算法。我在我的C#程序中一行一行地实现了它。但输出是未排序的。以下是我的全部代码以及输出:

using System;

namespace clrs
{
    class MainClass
    {

        public static void Main (string[] args)
        {
            int[] numbers = { 2, 1, 4 };
            Console.WriteLine("unsorted: " + string.Join(",", numbers));
            quicksort (numbers, 0, numbers.Length-1);
            Console.WriteLine("sorted: " + string.Join(",", numbers));
            Console.WriteLine("");

            numbers = new int[]{ 7, 2, 1, 6, 1 };
            Console.WriteLine("unsorted: " + string.Join(",", numbers));
            quicksort (numbers, 0, numbers.Length-1);
            Console.WriteLine("sorted: " + string.Join(",", numbers));
            Console.WriteLine("");

            numbers = new int[]{ 2,8,7,1,3,5,6,4 };
            Console.WriteLine("unsorted: " + string.Join(",", numbers));
            quicksort (numbers, 0, numbers.Length-1);
            Console.WriteLine("sorted: " + string.Join(",", numbers));
            Console.WriteLine("");

            numbers = new int[]{ 2, 33, 6, 9, 8, 7, 1, 2, 5, 4, 7 };    
            Console.WriteLine("unsorted: " + string.Join(",", numbers));
            quicksort (numbers, 0, numbers.Length-1);
            Console.WriteLine("sorted: " + string.Join(",", numbers));
            Console.WriteLine("");
        }

        public static void quicksort(int[] a, int p, int r){
            int q;

            if (p < r){
                q = partition (a, p, r);
                quicksort(a, p, q-1);
                quicksort(a, q+1, r);
            }
        }

        public static int partition(int[] a, int p, int r){
            int x = a[r];
            int i = p - 1;

            for (int j=p; j<r-1; j++){
                if(a[j] <= x){
                    i = i + 1;

                    int temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
            int temp1 = a[i+1];
            a[i+1] = a[r];
            a[r] = temp1;

            return (i+1);
        }
    }
}
我已经完全按照CLRS第3版中给出的方式实现了快速排序。我的代码可以编译,但输出没有完全排序

我做错了什么?或者CLRS伪代码中是否存在错误(极不可能)


请帮忙

我称之为恶作剧。。。好像有人打错了

这个


for(int j=p;j我称之为骗局……好像有人打错了

这个


用于(int j=p;点动输出,你能发现输出的共同点吗?在你的Lomuto分区中,哪些是比较中使用的索引?@greybeard-谢谢你的快速回复。我一直在尝试调试这个,但由于它是一个精确的伪代码实现,它不应该“开箱即用”吗?观察输出,你能发现一些吗输出的共同点是什么?在你的Lomuto分区中,哪些是比较中使用的索引?@greybeard-谢谢你的快速回复。我一直在尝试调试这个,但由于它是一个精确的伪代码实现,它不应该“开箱即用”吗?非常感谢!在过去的两天里我一直在碰壁t无法使其工作。一旦我进行了此修改,那么数组将被正确排序。这是否意味着CLRS快速排序伪代码中存在错误?@SuyashGupta更新以向您说明错误原因,这是一个非常简单的错误。我查看了CLRS第三版的勘误表。但此“错误”未在此处列出。因此,这是一个已确认的错误/键入错误吗?@SuyashGupta我不确定…有人错了,但非常感谢!!!!在过去的两天里,我一直把头撞在墙上,但无法让它工作。一旦我做了这个修改,数组就被正确排序了。这是否意味着CLRS快速排序伪代码中有一个bug?@SuyashGupta更新,向您展示了为什么它错了,这是一个非常复杂的问题简单的错误我查看了CLRS第三版的勘误表。但是这里没有列出这个“错误”。那么这是一个确认的错误/打字错误吗?@SuyashGupta我不确定……不过有人错了
unsorted: 2,1,4
sorted: 2,4,1

unsorted: 7,2,1,6,1
sorted: 1,1,2,7,6

unsorted: 2,8,7,1,3,5,6,4
sorted: 2,3,1,4,5,7,8,6

unsorted: 2,33,6,9,8,7,1,2,5,4,7
sorted: 1,2,5,6,7,2,7,8,9,33,4
for (int j=p; j<r-1; j++){
for (int j=p; j<r; j++){
algorithm quicksort(A, lo, hi) is
    if lo < hi then
        p := partition(A, lo, hi)
        quicksort(A, lo, p - 1 )
        quicksort(A, p + 1, hi)

algorithm partition(A, lo, hi) is
    pivot := A[hi]
    i := lo - 1    
    for j := lo to hi - 1 do
        if A[j] < pivot then
            i := i + 1
            swap A[i] with A[j]
    swap A[i + 1] with A[hi]
    return i + 1
for j := lo to hi - 1 do   // note the - 1
    if A[j] < pivot then   // and <