CS100J, Spring 2001
Tues 4/3
Lecture 19
-------------------------------------------------------------------------------
Announcements:
+ ACSU lunch this Fri -- to join ACSU, see www.acsu.cornell.edu
+ P5 extension reminder: due Tues 4/10
+ exercises: solutions, graded, goto Carpenter (fixed website)
+ from 1/27: PL serial # is KYMEZ2UG
+ E9 due Tues 4/3 (will be posted about 11am today)
+ Prelim 2 Stats:  mean    71.58
+ final exam info set
+ section topics -- see web announce
-------------------------------------------------------------------------------
Topics:
+ strings/chars
+ searching/sorting
-------------------------------------------------------------------------------
Summary from Lecture 18
+ row and column major
+ intro to chars
-------------------------------------------------------------------------------
Characters:
+ primitive type (pg 57)
+ stores integer codes for 65000+ characters (UNICODE)
+ deal mostly with ASCII (original 128 characters) (pg 992)
  - characters on keyboard
  - non-printing characters 
    + escape, backspace, bell, new line
    + see \n, \t (pg 88)
+ syntax of type:
  char var = 'integer'
  - default value of char instance variable is NUL character (ASCII 0)
  - note: $''$ is not NUL character -- it's illegal
+ interesting tricks:
   'A' to 'Z' has ASCII range 65 to 90
   'a' to 'z' has ASCII range 97 to 122
   --> 'a'-'A' is 32
   --> 'Q'+'a'-'A' gives 'q'
+ arithmetic
  - character arithmetic give integers!
  - can mix ints and chars
  - can typecast with char to int and vice versa
  example)
  System.out.println('Q'+ 'a'-'A') gives 113
  To fix and get q?
  -->  System.out.println((char) ('Q'+ 'a'-'A')) 
  -->  char tmp = 'Q'+ 'a'-'A';  System.out.println(tmp)
+ see Character class in API
-------------------------------------------------------------------------------
Strings:
+ collections of text
+ objects in Java
+ string literal: "stuff" (saves hassle of calling a constructor)

+ constructors?
  String s1 = new String();        // create empty string
  String s2 = new String("stuff"); // create string of "stuff"
  char[] tmp = {'a','b','c'};
  String s3 = new String(tmp);     // create string from chars

+ so, what is a string literal?
  an instance of class String -- namely, a shortcut from calling a constructor!

+ immutable -- once created, cannot change!
  see StringBuffer class for mutable strings

+ Resemblence of Strings to arrays:
  - index of characters starts count at ZERO
  - find number of characters with $length()$ (not $length$)

+ String methods... (see lecture example)

+ $equals$
  - to compare contents of 2 Strings $s1$ and $s2$: $s1.equals(s2)$
  - why not $==$?
    $==$ tests equality of references, not contents!
    so, $s1==s2$ test if $s1$ and $s2$ refer to the same object
    $==$ will check contents when comparing 2 String literals only

-------------------------------------------------------------------------------
Searching:
+ linear: brute force
  see array_mode.java example
+ bisection: "divide and conquer"
  see Project 1
-------------------------------------------------------------------------------
Sorting:
+ Problem:         Sort {3,9,6,1,2} in ascending order.
+ Human solution: look for lowest, put near front, repeat
+ Automation?
  - lots of strategies
  - selection, bubble, merge, insertion sort....
-------------------------------------------------------------------------------
Insertion sort:
+ high-level:
  - scan unsorted portion of list for unsorted values
  - insert unsorted values into the previously sorted list
  - stop when sorted
+ strategy (for sorting left to right):
  - assume first element is already sorted
  - sort remaining elements:
    for remaining elements
    + obtain current (i) element (key) and its position:
      key <- x[i]
      pos <- i;
    + compare key with previous element (element to the left):
      - if previous element > key
      - shift previous element to key's position
      - test key with next element to the left
      - shift that element if > key
      - repeat until position reaches 1st element on left
    + insert key into appropriate position
    + repeat for next element (i+1)
-------------------------------------------------------------------------------
4 2 1 3

don't touch 1st element:
4

for remaining elements 2 1 3:
- start with position i=1, element 2:
  - compare 2 and 4
  - 4 > 2, so move 4 into 2's position: 4 4 1 3
  - no more elements to test, so 2 replaces the 1st 4: 2 4 1 3
- next: i=2, element 1:
  - compare 1 and 4
  - 4 > 1, so move 4 to 1's position: 2 4 4 3
  - compare 2 and 1
  - 2 > 1, so move 2 to 2nd position (1st 4): 2 2 4 3
  - no more elements to test, so 2 replaces the 1st 4: 1 2 4 3
- next: i=3, element 3:
  - compare 3 and 4
  - 4 > 3, so move 4 to 3's position: 1 2 4 4
  - compare 3 and 2
  - 3 > 2, so put 3 AFTER the 2: 1 2 3 4
- no more elements (i > length), so done
-------------------------------------------------------------------------------