Guest User

Untitled

a guest
Jul 17th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. BLOC WDT Module 3 - Computer Science Fundamentals
  2. Section 3 - Algorithms
  3. Checkpoint 5 - Algorithms: Searching
  4.  
  5. **Short Answer**
  6.  
  7. 1. What is a real-life scenario that uses linear search?
  8. > A. Finding where a particular participant finished a race. At for myself, I don't start looking in the middle, I start at the top and work my way down until I find who I'm looking for in the list
  9.  
  10. 2. What is a real-life scenario that uses binary search?
  11. > A. The way most people search for a word in a dictionary mirrors a binary search somewhat. I usually pull open the dictionary somewhere other than the start or the end, compare where I am to what I'm looking for, and move forward or backward from there accordingly.
  12.  
  13. 3. Given the alphabetically sorted collection in this checkpoint, how many iterations would it take to find the value G using linear search?
  14. > A. 7
  15.  
  16. 4. Given the alphabetically sorted collection in this checkpoint, how many iterations would it take to find the value G using binary search?
  17. > A. 3
  18.  
  19. 5. Given an unsorted collection of a million items, which algorithm would you choose between linear search and binary search? Explain your reasoning.
  20. > A. Binary search is not possible on an unsorted list, so linear
  21.  
  22. 6. Given a sorted collection of a million items, which algorithm would you choose between linear search and binary search? Explain your reasoning.
  23. > A. For a SORTED data set that large I would use a binary search for sure. Binary searches work with sorted collections and with linear search the worst case would be O(n) but binary search case only O(log n)
  24.  
  25. **Programming Assignment:**
  26. 1. You and a friend have set a wager to see who can find the word "Albatross" in the dictionary the fastest. Write a program to allow you to win the bet.
  27. > A. Since the dictionary is already sorted, and the word sought may change in the future I would implement a binary search even though in this case a linear search may be faster since Albatross is close to the front of the dictionary.
  28. ```javascript
  29. function wordSearch(arrayToSearch, wordToFind) {
  30. var lowElement = arrayToSearch[0];
  31. var highElement = arrayToSearch[arrayToSearch.length - 1];
  32.  
  33. while (lowElement <= highElement) {
  34. var middleElement = (arrayToSearch.indexOf(lowElement) + arrayToSearch.indexOf(highElement)) / 2;
  35. middleElement = Math.round(middleElement);
  36.  
  37. if (arrayToSearch[middleElement] > wordToFind) {
  38. highElement = arrayToSearch[middleElement - 1];
  39. } else if (arrayToSearch[middleElement] < wordToFind) {
  40. lowElement = arrayToSearch[middleElement + 1];
  41. } else {
  42. return arrayToSearch[middleElement];
  43. }
  44. }
  45. return 'That name is not in the list';
  46. }
  47.  
  48. var dictionary = ['Albatross','Baboon','Cat','Dog','Egret','Falcon','Gorilla','Hare','Incubus','Jellyfish','Killer Whale'];
  49. wordSearch(dictionary, 'Albatross');
  50. ```
  51.  
  52. 2. You work at a pet store, and a child has asked you to net the only white-spotted goldfish from the fish tank. Write a program that will help you net the right fish.
  53. > A. Since the fish tank is unsorted we will have to use a linear search
  54. ```javascript
  55. function findFish(fishesToSearch, fishToFind) {
  56. for (var i = 0; i < fishesToSearch.length; i++) {
  57. if (fishesToSearch[i] === fishToFind) {
  58. return "You netted a " + fishToFind + " from the fish tank";
  59. }
  60. }
  61. return "There is no " + fishToFind + " in the tank";
  62. }
  63.  
  64. var fishTank = ['goldfish','goldfish','green swordtail','commmon molly','goldfish','platy','white-spotted goldfish','platy','common molly','goldfish'];
  65.  
  66. findFish(fishTank, 'white-spotted goldfish');
  67. ```
Add Comment
Please, Sign In to add comment