science ii
play

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


  1. CS 171: Introduction to Computer Science II Arrays Li Xiong 1

  2. Roadmap • Basics of Array • Number guessing and Binary Search • Hw1: Number guessing game • Arraylist • OO and inheritance

  3. Guess-a-Number Game • 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. • Think about a strategy that leads to the fewest number of guesses on average.

  4. Guess-a-Number Game • Assuming the correct answer is 33:

  5. 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 ordered array is that it allows faster search using binary search .

  6. Binary Search • Goal : given a specific key, find it’s location in an ordered array if it exists.

  7. Binary Search - BinarySearch.java // 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; }

  8. Binary Search Cost • What’s the number of steps for binary search in a sorted array of size N?

  9. 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

  10. 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.

  11. Hw1 Discussion • Read the instructions carefully • Think before you code • Some hints and useful classes/methods – Algorithm – ArrayList – Random Number generation

  12. 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

  13. 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);

  14. 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);

  15. 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));

  16. Roadmap • Basics of Array • Number guessing and Binary Search • Hw1: Number guessing game • OO and inheritance

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend