0

I implemented a bubble sort solution for pset3-sort. It works when I call the function from a test file I made, but when I print out the values of values in find.c it gets printed out unsorted.

I've been into this now for two days and just can't seem to understand where I'm going wrong as it works fine in the test file I made which calls and passes the exact same values find.c does.

Here's my code section for reference:

void sort(int values[], int n)
{
    int swapped = true;
    while (swapped == true)
    {
        swapped = false;
        for (int i = 0; i < n - 1; i++)
        {
            if (values[i] > values[i+1])
            {
                int temp = values[i+1];
                values[i+1] = values[i];
                values[i] = temp;
                swapped = true;
            }
        }
    }
}

Any help would be much appreciated!

0

Without seeing the data that you're feeding it, it's impossible to say precisely what's happening. It's possible that your test program data set is demonstrating a success, or a partial success, while other data might not.

Having said that, there's actually a problem with your sort. You pass it an array with N elements to be sorted. However, you are sorting N + 1 elements! In other words, you're going past the end of the array. When you do this, you are introducing garbage data into the sort, so the result is unpredictable.

In real terms, let's say that N=5. As the sort progresses to the end of the array, it will end up looking at values[5] and values[6] and comparing them, as well as sorting the values stored there with the other values in the array. The problem is that the array range is from 0 to 4. Big problem.

Also, without seeing how the calling code and data, both in your test program and in find.c, I can't see if there are other problems.

If this answers your question, please click on the check to accept. Let's keep up on forum housekeeping. ;-)

5
  • Hi Thanks for your answer, the + was actually a typo, I copied it across line by line as I can't copy and paste from the CS50 appliance. I'll correct the code. I have since realised that the array being passed in to the function is actually a two dimensional array, even though the task notes on s50 say it is one dimensional. I'll have to go back and relook over everything again. – Liam Winder Sep 26 '15 at 21:11
  • What makes you say that it is a two dimensional array? – Cliff B Sep 26 '15 at 21:20
  • Wow, what a great question! You just made me realise all this time that when the program find.c was asking me for a haystack (number) I was giving it a number such as 9875, then on the next line a number similar such as 9742. I just realised this whole time I should have been entering a single int (number) per line. It is now working! Thanks for helping me question what I was doing! – Liam Winder Sep 26 '15 at 21:36
  • OK, glad I could help. Could you accept the answer to close the question? Otherwise it'll sit here in the unanswered question pool forever. – Cliff B Sep 26 '15 at 21:56
  • Yep no problem, and thanks again. – Liam Winder Sep 26 '15 at 22:00

You must log in to answer this question.

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