10a: Applets; Searching & Sorting
ICS211, Fall 2012
Dr. Zach
(switch view)
Status check
- Exam 2... still grading. :(
- A07 due... but grader not ready. 2 day extension.
- A08 (GUIs) and A09 (sorting) up soon though, with some overlap.
Last time
- A GUI number pad
- Runs as an application: main method that produces a JFrame.
- Now, how to run as an applet?
Applets
- Java's "killer app": could safely run untrusted code in your browser
- Other comparable tech not as safe (ActiveX)
- tho Java sandbox not foolproof either though...
- Not as popular now
- Flash, dynamic HTML (JavaScript + CSS), AJAX
- But still useful for you: a way to run a program online
Applets extend JApplet
public class NumberPad extends JApplet implements ActionListener {
Test it locally
- Lots that can go wrong getting an applet up and running, so take it a step at a time!
- Can use appletviewer (which comes with the JDK)
appletviewer numpad.html
- Note that you have to give it a valid HTML page, not just the class
- Can also load numpad.html in a browser
When it doesn't work
- In browser: When it doesn't load, right-click the applet (or the coffee cup Java icon in system tray) and go to Open Console. (Not: Open Control Panel)
- If there is no such icon, go to Start -> Control Panel, and find Java. Go to the Advanced tab and set
- Console will show you stack trace of crashes and any System.out.println content
- Usual problems: path trouble, missing files, wrong Java version, etc.
- If compiled it with Java 7 JDK, you'll need a Java 7 JRE to view the applet
- If using a browser, sometimes broken applet gets cached (even when you hit refresh).
- Close ALL open browser windows and try again.
Upload
- Only once it works locally... Use SFTP to upload to server (uhunix.hawaii.edu)
- Use either
chmod
command or SFTP options to set permissions correctly.
- Make sure all files are world-readable.
-rw-r--r-- 1 ztomasze uh 2401 Oct 14 22:54 NumberPad.class
-rw-r--r-- 1 ztomasze uh 176 Oct 14 22:57 numpad.html
- Directories need to be world-executable. (.class files don't tho.)
drwxr-xr-x 2 ztomasze uh 512 Oct 14 23:05 ./
drwxr-xr-x 5 ztomasze uh 512 Oct 14 23:05 ../
- I'll ask Anthony will demo this uploading process in lab
- Then view your applet online
When it doesn't work
- Usually a permissions problem
- Or a missing file
- Use your debugging skills and Java console to investigate
- Can you at least see the enclosing HTML page (ie, window title shows up)?
- If so, what Java error message do you get?
- Example setup
Searching
Searching
- finding an element: indexOf or contains (exists at all?) or count/frequency
- depends on underlying data structure
- at this point, we only know linear data structures: linked list or array
Linear search code
- naive approach: look at each item in sequence until you find what you're looking for
- if you don't find it by the end, it's not here
//assuming array-based implementation
public int indexOf(E item) {
for (int i = 0; i < this.array.length; i++) {
E element = this.array[i];
if (element.equals(item)) {
return i;
}
}
return -1; //not found
}
Linear search
- works with unordered data
- .equals should be overridden for elements to be useful here (if you don't want ==)
- could use
element.compareTo(item) == 0
too, but then limited to Comparables
- From past lab: recursive version
- usually done iteratively in practice
- Big-O? O(n)
Binary search
- only possible with sorted data
- requires elements be Comparable
- recursively divide sequence in half until item is found
- From past lab: recursive version
- often done recursively, but can also be done iteratively (why?)
Binary search code
public int indexOf(E item) {
int low = 0;
int high = array.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (item.compareTo(array[mid]) == 0) {
return mid;
}else if (item.compareTo(array[mid]) < 0) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return -1; //not found
}
Searching conclusion
- options depends on data
- unsorted requires linear (but O(n)'s not too bad...)
- sorted (which usually has overhead on insertion, but O(lg n) is nice)
- data structures
- linear here
- more later: trees (binary search tree), hashes (hashing search)
Sorting
Insertion and Selection sort
Sort as you insert
- Each time you add, put it in the right place.
- Did this with nodes on Quiz 8 and Exam 2
- With arrays, requires shifting:
public void add(E item) {
if (size == array.length) {
//increase array
array = java.util.Arrays.copyOf(array, array.length * 2);
}
//then shift to produce empty spot at insertion point:
int i = size; //need i after loop
while (i > 0 && array[i - 1].compareTo(item) > 0) {
array[i] = array[i - 1];
i--;
}
array[i] = item;
size++;
}
- Most sorting done with arrays (avoid extra overhead of seeking a particular node)
Insertion sort
- Start with an unsorted array, then just copy each element into a new sorted array
- each insert would be basically like previous slide
- but this requires 2 arrays
- Optimize: one array, with unsorted and sorted partitions
- (trace)
- animation
Insertion sort code
//from wikipedia pseudocode
for (int i = 1; i < array.length; i++) {
E item = array[i];
int hole = i;
while (hole > 0 && array[hole - 1].compareTo(item) > 0) {
array[hole] = array[hole - 1];
hole--;
}
array[hole] = item;
}
- Big-O? O(n2)
- But note behavior when already sorted or many equal values; reverse sorted
- stable sort (if stop shifting before first equal value)
Selection sort
- Similar to insertion: select next smallest from unsorted each time and add to end of sorted array
- (trace)
- animation
Selection sort code
//from wikipedia code
for (int i = 0; i < array.length - 1; i++) {
int min = i; //maybe current item is min...
//find smaller than min
for (int seek = i + 1; seek < array.length; seek++) {
if (array[seek].compareTo(array[min]) < 0) {
min = seek;
}
}
//swap min
if (min != i) { //optional check to reduce writes
E temp = array[i];
array[i] = array[min];
array[min] = temp;
}
}
- not stable (4\1 3\2 8\3 8\4 5\5)
Insertion vs Selection
- Both O(n2)
- When would one be better than another?
- (nearly) sorted to begin with
- (nearly) reverse sorted to begin with
- number of comparisons vs number of moves (shifts)
- a concern if writing is more expensive than reading
- tho other sorts that are even better at this
- Be able to recognize the difference
- More animations
For next time...
- More sorting (Chapter 8)
- GUI assignment to be posted