0

I'm very confused! I have spent many (MANY) hours working through this pset. I've gotten to the point where my code passes all check50 requirements for 'find', but it's failing the 'sort' requirements. I'm not even sure how this is possible (wouldn't it have to be sorted for my 'find' algorithm to work??). Here's my sort code... any help would be GREATLY appreciated:

void sort(int values[], int n)
{
//implement a sorting algorithm: bubble
for (int i = 0; i < (n - 1); i ++)//iterate through first time
{
    for (int j = 1; j < n - i - 1; j ++)//iterate through again without last
    {
        if (values[j] > values[j + 1])
        {
            int swap = values[i];
            values[i] = values[i+1];
            values[i+1] = swap;
        }
    }
}
return;
}

Here's what I get from check50:

:( sorts {5,4,3,2,1} expected "2", not "\n"

:( sorts {5,3,1,2,4,6} expected "3", not "\n6\n"

All the rest are smiley faces (explain how 'find' could work if the 'sort' is failing??)

:) finds 28 in {28,29,30}
:) finds 28 in {27,28,29}
:) finds 28 in {26,27,28}
:) finds 28 in {27,28,29,30}
:) finds 28 in {26,27,28,29}
:) finds 28 in {25,26,27,28}
:) doesn't find 28 in {25,26,27}
:) doesn't find 28 in {25,26,27,29}
:) doesn't find 28 in {29,30,31,32}
:) doesn't find 28 in {29, 30, 31}
:) finds 28 in {30,27,28,26}

Thanks so much!!!

0

If you're testing to see if values should be swapped based on the index j, why are you swapping values based on i?

BTW, CS50 assumes no responsibility for bruises due to self-inflicted head slaps. ;-)

2
  • Head... slapped! :-) Although, I switched j for i and it is still not sorting and failing check50. I'm very confused about how the code could pass all 'find' checks if it isn't properly sorting. – Alex Dec 3 '17 at 20:31
  • It would pass the find tests easily if the lists were already sorted and the sort function didn't alter the order. – Cliff B Dec 3 '17 at 22:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .