why does binary search require that an array be sorted
I have always heard that linear search is a naive approach and binary search is better than it in performance due to better asymptotic complexity. But I never understood why is it better than linear search when sorting is required before binary search?. That seems to be the basis of saying that binary search is better. But binary search requires sorting which is
for the best algorithms. So binary search shouldn't be actually faster as it requires sorting. I am reading CLRS in which the author implies that in insertion sort instead of using the naive linear search approach it is better to use binary search for finding the place where item has to be inserted. In this case this seems to be justified as at each loop iteration there is a sorted list over which the binary search can be applied. But in the general case where there is no guarantee about the data set in which we need to search isn't using binary search actually worse than linear search due to sorting requirements? Are there any practical considerations that I am overlooking which make binary search better than linear search? Or is binary search considered better than linear search without considering the computation time required for sorting?
In java language! Question 1 Which of the following are names for Big-O run times? a. variable b. linear c. constant d. quadratic Question 2 Big O notation describes ________. a. an algorithmвs efficiency in terms of the work required to solve a problem b. the difficulty of writing an algorithm to solve a specific problem c. the amount of memory required by an algorithm d. the length of an algorithm for solving a specific problem Question 3 An algorithm that is O(1) _______. a. can only search an array of size 1 b. does not necessarily require only one comparison c. requires only one comparison d. is inefficient Question 4 An O ( n ) algorithm is referred to as having a _______ run time. a. constant b. negative c. linear d. logarithmic e. quadratic Question 5 The linear search algorithm runs in ________time. a. constant b. nonlinear c. O( n ) d. O(log n) e. quadratic Question 6 The worst case in linear search is that every element must be checked to determine whether the search key exists, which occurs if the search key ________. a. is the last array element b. is not present c. is the last array element or is not present d.
None of the above. Question 7 How many comparisons will the linear search algorithm make if the search key is not in an array of 10 elements? Question 8 Which of the following is true? a. The binary search algorithm is less efficient than the linear search, but it requires that the array be sorted. b. The binary search algorithm is more efficient than the linear search, but it requires that the array be unsorted. c. The binary search algorithm is more efficient than the linear search, but it requires that the array be sorted. d. The binary search algorithm is less efficient than the linear search, but it requires that the array be unsorted. Question 9 Which of the following is a drawback of binary search? Select all that apply. a. The data must be in sorted order. b. It is slower (less efficient) than linear search. c. It requires significantly more memory than linear search. Question 10 What is the term used for binary searchвs run time? a. quadratic b. constant c. logarithmic d. negative e. linear Question 11 Using a binary search, what is the maximum number of comparisons required to find a search key in a 31-element sorted array?
For questions 12-15, to trace a search, write the index of each element that is visited separated by a single space. For example, in this array: [J, A, V, A] if you visited the first three indices, you would write: 0 1 2 if you visited the whole array, you would write: 0 1 2 3 Question 12 Trace a linear search on the following array. Question 13 Trace a linear search on the following array. Question 14 Trace an efficient linear search on the following sorted array. Question 15 Trace an efficient linear search on the following sorted array. For questions 16 and 17, to trace the binary search, write the value of mid each time it is calculated, separated by a single space. Use the algorithm given in the lecture notes/code. Question 16 Trace a binary search on the following sorted array. Question 17 Trace a binary search on the following sorted array. Question 18 Write a binary search method that uses recursion. The method header is: public int binSearch(int nums, int target) Hint: Include a helper method with additional parameters.
- Views: 0
why do we need to study binary numbers
why do we need to study binary numbers
why is my google voice search not working
why does it redirect my google searches
why does my hard drive make a lot of noise
why is all data stored in binary form
why my site is not indexed in google