CS 105 Week 5 Midterm #1 Tuesday, Oct. 7, 2014 8:00pm 9:30pm - - PowerPoint PPT Presentation

cs 105
SMART_READER_LITE
LIVE PREVIEW

CS 105 Week 5 Midterm #1 Tuesday, Oct. 7, 2014 8:00pm 9:30pm - - PowerPoint PPT Presentation

CS 105 Week 5 Midterm #1 Tuesday, Oct. 7, 2014 8:00pm 9:30pm Midterm #1 Conflict Exam Signup Posted on course website tonight! Deadline : Tuesday by 9:00pm CS 105: Improved 9am Lecture Video Recorded TA Lecture Notes Lecture Review


slide-1
SLIDE 1

CS 105

Week 5

slide-2
SLIDE 2

Midterm #1 Tuesday, Oct. 7, 2014 8:00pm – 9:30pm

slide-3
SLIDE 3

Midterm #1 Conflict Exam Signup Posted on course website tonight! Deadline: Tuesday by 9:00pm

slide-4
SLIDE 4

CS 105: Improved 9am Lecture Video Recorded TA Lecture Notes

slide-5
SLIDE 5

Lecture Review

slide-6
SLIDE 6

Which conditional does not contain an error in the code? A) if (a = 50) B) if (x =! 40) C) if (c > 4 || c < 2) D) if (x > 20 & > 30) E) IF (y <= 50)

slide-7
SLIDE 7

Which conditional does not contain an error in the code? A) if (a = 50) B) if (x =! 40) C) if (c > 4 || c < 2) D) if (x > 20 & > 30) E) IF (y <= 50)

slide-8
SLIDE 8

var a = [ [2, 3], [4, 5], [6, 2] ];

What is the value of a[1][1]?

A) 2 B) 3 C) 4 D) 5 E) 6

slide-9
SLIDE 9

var a = [ [2, 3], [4, 5], [6, 2] ];

What is the value of a[1][1]?

A) 2 B) 3 C) 4 D) 5 E) 6

slide-10
SLIDE 10

Searching

slide-11
SLIDE 11

How do I search for a string inside an array?

