cs 105
play

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


  1. CS 105 Week 5

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

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

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

  5. Lecture Review

  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)

  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)

  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

  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

  10. Searching

  11. How do I search for a string inside an array? var s = "carrot"; var fruits = ["apple", "banana", "grape", "orange", "strawberry“];

  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

  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;

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

  15. Linear Search • Move sequentially through an array, checking each element for a match. • If the length of the array is… – 100:

  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.

  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:

  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.

  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.

  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.

  21. Better Searching?

  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.

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

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

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

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

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

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

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

  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:

  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

  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:

  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

  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

  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

  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

  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!

  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.

  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.

  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 one additional comparison is needed. – Allows for fast processing of big data.

  41. Sorting

  42. Sorting vs. Searching • Searching is “easy”: – Unsorted  Linear Search – Sorted  Binary Search • Sorting is “hard”: – Hundreds of different algorithms

  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 2 nd element.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  58. Popular Sorting Algorithms • Selection Sort • Bubble Sort • Merge Sort • Quick Sort • Radix Sort

Recommend


More recommend