I was having trouble following the selected sorting algorithm for sorting strings in an array alphabetically. My code as it is, is a complete mess.
public class sorting {
public void getArray(String [] a) {
int min = 0;
int minIndex = 0;
for (int j = 0; j < a.length; j++ ) {
for (int i = j; i < a.length; i++) {
if (i == j) {
a[min] = a[j];
}
if (a[min].compareTo(a[i]) < 0) { // if current element is < lowest, assign new lowest.
a[min] = a[i];
minIndex = i;
} // end of if
} // end of INSIDE for
a[minIndex] = a[j]; // place first element at the location of the smallest element.
a[j] = a[min]; // place the smallest element value in the first spot.
} // end of OUTSIDE for
}
Can anyone throughly explain to me their thought process in going on about this? For instance, what does the inner for loop do vs the outside? Many thanks in advance!