1
Science II Arrays Li Xiong 1 Roadmap Basics of Array Number - - PowerPoint PPT Presentation
Science II Arrays Li Xiong 1 Roadmap Basics of Array Number - - PowerPoint PPT Presentation
CS 171: Introduction to Computer Science II Arrays Li Xiong 1 Roadmap Basics of Array Number guessing and Binary Search Hw1: Number guessing game Arraylist OO and inheritance Guess-a-Number Game I am asking you to guess
Roadmap
- Basics of Array
- Number guessing and Binary Search
- Hw1: Number guessing game
- Arraylist
- OO and inheritance
Guess-a-Number Game
- Think about a strategy that leads to the
fewest number of guesses on average.
- I am asking you to guess a number that I am
thinking of between 1 and 100
- When you make a guess, I will tell you one of
three things: too large, too small, your guess is correct.
Guess-a-Number Game
- Assuming the correct answer is 33:
Ordered Array
- An array in which data elements are sorted in
ascending values.
– The smallest value at index 0 – Each element is larger that its previous element
- Search
– The main advantage of an
- rdered array is that it allows
faster search using binary search.
Binary Search
- Goal: given a specific key, find it’s location in
an ordered array if it exists.
// precondition: array a[] is sorted public static int rank(int key, int[] a) { int lo = 0; int hi = a.length - 1; while (lo <= hi) { // Key is in a[lo..hi] or not present. int mid = lo + (hi - lo) / 2; if (key < a[mid]) hi = mid - 1; else if (key > a[mid]) lo = mid + 1; else return mid; } return -1; }
Binary Search - BinarySearch.java
Binary Search Cost
- What’s the number of steps for binary search
in a sorted array of size N?
Binary Search Cost
- What’s the number of steps for binary search
in a sorted array of size N?
– Best case: 1 – Worst case: log N
Hw1: Number guessing game
- Guess a number I am thinking of between
[1000, 9999]
- When you make a guess, I will tell you the
number of matching digits between your guess and my number
- Think about a strategy that leads to the
fewest number of guesses on average.
Hw1 Discussion
- Read the instructions carefully
- Think before you code
- Some hints and useful classes/methods
– Algorithm – ArrayList – Random Number generation
Hw1 Algorithm
- Initialize a list of possible numbers
- Repeat
– Give a random guess
- think about how to generate a random index
– Update the list based on input matching digits
- think about how to eliminate impossible numbers (read the
hint)
- Until the list is empty or the guess is correct
ArrayList
- ArrayList – resizable-array implementation
- Use generics - parameterized types
– Type parameters have to be instantiated as reference types – Autoboxing: Automatically casting a primitive type to a wrapper type – Auto-unboxing: automatically casting a wrapper type to a primitive type
ArrayList<Integer> numbers = new ArrayList<Integer>(); numbers.add(1001); int mynumber = numbers.remove(0);
ArrayList
- Useful methods
– add(E e): Appends the specified element to the end of this list – size(): returns the number of elements in this list – remove(int index): Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices) – get(int index): Returns the element at the specified position in this list.
ArrayList<Integer> numbers = new ArrayList<Integer>(); numbers.add(1001); numbers.add(1002); int n = numbers.size(); int mynumber = numbers.get(0); mynumber = numbers.remove(0); mynumber = numbers.get(0);
Random Number Generation
- Method
static double Math.random() - returns a random double value between [0.0, 1.0)
- Examples
– Generate a random number between [0, 100) int index = (int) Math.random() * 100; – Generate a random integer between [a, b)
int x = a+ (int) (Math.random() * (b-a));
Roadmap
- Basics of Array
- Number guessing and Binary Search
- Hw1: Number guessing game
- OO and inheritance