Python Lesson 8
Python Lesson 8
Purpose. The purpose of this lesson is to gain more experience with logic in your
programming by using the if-statement to reorder a list of variables alphabetically.
We’ll be using the same problem and code set up in lesson 7 for this lesson, so the
explanation of the sorting procedure below takes advantage of that by using the
same variables to explain and illustrate the sorting procedure.
To reorder a set of variables, you’ll have to “swap” the contents until they are in
order. Here’s how to compare and swap two, so that the name stored in “airline1”
precedes that stored in “airline2”, alphabetically:
The number of times you must “swap” depends on the number of variables you
have to reorder. Only one if-statement is required to sort two names alphabetically.
It would require three if-statements to reorder three names, six if-statements to
reorder four names, and 10 if-statements to reorder five names.
This “recipe” (aka pseudocode) describes the number of swaps (aka calculations)
that must be done to sort the contents of five variables:
1. if the first name is greater than the second name, swap them.
2. if the first name is greater than the third name, swap them.
Programming Exercise 8:
Sorting a List in Alphabetical Order
[names.txt]
3. if the first name is greater than the fourth name, swap them.
4. if the first name is greater than the fifth name, swap them.
1. if the second name is greater than the third name, swap them.
2. if the second name is greater than the fourth name, swap them.
3. if the second name is greater than the fifth name, swap them.
1. if the third name is greater than the fourth name, swap them.
2. if the third name is greater than the fifth name, swap them.
1. if the fourth name is greater than the fifth name, swap them.
This method of reordering, or sorting, a list of items is called a “bubble sort”. In this
sorting algorithm, we make multiple passes through a list to compare adjacent items.
In each pass, two adjacent items are compared (greater than or less than).
If the comparison is True, the adjacent items stay as they were. If the comparison is
False, the values of the two variables are “swapped” so that they are in the right
order.
Through multiple passes through the list, each item “bubbles up” to the location in the
list where it belongs.
For purposes of illustration, here is a list of names of five variables, airline1 – airline5,
and their current contents, obviously not in alphabetical order:
In the first comparison (airline1 > airline2 ), the question is asked, is “United” greater
than “Hawaiian”?
It might help here to recall that in the computer, the uppercase and lowercase letters
of the alphabet are represented by numbers. The capital letters, A – Z are
represented by the numbers (in decimal notation) from 65 – 90. The lowercase letters
are represented by the numbers (in decimal notation) from 97 – 122.
In the computer, this first comparison asks, is the numeric representation of the letter
“U” greater than the numeric representation of the letter “H”? The letter “U” is
represented by the decimal number 85 and the letter “H” is represented by the
decimal number 72.
Since it is True, the indented swap (airline1, airline2 = airline2, airline1) takes
place: the value originally assigned to the variable airline1, United, is assigned to the
variable airline2, and the value originally assigned to the variable airline2, Hawaiian, is
assigned to airline1, and our list now looks like this:
The second comparison (refer to pseudocode above) asks, is airline1 > airline3? It is
True: “Hawaiian” is “greater than” “American,” so the swap (airline1, airline3 =
airline3, airline1) takes place, and now our list looks like this:
The third comparison (refer to pseudocode above) asks, is airline1 > airline4? It is False:
“American” is not “greater than” “Southwest,” so the swap (airline1, airline4 =
airline4, airline1) does not take place, and our list is unchanged.
The fourth comparison (refer to pseudocode above) asks, is airline1 > airline5? It is True:
“American” is “greater than” “Alaska,” so the swap (airline1, airline5 = airline5,
airline1) does take place, and now our list looks like this:
The name in our list that should be first in the alphabetical listing has “bubbled up” to
the top of the list: airline1 has now been assigned the value “Alaska”.
With the first name in the reordered list fixed in place, the second set of comparisons
compare the current values of the remaining four variables (airline2 – airline5).
Referring to the pseudocode above again, we see that three comparisons must be
done to determine which name will “bubble up” to be second in the reordered list as
the value of the variable airline2.
Since this is a short list, you can readily see that the name that should end up second
in the list as the value of airline2 is “American”. When the second set of comparisons is
completed, the list would look like this:
While we can easily see that in this case, the list is now in alphabetical order after the
second set of comparisons has been completed, it is not necessarily so. To ensure the
list is in alphabetical order, the third and fourth sets of comparisons must be
completed as well.
Your Turn. Edit the program you wrote in lesson 7 so that the names of the 5 airlines
are output in alphabetical order with the price quoted by each, and then print the
name of the airline and the price quoted that was the lowest price.
STEP 1. DO THIS:
STEP 2. DO THIS:
Begin the sorting algorithm after your formatting statements in the calculations
code block and before your output code block comment
Use the “swapping” code provided above (on page 1) in the explanation of
sorting names alphabetically as a model.
Use the pseudocode given on pages 1-2 as a guide for coding the if-
statements needed to order the five airline names alphabetically, but read the
hints below first to see what you must do to keep the pairs of airline names and
prices together.
As you write your if-statements for sorting the names, note that you also need to
include code to swap the price quoted by each airline with the airline name. This is
important because we are “swapping” the contents of the variable names in the
sorting process. The way the sorting algorithm works, the variable name airline1 (or
whatever you chose to name it) will hold the airline name that goes first
alphabetically, not necessarily the airline name that was originally input for airline1. To
keep the pairs of airline names and price quotes together, we must swap the
contents of the price variables at the same time that we swap the names of the
airline variables.
For example, using the price and airline variable names I included above, the first six
lines in the sorting algorithm would be:
As you saw in the pseudocode provided, there are ten if-statements required to sort
five names alphabetically. In this exercise, since you must swap the prices along with
the airline names, there will be three lines of code for each if-statement, so that is 30
lines of code overall for the sorting algorithm! Thank goodness for copy and paste,
yes? The number of lines you have in your code will depend on many blank lines you
include to improve readability. I recommend leaving a blank line between each of
the four chunks of swap code in the sorting algorithm.
STEP 3. DO THIS:
When you have completed coding the sorting algorithm, run the program and
test it several times with the same names entered in a different order.
The list of airlines in the output should display in alphabetical order every time.