var s = "carrot"; var fruits = ["apple", "banana", "grape", "orange", "strawberry“];

slide-12
SLIDE 12

How do I search for a string inside an array?

var s = "carrot"; var fruits = ["apple", "banana", "grape", "orange", "strawberry“]; // for each element in the array: // check if the element is a match

slide-13
SLIDE 13

var s = "carrot"; var fruits = ["apple", "banana", "grape", "orange", "strawberry“]; for (var i = 0; i < fruits.length; i++) { if (s == fruits[i]) { return true; } } return false;

slide-14
SLIDE 14

function linearSearch(s, list) { for (var i = 0; i < list.length; i++) { if (s == list[i]) { return true; } } return false; }

slide-15
SLIDE 15

Linear Search

  • Move sequentially through an array,

checking each element for a match.

  • If the length of the array is…

–100:

slide-16
SLIDE 16

Linear Search

  • Move sequentially through an array,

checking each element for a match.

  • If the length of the array is…

–100: check up to 100 elements.

slide-17
SLIDE 17

Linear Search

  • Move sequentially through an array,

checking each element for a match.

  • If the length of the array is…

–100: check up to 100 elements. –1000:

slide-18
SLIDE 18

Linear Search

  • Move sequentially through an array,

checking each element for a match.

  • If the length of the array is…

–100: check up to 100 elements. –1000: check up to 1000 elements.

slide-19
SLIDE 19

Linear Search

  • Move sequentially through an array,

checking each element for a match.

  • If the length of the array is…

–100: check up to 100 elements. –1000: check up to 1000 elements.

slide-20
SLIDE 20

Linear Search

  • Move sequentially through an array,

checking each element for a match.

  • If the length of the array is…

–100: check up to 100 elements. –1000: check up to 1000 elements.

  • If the list doubles, it takes twice as

long.

slide-21
SLIDE 21

Better Searching?

slide-22
SLIDE 22

Binary Search

  • Look at an element in the center of an
  • array. Remove the half of the array

where the element would not exist.

slide-23
SLIDE 23

“apple” “banana” “blackberry” “blueberry” “grape” “kiwi” “lemon” “orange” “strawberry” “carrot”

slide-24
SLIDE 24

“apple” “banana” “blackberry” “blueberry” “grape” “kiwi” “lemon” “orange” “strawberry” “carrot”

slide-25
SLIDE 25

“apple” “banana” “blackberry” “blueberry” “grape” “kiwi” “lemon” “orange” “strawberry” “carrot”

slide-26
SLIDE 26

“apple” “banana” “blackberry” “blueberry” “grape” “kiwi” “lemon” “orange” “strawberry” “carrot”

slide-27
SLIDE 27

“apple” “banana” “blackberry” “blueberry” “grape” “kiwi” “lemon” “orange” “strawberry” “carrot”

slide-28
SLIDE 28

“apple” “banana” “blackberry” “blueberry” “grape” “kiwi” “lemon” “orange” “strawberry” “carrot”

slide-29
SLIDE 29

“apple” “banana” “blackberry” “blueberry” “grape” “kiwi” “lemon” “orange” “strawberry” “carrot”

slide-30
SLIDE 30

Binary Search

  • Look at an element in the center of an
  • array. Remove the half of the array

where the element would not exist.

  • If the length of the array is…

–9:

slide-31
SLIDE 31

Binary Search

  • Look at an element in the center of an
  • array. Remove the half of the array

where the element would not exist.

  • If the length of the array is…

–9: up to 3 comparisons

slide-32
SLIDE 32

Binary Search

  • Look at an element in the center of an
  • array. Remove the half of the array

where the element would not exist.

  • If the length of the array is…

–9: up to 3 comparisons –18:

slide-33
SLIDE 33

Binary Search

  • Look at an element in the center of an
  • array. Remove the half of the array

where the element would not exist.

  • If the length of the array is…

–9: up to 3 comparisons –18: up to 4 comparisons

slide-34
SLIDE 34

Binary Search

  • If the length of the array is…

–9: up to 3 comparisons –18: up to 4 comparisons –100: up to 7 comparisons

slide-35
SLIDE 35

Binary Search

  • If the length of the array is…

–9: up to 3 comparisons –18: up to 4 comparisons –100: up to 7 comparisons –1000: up to 10 comparisons

slide-36
SLIDE 36

Binary Search

  • If the length of the array is…

–9: up to 3 comparisons –18: up to 4 comparisons –100: up to 7 comparisons –1000: up to 10 comparisons –1,000,000: up to 20 comparisons

slide-37
SLIDE 37

Binary Search

  • If the length of the array is…

–9: up to 3 comparisons –18: up to 4 comparisons –100: up to 7 comparisons –1000: up to 10 comparisons –1,000,000: up to 20 comparisons –7,000,000,000: only 33 comparisons!

slide-38
SLIDE 38

Binary Search

  • Look at an element in the center of an
  • array. Remove the half of the array

where the element would not exist.

slide-39
SLIDE 39

Binary Search

  • Look at an element in the center of a

sorted array. Remove the half of the array where the element would not exist.

slide-40
SLIDE 40

Binary Search

  • Look at an element in the center of a

sorted array. Remove the half of the array where the element would not exist.

  • If you double the size of the data, only
  • ne additional comparison is needed.

– Allows for fast processing of big data.

slide-41
SLIDE 41

Sorting

slide-42
SLIDE 42

Sorting vs. Searching

  • Searching is “easy”:

–Unsorted  Linear Search –Sorted  Binary Search

  • Sorting is “hard”:

–Hundreds of different algorithms

slide-43
SLIDE 43

Selection Sort

  • Find the element that is

alphabetically first in the list.

  • Swap that element with the first

element in the list.

  • Repeat for the 2nd element.
slide-44
SLIDE 44

“lemon” “grape” “apple” “strawberry” “banana” “kiwi” “blackberry” “orange” “blueberry”

slide-45
SLIDE 45

“lemon” “grape” “apple” “strawberry” “banana” “kiwi” “blackberry” “orange” “blueberry”

slide-46
SLIDE 46

“lemon” “grape” “apple” “strawberry” “banana” “kiwi” “blackberry” “orange” “blueberry”

slide-47
SLIDE 47

“lemon” “grape” “apple” “strawberry” “banana” “kiwi” “blackberry” “orange” “blueberry”

slide-48
SLIDE 48

“lemon” “grape” “apple” “strawberry” “banana” “kiwi” “blackberry” “orange” “blueberry”

slide-49
SLIDE 49

“lemon” “grape” “apple” “strawberry” “banana” “kiwi” “blackberry” “orange” “blueberry”

slide-50
SLIDE 50

“lemon” “grape” “apple” “strawberry” “banana” “kiwi” “blackberry” “orange” “blueberry”

slide-51
SLIDE 51

“lemon” “grape” “apple” “strawberry” “banana” “kiwi” “blackberry” “orange” “blueberry”

slide-52
SLIDE 52

“lemon” “grape” “apple” “strawberry” “banana” “kiwi” “blackberry” “orange” “blueberry”

slide-53
SLIDE 53

“lemon” “grape” “apple” “strawberry” “banana” “kiwi” “blackberry” “orange” “blueberry”

slide-54
SLIDE 54

“lemon” “grape” “apple” “strawberry” “banana” “kiwi” “blackberry” “orange” “blueberry”

slide-55
SLIDE 55

“lemon” “grape” “apple” “strawberry” “banana” “kiwi” “blackberry” “orange” “blueberry”

slide-56
SLIDE 56

“lemon” “grape” “apple” “strawberry” “banana” “kiwi” “blackberry” “orange” “blueberry”

slide-57
SLIDE 57

“apple” “grape” “lemon” “strawberry” “banana” “kiwi” “blackberry” “orange” “blueberry”

slide-58
SLIDE 58

Popular Sorting Algorithms

  • Selection Sort
  • Bubble Sort
  • Merge Sort
  • Quick Sort
  • Radix Sort