Javascript search5/25/2023 ![]() Linear Search has a space complexity of O(1) – constant space. If our target element is somewhere in the middle of the array, then the time complexity will be approximately O(n/2), which simplifies to O(n) – linear time. ![]() Average-case time complexity of Linear Search This means that the Big O notation of Linear Search is Big O(n) – linear time complexity. If our target is the last element in the array, then the algorithm will have to make n comparisons (n being the length of the input array). Worst-case time complexity of Linear Search The algorithm will always only have to perform one comparison, no matter what the size of the array. If our target value is at the beginning of the array, the algorithm will always run at constant time, O(1). ![]() Time complexity of Linear Search Best-case time complexity of Linear Search If the target isn’t found, then we simply return -1 at the end of the function. ![]() We simply loop over each element in the array, and check to see if the current element is equal to the target if so, we return that elements index. Source of the above gif: Īnd if you ever find yourself looking for a specific length of French fry: Linear Search in JavaScriptĬonsole.
0 Comments
Leave a Reply. |