Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Programming Help - Bubble Sort (http://www.go4expert.com/forums/programming-help-bubble-sort-t4617/)

i m gr8 10Jun2007 21:49

Programming Help - Bubble Sort
 
Hello,

I am implementing Bubble Sort and File Writing as an Assignment . I am having a problem . This is the function for the Bubble Sort I have written:

Code:

void BubbleSort()
              {
        ifstream input;
        input.open("input.txt");
        int temp[MAX];
        int count=0;
        while (!input.eof())
                {
            int t;
            input>>t;
            temp[count]=t;
            count++;
            }

        input.close();

        for (int i=0;i<(count-1);i++)
            {
            for (int j=(count-1);j>i;j--)
                    {
              if (temp[j]<temp[j-1])
                      {
                  int tempswap;
                  tempswap=temp[j];
                  temp[j]=temp[j-1];
                  temp[j-1]=tempswap;
                  }
              }
            }

        ofstream bubble;
        bubble.open("bubble.txt", ios::app);

        for (int i=0;i<(count-1);i++)
                {
            bubble<<temp[i]<<endl;
            }

        bubble.close();

        }

The problem is that by running the program suppose I enter in the exact same order:

5
2
7
21
9
1

in the input.txt file , when i call the bubble sort function above.

The output in bubble.txt is formed as:

1
1
2
5
7
9

It is showing an extra 1 and also not showing 21 .

I have been trying to solve it for few hours now cant figure it out . I have also made Selection sort and it is working perfectly fine.

Can anyone help me?

Thanks in advance.

shabbir 10Jun2007 22:04

Re: Programming Help - Bubble Sort
 
Don't you think i<(count-1) will not loop through all the elements.

DaWei 10Jun2007 22:14

Re: Programming Help - Bubble Sort
 
You have a couple of issues. One not pertaining to the sort is that you did not test the input open or the input read to see if they succeeded. That just a newbie mistake. Now that you know about it, I'm sure you won't make it again. Sure, you can luck out, but you are courting failure.

The sort is working. That is because you are saving the 1 twice. It's a perfect example of why conditioning the while on EOF is not enough. The one is followed by a newline, which terminates the read. It does not generate an EOF because the newline terminated the read; thus you did not hit end of file. You go around the loop one more time. You hit EOF. Because you did not test AFTER the read which resulted in EOF and BEFORE you saved t, you just saved t again, which is the old data, the 1.

Test the read for .good (). If it's not good, it could be either a failure or EOF. In that case, test if for .eof(). If it's EOF, you're done. If it's not .good () and not .eof(), you have a failure. Notify the user and bail.

The problem could be fixed by removing the last newline, but that would be stupid. You can't guarantee what your user's file will be like.

Pay attention to the correct count, as mentioned by Shabbir.

i m gr8 11Jun2007 00:48

Re: Programming Help - Bubble Sort
 
Quote:

Originally Posted by shabbir
Don't you think i<(count-1) will not loop through all the elements.

Thanks , I have added that . Yup I guess you are right . But still the problem persist I am trying also to implement what Dawai has said to me in next post.

Thanks again :)

i m gr8 11Jun2007 00:51

Re: Programming Help - Bubble Sort
 
Quote:

Originally Posted by DaWei
You have a couple of issues. One not pertaining to the sort is that you did not test the input open or the input read to see if they succeeded. That just a newbie mistake. Now that you know about it, I'm sure you won't make it again. Sure, you can luck out, but you are courting failure.

The sort is working. That is because you are saving the 1 twice. It's a perfect example of why conditioning the while on EOF is not enough. The one is followed by a newline, which terminates the read. It does not generate an EOF because the newline terminated the read; thus you did not hit end of file. You go around the loop one more time. You hit EOF. Because you did not test AFTER the read which resulted in EOF and BEFORE you saved t, you just saved t again, which is the old data, the 1.

Test the read for .good (). If it's not good, it could be either a failure or EOF. In that case, test if for .eof(). If it's EOF, you're done. If it's not .good () and not .eof(), you have a failure. Notify the user and bail.

The problem could be fixed by removing the last newline, but that would be stupid. You can't guarantee what your user's file will be like.

Pay attention to the correct count, as mentioned by Shabbir.

Hello,

Thanks . I'll make sure next time that I do check if the file has been successfully opens for reading and writing and also successfully closed too . I will read into that on how to do it .

I think I am getting why it is putting an extra time the last input . But havent been able to rectify it.

I tried the following two codes:

Code:

        while (!input.eof())
                {
            int t;
            input>>t;
            temp[count]=t;
            count++;
            if (input.good()==0)
                    {
              if (input.eof()==1)
                      {
                      break;
                  }
              }
            }

and

Code:

        while (!input.eof()&&input.good())
                {
            int t;
            input>>t;
            temp[count]=t;
            count++;
            }

But none is fixing that problem.

Should the checks be implemented in some other way ?

i m gr8 11Jun2007 00:56

Re: Programming Help - Bubble Sort
 
Quote:

Originally Posted by shabbir
Don't you think i<(count-1) will not loop through all the elements.

Thanks it has started showing the higher number by putting i<=count-1 . Thanks alot .

Still trying to rectify that last input showing twice problem though.

DaWei 11Jun2007 01:16

Re: Programming Help - Bubble Sort
 
I specifically told you that it won't work with the EOF test ONLY in the while condiditon. I even explained why that is. You chose to ignore that completely. If you're going to do that in future, you can kiss my *** on the quarterdeck, in front of God and the admiral and everybody. Now listen up.

Code:

    while (!input.eof())
          {
        int t;
        input>>t;
        if (input.good ()) temp[count]=t;
        else
        {
            if (input.eof ()) break;
            else
            {
                cerr << "File input failed";
                return;
            }
        }
        count++;
    }



All times are GMT +5.5. The time now is 19:15.