lundi 2 mars 2015

Selection Sort Strings



I read a file with cities and its population and I am trying to sort the cities alphabetically using a selection sort. The issue is that for some reason it sorts it in an odd way. The order of the "sorted" list is "ABDCEFHG..." I tried following it but I can't understand where it messes up. This is the code:



//Selection
for (int i = 0; i < cities.size() - 1; i++)
{
int minPos = i;
for (int j = i + 1; j < cities.size(); j++)
{
if (cities.get(j).compareToIgnoreCase(cities.get(minPos)) < -1)
minPos = j;
}
swap(cities, minPos, i);
}
System.out.print("\nSelection: ");
for(int i = 0; i < cities.size(); i++)
System.out.print(cities.get(i) +"|");

private static void swap(ArrayList <String> a, int i, int j)
{
String temp = a.get(i);
a.set(i, a.get(j));
a.set(j, temp);
}


Any idea what's wrong?


Thanks




Aucun commentaire:

Enregistrer un commentaire