In Class: Week 10 Tuesday and Thursday:
Sorting


Create a week10 subdirectory in your cs21/class directory by running update21:

$ update21
$ cd
$ cd cs21/class/week10

Topics


In-class work

    Binary Search

  1. Binary search allows us to improve search efficiency on sorted lists. But how to we quantify this improved efficiency? We will analyze the algorithm to see that it has a big-O of lg N.

    Sorting

  2. In groups of 2 or 3, grab 8 playing cards. Give a random set of cards, think of possible algorithms for sorting the list of cards from smallest to lowest. Specifically, describe each comparison that needs to be done and when a swap of two cards takes place
  3. First, we will discuss the algorithm selection sort which, on the first step, searches the entire list for the minimum value. This value is swapped with position 0 in the list. On the next pass, the algorithm repeats the process for the remaining part of the list (position 1 to N-1).
  4. In searchCards_wk10.py, we will implement selection sort to put a deck of cards (i.e. a list of ints) in order from smallest to largest
  5. How many steps does selection sort take in the worst case for a list of size N? How many compares does it it perform? How many swaps?
  6. Next, we will cover bubble sort, describe it's complexity, and implement it in our searchCards_wk10.py program.
  7. Analyzing Bubble vs Selection Sort

  8. How many comparisons does bubble sort do in the worst case? How many swaps? How does this differ/relate to selection sort? Bubble sort executes more swaps in the worst case since selection sort completes at most N swaps. Bubble sort, however, could take less time to run in an almost-sorted list since it knows that 0 swaps on an iteration means the sort is complete

    ASCII encodings for characters

  9. ascii: ascii is a numeric encoding of characters and is how characters are internally represented in the conputer. To see the ascii encodings, type the following at the unix prompt:
      $ man ascii
      
    Use the arrow keys to navigate, and 'q' to quit.

    You should never memorize the ascii encoding, but it is important to know that the encodings for 'a' to 'z' are contiguous, as are 'A' to 'Z' and '0' to '9'. Internallly, python uses the ascii encoding of characters to compare strings. For example, for the following expression:

      "ant" == "any"
      
    Python first comapares the ascii values the first character in each string (97 for 'a' to 97 for 'a'), since they are equal, python then compares the ascii value of the second character in each string (110 for 'n' to 110 for 'n'), since they are equal, python then compares the ascii value of the third character in each string (116 for 't' to 121 for 'y'), since the ascii values are not equal, python evaluates the expression to False.

    The ascii encoding also explains why the following relational expressions evaluate to True:

      "Zoo" < "ant"
      "zoo" > "ant"
      
    because the ascii encoding for 'Z' is less than that for 'a' (90 vs. 97) the string "Zoo" is less than the string "ant", and because the ascii value for 'z' is greater than for 'a' (122 vs. 97) the string "zoo" is "Zoo" < "ant" greater than the string "ant".

    You can use the ord function to get the ascii value for any character, and the chr function to get the character string for any numeric ascii value:

      print ord('a')   # prints 97
      print chr(99)    # prints the string c
    	
  10. Together let's write a program, charvalue.py, that asks the user to enter an int and then print out the ascii character encoded with the int value. So if the user enters the value 66, our program will print out the character B.
  11. Next, lets change the program so that the user enters a string value, and we print out all the characters and their ascii values in the string.