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

问题描述 投票:0回答:1

最近我正在研究quicksort,

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

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

两个程序之间的唯一区别是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 algorithm function sorting quicksort
1个回答
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的元素交换,这是不希望的。

希望能帮助到你!

© www.soinside.com 2019 - 2024. All rights reserved.