Portal Home > Knowledgebase > Articles Database > Programming Help - Bubble Sort


Programming Help - Bubble Sort




Posted by CresHost, 06-10-2007, 07:35 AM
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: 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 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.

Posted by SparkSupport, 06-10-2007, 11:20 AM
i did a quick scan. i think you need to use i<=(count-1) whereever you have i<(count-1). Suppose you have 6 items. After the first while loop, count gets the value 7. Then if you use i<(count-1), i takes values from 0 to 5 only.

Posted by CresHost, 06-10-2007, 12:21 PM
Tried it , Doesnt work properly still. It is showing an extra 1 which was never inputted and also not showing up 21 .

Posted by Codelphious, 06-10-2007, 01:18 PM
ofstream bubble; bubble.open("bubble.txt", ios::app); for (int i=0;i<(count-1);i++) { bubble<



Was this answer helpful?

Add to Favourites Add to Favourites    Print this Article Print this Article

Also Read