Course Module Image

Now, on a blank sheet of paper describe an algorithm to put three words in alphabetical order. For this example, use the three words“RAT,” “BAT,” “CAT,” which in alphabetical order is “BAT,” “CAT,” “RAT.”

 

ANSWER -- SPOILER ALERT!

We will assume that the word RAT is in position 1, BAT is in position 2, CAT is in position 3.

 

1. Look at the words in position 1 and position 2. If the word in position 1 should follow the word in position 2 in alphabetical order, swap the two words.
a. RAT should follow BAT so we swap the two words: result-- BAT, RAT, CAT

2. Look at the words in position 2 and position 3. If the word in position 2 should follow the word in position 3 in alphabetical order, swap the two words.
a. RAT should follow CAT, so we swap the two words: result—BAT, CAT, RAT3.

3. Go back and look again at the words in position 1 and position 2. If the word in position 1 should follow the word in position 2 in alphabetical order, swap the two words.
a. BAT should NOT follow CAT, so we leave everything alone---BAT, CAT, RAT

 

Why do we need step 3? Try the example starting with the words in order RAT, CAT, BAT.