【问题标题】:When I make the line active in the quicksort5 function, it doesn't sort well. But why?当我在 quicksort5 函数中激活该行时,它排序不好。但为什么?
【发布时间】:2018-08-12 08:20:35
【问题描述】:

最近在学习快速排序,

我编写了 2 个程序:一个成功,而另一个没有。

我试图找出另一个不工作的原因。(我知道原因但我想知道原因下的原因)

两个程序之间的唯一区别是 quicksort5 函数中的一行, 如下:

swap( &list[ ( (backwards-forwards) /2 ) ], &list[last]  );

它们都包含 stdio.h 和 3 个函数,分别称为 main、quicksort5、swap。

程序的上面几行如下:

#include <stdio.h>
int quicksort5(int *, int, int);
int swap(int *, int *);

1) 主要功能如下:

int main()
{
    int arrayofintegers[4096];
    int n=0, quantity=0;

    printf("Please enter how many integer numbers you want to get sorted: ");
    scanf("%d",&quantity);

    if (quantity <= 0)
        return -1;

    printf("\nPlease give at max 10 digits per number.\n\n");
    while ( n<quantity ) //import the numbers
    {
        printf("the     %5d. number = ",n+1);
        scanf("%d",&arrayofintegers[n]);
        n++;
    }
    printf("\n");

    quicksort5(arrayofintegers, 0, quantity-1);

    n=0;
    while ( n<quantity ) //The numbers will be displayed.
    {
        printf("the new %5d. number =%11d\n", n+1, arrayofintegers[n]);
        n++;
    }

    return 0;
}

2) quicksort5函数如下:

int quicksort5(int *list, int forwards, int backwards)
{
    if ( forwards >= backwards )
        return 0;

    int const first   = forwards;
    int const last    = backwards;

    /* //If I make the line bellow active the function doesn't sort successfully. But I want to know the main reason in this.
    swap( &list[ ( (backwards-forwards) /2 ) ], &list[last]  ); */

    int const pivot = list[last];

    int isforwardswaiting  = 0;
    int isbackwardswaiting = 0;


    backwards--; // the pivot won't change
    while (forwards<backwards)
    {
        isforwardswaiting   = (list[forwards]  >= pivot);
        isbackwardswaiting  = (list[backwards] <  pivot);

        if(isforwardswaiting && isbackwardswaiting)
        {
            swap(&list[forwards],&list[backwards]);
            forwards++;
            backwards--;
        }
        else
        {
            if ( !(isforwardswaiting))
                forwards++;

            if ( !(isbackwardswaiting))
                backwards--;
        }
    }

    if (list[forwards] < pivot)
        forwards++;

    swap(&list[forwards],&list[last]); //placing the pivot

    /* list[first], list[first+1] ... list[forwards-2], list[forwards-1] ==> the numbers smaller than the pivot
       list[forwards]                                                    ==> the number which is the pivot
       list[forwards+1], list[forwards+2] ... list[last-1], list[last]   ==> the numbers greater than the pivot */

    quicksort5(list, first, forwards-1);
    quicksort5(list, forwards+1, last);
}

3) 交换函数如下:

int swap(int *a, int *b)
{
    int c=*a;
    *a=*b;
    *b=c;
    return 0;
}

提前感谢您的回答。

【问题讨论】:

  • 请阅读“C 中的保留标识符”。在函数上使用前导下划线只会带来麻烦。也就是说,请同时格式化您的代码。您的缩进不一致且具有误导性,这很可能是您自己没有发现错误的原因。
  • 只是好奇你为什么将这个void _swap(int *,int *); 放在你的_qs3 函数中?
  • 我以为不声明就不能使用了。
  • 我已经优化和澄清了,所以我还在等待你的答案!
  • 检查,我已经发布了一个可以提供帮助的答案。

标签: c algorithm function sorting quicksort


【解决方案1】:

您只需要在您的函数 quicksort5 中修改以下行:

swap( &amp;list[ ( (backwards-forwards) /2 ) ], &amp;list[last] );

到这里:

swap( &amp;list[ (forwards + (backwards-forwards) /2 ) ], &amp;list[last] );

要了解为什么第一条语句会导致问题,请考虑backwards = 6forwards = 4 的情况。 因此,(backwards-forwards) /2 的计算结果为 1,swap 函数将索引 6 处的元素与索引 1 处的元素交换,这是不希望的。

希望对你有帮助!

【讨论】:

  • 非常感谢,它帮了大忙,你刚刚救了我的命。我很感激。我现在真的很开心。
猜你喜欢
  • 1970-01-01
  • 2020-03-02
  • 2018-11-02
  • 1970-01-01
  • 2020-10-19
  • 2019-01-14
  • 1970-01-01
  • 2017-03-23
  • 1970-01-01
相关资源
最近更新 更多