1.

Given an unsorted array of n elements find if the element k is present in the array or not javascript

Answer»

n unsorted array of n elements, find if the element k is present in the array or not.Complete the findNumber function in the editor below. It has 2 PARAMETERS:An array of integers, arr, denoting the elements in the array.An integer, k, denoting the element to be searched in the array.The function must return a string “YES” or “NO” denoting if the element is present in the array or not.I have seen this type of problem before in a lecture. It is most efficient to use a binary search algorithm after sorting the array.Binary search works like so: A sorted array is like a phone book. I want to look up the number for something like “Gary’s Guitar Lessons.” I flip to the MIDDLE, and the page has the number for “Ma and Pa’s Diner.” I throw out the bottom half because “Gary’s” would be in between the beginning and the middle. Then, I would repeat the process of (1) going to the middle and checking, (2) throwing out a half, and (3) checking the middle of the other half. Eventually, I will find it or determine it’s missing from the phone book.Binary search is better than linear search in terms of time COMPLEXITY because the amount of data gone through goes down by a half, as OPPOSED to 1 item LESS. (assuming both are given a sorted array as an argument)I implemented a binary search quickly based on my pseudocode knowledge…



Discussion

No Comment Found