willieshi232

Untitled

May 3rd, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. 1.You can perform a sequential search over the array using a loop, or you can sort the array using Arrays.sort and then perform a binary search over it using Arrays.binarySearch.
  2.  
  3. 2.Closest value to the number of elements that the binary search algorithm will need to examineon an array of one million integers:
  4.  
  5. 3. e
  6. 4. A sequential search must be used on an array of Point objects because they do not implement Comparable.
  7.  
  8. 5. Arrays.binarySearch and Collections.binarySearch can be used successfully if the array or collection contains elements that are sorted, according to either their natural ordering or the ordering of a Comparator.
  9.  
  10. 6. Collections.sort on a list of strings would arrange them in alphabetical order, case-sensitive. To change the order, you could pass a Comparator that defines a different order.
  11.  
  12. 7. Collections.sort would not work on a list of Point objects by default because they do not implement the Comparable interface. To make it work, you could pass a Comparator that defines an ordering for Points.
  13.  
  14. 8. The AccountComparator shown has a few errors:
  15.  
  16. 9.line 2: It should implement Comparator rather than just Comparator.
  17. line 3: The method should be named compare, not compareTo. It should also accept two BankAccount parameters, not just one.
  18. lines 5,7: The code should refer to the two parameters passed in, not this.
  19. line 7: You cannot return the subtraction of two doubles from a compare method. The result must be of type int, and it must still work even if the accounts differ only by a few cents.
  20. Here is a corrected version of the code:
  21.  
  22. 10.We could easily reverse the order of our LengthComparator by using the built-in method Collections.reverseOrder, which accepts a Comparator and returns a new one with the opposite order of the one passed in.
  23.  
  24. 11. O(log N)
  25.  
  26. 12. O(N)
  27.  
  28. 13. O(N2)
  29.  
  30. 14. O(N2)
  31.  
  32. 15.O(N)
  33.  
  34. 16.Complexity classes of the given algorithms in terms of N:
  35.  
  36. O(N)
  37. O(N2)
  38. O(N)
  39. O(N)
  40. O(log B)
  41. O(N3)
  42. O(N)
  43. O(N)
  44. 17.Complexity classes of the given statements:
  45.  
  46. O(N log N)
  47. O(N2)
  48. O(N2 log N)
  49. O(N)
  50. O(1)
  51. O(N)
  52. O(N!)
  53. The runtime complexity of both sequential searches is O(N).
  54.  
  55. 17.Binary search requires a sorted dataset because it uses the ordering to jump to the next index. If the elements are out of order, the search isn't guaranteed to find the target element.
  56.  
  57. 18.A binary search of 60 elements examines at most 6 elements, because log2 60 (when rounded up) equals
  58. 13.3
  59. 19.
  60. a.The algorithm will examine index 4 and will return 4.
  61. b.The algorithm will examine indexes 4 and 6 and will return 6.
  62. c.The algorithm will examine indexes 4, 6, and 7 and will return 7.
  63. d.The algorithm will examine indexes 4, 2, 1, and 0 and will return 0.
  64. 20.The algorithm will examine indexes 4, 6, and 5 and will return -1. The algorithm doesn't work properly because the input array isn't sorted.
  65. 21.
  66. 42: examines 7, 11, 9; returns 9
  67. 11: examines 7, 3, 4; returns -5
  68. 74: examines 7, 11, 13, 14; returns 14
  69. 30: examines 7, 3, 5, 6; returns -8
  70. 22.
  71. -5: examines 6, 2, 4, 3; returns -4
  72. 0: examines 6; returns 6
  73. 11: examines 6, 10, 8, 9; returns -10
  74. -100: examines 6, 2, 0; returns -1
  75. 24.
  76. d. [-4, 17, 3, 94, 46, 8, 29, 12]
  77. 13.4
  78. 26.
  79. merge sort of 32 elements will generate 63 total calls to mergeSort and will perform the merge operation 31 times.
  80. 27.
  81. a.
  82. [1, 2, 3, 4, 5, 11, 9, 7, 8, 10]
  83. b.
  84. [7, 2, 8, 4, 1, 11, 9, 5, 3, 10]
  85. [7, 2, 8, 4, 1], [11, 9, 5, 3, 10]
  86. [7, 2], [8, 4, 1], [11, 9], [5, 3, 10]
  87. [7], [2], [8], [4, 1], [11], [9], [5], [3, 10]
  88. [4], [1], [3], [10]
  89. [8], [1, 4], [5], [3, 10]
  90. [2, 7], [1, 4, 8], [9, 11], [3, 5, 10]
  91. [1, 2, 4, 7, 8], [3, 5, 9, 10, 11]
  92. [1, 2, 3, 4, 5, 7, 8, 9, 10, 11]
  93. 28.
  94. [-3, -1, 1, 2, 4, 8, 7, 21, 12, 30, 6, 9]
  95. 29.b.
Advertisement
Add Comment
Please, Sign In to add comment