Science II Arrays Li Xiong 1 Roadmap Basics of Array Number - - PowerPoint PPT Presentation

science ii
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

1

CS 171: Introduction to Computer Science II Arrays

Li Xiong

slide-2
SLIDE 2

Roadmap

  • Basics of Array
  • Number guessing and Binary Search
  • Hw1: Number guessing game
  • Arraylist
  • OO and inheritance
slide-3
SLIDE 3

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.

slide-4
SLIDE 4

Guess-a-Number Game

  • Assuming the correct answer is 33:
slide-5
SLIDE 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

  • rdered array is that it allows

faster search using binary search.

slide-6
SLIDE 6

Binary Search

  • Goal: given a specific key, find it’s location in

an ordered array if it exists.

slide-7
SLIDE 7

// 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

slide-8
SLIDE 8

Binary Search Cost

  • What’s the number of steps for binary search

in a sorted array of size N?

slide-9
SLIDE 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

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

slide-11
SLIDE 11

Hw1 Discussion

  • Read the instructions carefully
  • Think before you code
  • Some hints and useful classes/methods

– Algorithm – ArrayList – Random Number generation

slide-12
SLIDE 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
slide-13
SLIDE 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);

slide-14
SLIDE 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);

slide-15
SLIDE 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));

slide-16
SLIDE 16

Roadmap

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