当我在quicksort5函数中激活该行时,它不能很好地排序。但为什么呢?

时间:2018-03-04 16:27:02

标签: c algorithm function sorting quicksort

最近我正在研究quicksort,

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

我试图找到为什么另一个不工作。(我知道原因,但我想知道原因的原因)

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;
}

提前感谢您的回答。

1 个答案:

答案 0 :(得分:0)

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

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

到此:

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

要理解第一个语句导致问题的原因,请考虑backwards = 6forwards = 4时的情况。 因此,(backwards-forwards) /2求值为1,交换函数将索引为6的元素与索引为1的元素进行交换,这是不希望的。

希望它有所帮助!