blitzeus
blitzeus

Reputation: 495

c++ bubble sort compact pointers

Im trying to implement a bubble sort with compact pointers. The sort seems to only sort the first two and then fails when trying to use DisplayValues driver supplied by the teacher

  float *Sort(float *first, size_t elements)
{
    {
        for (didSwap = 0, current = first, next = first + 1; current < last; ++current, ++next)
        {
                 didSwap = 1;
            }
        }
        --last;
    }
    while (didSwap);

    return first;
}

Upvotes: 0

Views: 229

Answers (2)

Bok McDonagh
Bok McDonagh

Reputation: 1447

Shouldn't there be a 'do' to make that a 'do{...}while block?

float *Sort(float *first, size_t elements)
{
    do
    {
        for (didSwap = 0, current = first, next = first + 1; current < last; ++current, ++next)
        {
             didSwap = 1;
        }

        --last;
    }
    while (didSwap);
}

Upvotes: 0

john
john

Reputation: 87957

It's pretty simple, the professors routine is expecting the array to be in descending order. Your code looks correct to me but it sorts to ascending order.

BTW your professor has a misunderstanding about how setiosflags works. It should be

cout << setiosflags(ios_base::fixed);

to set the flags on the cout stream. Each stream has it's own flags, your professor seems to be under impression that there is one global set of flags. Same error for resetiosflags.

Upvotes: 1

Related Questions