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. Using your worksheet on sample binary search problems, we will go through problem 1 in class. On your own, show the step-by-step process for performing binary search on lists of ints or strings
  2. Sorting

  3. 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
  4. 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).
  5. 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
  6. 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?
  7. Next, we will cover bubble sort, describe it's complexity, and implement it in our searchCards_wk10.py program.
  8. 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
    	
  9. 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.
  10. 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.