# Programming Help - Bubble Sort

Discussion in 'C' started by i m gr8, Jun 10, 2007.

1. ### i m gr8New Member

Joined:
Jun 10, 2007
Messages:
4
0
Trophy Points:
0
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?

Joined:
Jul 12, 2004
Messages:
15,375
388
Trophy Points:
83
Don't you think i<(count-1) will not loop through all the elements.

3. ### DaWeiNew Member

Joined:
Dec 6, 2006
Messages:
835
5
Trophy Points:
0
Occupation:
Semi-retired EE
Location:
Texan now in Central NY
http://www.daweidesigns.com
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.

Last edited: Jun 10, 2007
4. ### i m gr8New Member

Joined:
Jun 10, 2007
Messages:
4
0
Trophy Points:
0
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

5. ### i m gr8New Member

Joined:
Jun 10, 2007
Messages:
4
0
Trophy Points:
0
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 ?

6. ### i m gr8New Member

Joined:
Jun 10, 2007
Messages:
4
0
Trophy Points:
0
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.

7. ### DaWeiNew Member

Joined:
Dec 6, 2006
Messages:
835
5
Trophy Points:
0
Occupation:
Semi-retired EE
Location:
Texan now in Central NY
http://www.daweidesigns.com
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++;
}
```