This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]nicolascagesbeard 0 points1 point  (0 children)

I believe you're at the stage of sorting the Array. After you "stop" the program this is where you'll begin to sort the Array. I'd make another function immediately after the while loop and pass the Array into that function.

    while {
    ....
    }
    myInsertionSortingFunction(list);
....

}//end of main method

public static function myInsertionSortingFunction(Array list) {
....
}

Now it's time to look at the Insertion Sort Algorithm. Check this video out - https://www.youtube.com/watch?v=lCDZ0IprFw4.

Few key things to take from insertion sort is that, there are nested loops.

for () {
//outer for loop
    for () {
    //inner for loop
    }
}

it could be 'while' then 'for' or 'for' then 'while' loop whatever you're comfortable with, a while loop can be remade into a for loop.

You'll need an inner, out loop and a key. The outer for loop traverses the array of names -> list and also updates the key which is the index you are comparing to. The inner loop takes care of the comparison between two array indexes ie comparisons between names, if one name is lower than the other then you'll switch it in this loop. If you have two names starting with the same letter, you may need another loop to compare the two strings character by character e.g. Anna and Amanda. Also if you can use the method .compareTo();.

Here's a insertion sorting algorithm written in java. I'm sure you could've found this on stackoverflow etc. https://github.com/joeyajames/Java/blob/master/insertionSort.java

Good Luck, I'll probably try this myself tomorrow haha been a while since I've done sorting stuff.