I have to write a selection sort. This is what I came up with
public static void sortString(String[] stringArray) {
int max = DataManager.getArraySizeExcludingNull(stringArray), smallest = 0;
String temp;
for (int i = 0; i < max; i++) {
smallest = i;
for (int j = 0; j < max; j++) {
if (stringArray[j].compareTo(stringArray[smallest]) < 0) {
smallest = j;
}
}
temp = stringArray[i];
stringArray[i] = stringArray[smallest];
stringArray[smallest] = temp;
}
}
I have a list with 45 names (I've checked that max is of size 45 and that each index is a string). The method seems to separate the array in half, in a lexicographically correct order in 2 parts? This is my output.
Kate
Leroy
Nicola
Nancy
Oprah
Peter
Quinton
Richard
Kelly
Sven
Theo
Terry
Violet
Wilma
Zed
Allan
Bob
Steve
Nigel
Neil
Fred
Sally
Glenn
Gary
Heather
Horatio
Ivan
Ingrid
Susy
Lindsay
Mitch
Shelly
David
Kirsten
Sarah
Janet
Barbra
Carrie
Jacob
Elenor
Evan
John
Mike
Josh
Aaron