I’ve written a program which reads a text file (a list of countries), then sorts it in a couple of ways. It works almost flawlessly, except sorting by the name of the country ─ it moves the first element to the last position. I don’t think, however, that my sorting algorithm is wrong (it’s bubblesort), but that the part which reads the file is broken because sometimes (especially with a shorter list of countries) I get two extra lines in the sorted array.
Can anyone see what’s wrong with this code?
Here’s the source:
And the file to sort: countries.txt
On Sun, 23 May 2010 21:46:01 +0000, lordmorgul wrote:
> I don’t think, however, that my sorting algorithm is wrong (it’s
> bubblesort), but that the part which reads the file is broken because
> sometimes (especially with a shorter list of countries) I get two extra
> lines in the sorted array.
Without looking at your code (this looks like a homework assignment to
me), it might be useful if you insert some debug statements in the code -
for example, if you have a loop, insert some code to print something to
the console every time it iterates.
Maybe even the values of some variables.
That way you can see what’s happening when the program runs, and that’ll
point you in a direction for debugging the code.
openSUSE Forums Administrator
+1 to the above, but also start with an easier input file, something like:
in different orders. Then print the resulting array (or whatever you’re using) after each iteration of your sort algorithm as suggested to see what it’s doing.