Presentation is loading. Please wait.

Presentation is loading. Please wait.

15-111 Recitation 2 Tuesday, January 27, 2009. General Stuff Office Hours: Mon-Thurs 7-10 Habermann Did everybody manage to set up a way to turn things.

Similar presentations


Presentation on theme: "15-111 Recitation 2 Tuesday, January 27, 2009. General Stuff Office Hours: Mon-Thurs 7-10 Habermann Did everybody manage to set up a way to turn things."— Presentation transcript:

1 15-111 Recitation 2 Tuesday, January 27, 2009

2 General Stuff Office Hours: Mon-Thurs 7-10 Habermann Did everybody manage to set up a way to turn things in? (SSH, FTP)‏

3 Java Review Time! Arrays Classes Constructors private/public

4 Scanner Class Scanner sc = new Scanner(System.in); while(sc.hasNext()){ String line = sc.nextLine(); int num = sc.nextInt(); } sc.close();

5 Binary Search More efficient than brute force Each iteration causes the array to be cut in half O(log n)‏ BinarySearch(A[0..N-1], value) { low = 0 high = N - 1 while (low <= high) { mid = low + (high - low) / 2 if (A[mid] > value)‏ high = mid - 1 else if (A[mid] < value)‏ low = mid + 1 else return mid } return -1 }

6 Selection Sort Algorithm: 1. Find the minimum value in the list 2. Swap it with the value in the first position 3. Repeat the steps above for remainder of the list (starting at the second position)‏ O(n^2)‏

7 Insertion Sort Locate where item needs to go Shift other items Insert item into place Good when you're only adding one element at a time

8 Asymptotic Analysis Complexity Big-Oh N generally refers to the number of elements We only care about n increasing towards infinity O(n) = linear O(log n) = (like binary search)‏ O(n^2) = (like selection sort)‏ Coefficients don't matter


Download ppt "15-111 Recitation 2 Tuesday, January 27, 2009. General Stuff Office Hours: Mon-Thurs 7-10 Habermann Did everybody manage to set up a way to turn things."

Similar presentations


Ads by Google