Alphabetical selection sort of a 2D array
My assignment asks me to store inputted data in a 2D array and use a
selection sort to sort the crimes in alphabetical order. How can I alter
the following selection sort:
//selection sort
for(i = 0; i < criminal.length; i++){
smallest = i;
for(j = i; j < criminal.length; j++){
//compare smallest to current position
if(criminal[j] < criminal[smallest]){
smallest = j;
}
//swap smallest with position in the array
temp = criminal[i];
criminal[i] = criminal[smallest];
age[smallest] = temp;
}
}
//output
for(i = 0; i < criminal.length; i++){
System.out.println(criminal[i]);
}
So that it can accommodate a 2D array that looks like this:
//loop to request to fill array
for (i = 0; i < criminals.length ; i++) {
System.out.print("Enter first and last name of criminal: ");
criminals[i][0] = br.readLine(); //Criminal Name
System.out.print("Enter crime committed: ");
criminals[i][1] = br.readLine(); //Criminal Crime
System.out.print("Enter year of conviction: ");
criminals[i][2] = br.readLine(); //Year of conviction
}
That's it. Any help would be great! I really need to finish this ASAP.
Thanks in advance. :)
No comments:
Post a Comment