Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Why it doesn't sort well when I make the line "//_swap(&lst[(last-first)/2],&lst[last]);" active in func. _qs3 ?

0 votes
asked Mar 4, 2018 by Umut
#include <stdio.h>
#define _Arrlim 0x00001000
int main()
{
int cols=0, _list[_Arrlim];
int _qs3(int *, int, int);
printf("Enter the quantity of elements: ");
scanf("%d",&cols);
if( (cols<=0) || (cols>_Arrlim) )
return 1;
printf("\n");
//get the list of given numbers

    {   int u=0, rcols=cols;
        while(u<rcols)
        {
            printf(" element[%5d] = ",u);
            scanf("%d",&_list[u]);
            u++;
        }
    }

printf("\n");
_qs3(_list, 0, cols-1);

//Show the list of sorted numbers
    {   register int u=0, rcols=cols;
        while(u<rcols)
        {
            printf(" element[%5d] = %10d",u,_list[u]);
            u++;
            (u%3) ? printf("\t|") : printf("\n") ;
        }
    }

return 0;
}

int _qs3(int *lst,int fwds,int bwds) // lst means list // fwds means forwards // bwds means backwards
{
    if (fwds>=bwds)
        return 0;//printf("%d_%d|",fwds,bwds);

    int const first=fwds, last=bwds;
    void _swap(int *,int *);
    int iwfe_fwds, iwfe_bwds; // 'iwfe' means 'is waiting for exchange?'
    printf("(l-f)/2= %d\n",(last-first)/2);

    //_swap(&lst[(last-first)/2],&lst[last]);

    int const pivot = lst[ last ];
    printf("pivot =%d\n",pivot);

    while (fwds<bwds)
    {
        iwfe_fwds=(lst[fwds] >= pivot);
        iwfe_bwds=(lst[bwds] < pivot);

        if ( iwfe_fwds && iwfe_bwds)
        {

           _swap(&lst[fwds], &lst[bwds]);
            fwds++;
            bwds--;
        }
        else
        {
            if (!iwfe_fwds)
            fwds++;

            if (!iwfe_bwds)
            bwds--;
        }

    }

if ( lst[fwds] < pivot )
fwds++;

_swap(&lst[fwds],&lst[last]);

_qs3(lst,first,fwds-1);
_qs3(lst,fwds+1,last);
    return 0;
}

void _swap(int *a,int *b)
{
    int c=*a;
    *a=*b;
    *b=c;
}

/*

Please Help me I couldn't understand really why it doesn't work. just one line makes everything dysfunctional

Thank you very much for your efforts

*/

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...