CS100J, Fall 2001 Tues 11/6 Lecture 19 ------------------------------------------------------------------------------- Announcements: + reading: all of Chap7, 10.1 (need for P6) + P6 posted in about 1 week (need to cover some inheritance first) + T3 Tues 11/20 (see Exams-->Review 3 and Exams-->Prelim 3 in a week or so) + E7 due 11/8, E8 due 11/13 (see Exercise Listing) ------------------------------------------------------------------------------- Summary: + multidimensional rectangular (think row-col-depth...) + multidimensional ragged arrays (use aoa concept... 1st, 2nd, 3rd,... dims) + row and col major ------------------------------------------------------------------------------- Topics: + linear searching + binary searching + selection sort + insertion sort ------------------------------------------------------------------------------- Linear search: + search for an element in a collection one at a time ex) find the max value in an array // Create int[] array = new int[10]; // Fill array with random values: for (int i=0;i 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) ------------------------------------------------------------------------------- Example of insert sort: + start with 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 -------------------------------------------------------------------------------