# What is the best way to search for an item in a sorted list in JavaScript?

In the given problem statement we are asked to find the best way to search for an element in a sorted list with the help of javascript functionalities. As we talk about sorting any list or array the binary search algorithm is the best way in data structures.

## What is binary search in a sorted list in JavaScript ?

Let's understand the working of a list in JavaScript.

A list is an object that stores multiple elements. In programming, a list is a collection of similar data elements under one roof. Since a list is also an object, it has some properties and methods that make working with lists easier in JavaScript.

Following is the syntax to define list in JavaScript : −

list = [
{ day: 'Monday', reading: 3, id: 201},
{ day: 'Tuesday', reading: 5, id: 202},
{ day: 'Sunday', reading: 6, id: 405}
];


In the data structures binary search is a very efficient way to search an item in a sorted list. Binary search basically works on divide and conquer methods. And this approach takes O(log n) time which is better than the linear search, because linear search algorithm takes O(n) time.

Binary search can be implemented using iterative and recursive methods.

We have given a sorted list. Our task is to find the index of an element in the list with the help of binary search. So we will understand this with an example:

Input array = {2, 4, 6, 8, 10, 12}
a = 5
Output : Item found!

Input array = {2, 4, 6, 8, 10, 12}
a = 7


## Logic for Finding the Element

The most easy way to find an item in a sorted list in javascript is by using the binary search algorithm.

The algorithm works on divide and conquer methods. So in the divide and conquer method we divide a sorted list in half and compare the search element with the middle item.

If the mid element is similar to the search element, the searching task is complete. Otherwise we check the search item is less than the mid element, the searching will continue in the left half of the list. If the search item is greater than the mid element then we search it in the remaining right side of the sorted list.If the mid element is similar to the search element, the searching task is complete. Otherwise we check the search item is less than the mid element, the searching will continue in the left half of the list. If the search item is greater than the mid element then we search it in the remaining right side of the sorted list.

### Algorithm

Step 1 − Declare a function named itemSearch taking in a list and the element to search as arguments.

Step 2 − Declare the left and right pointer variables to find the exact location of the search element.

Step 3 − The function we have declared just above in Step 1 is basically a recursive function. The function is working with a while loop inside it. This loop will run until the left element is less than or equal to the right element.

Step 4 − Now forwarding , after the while loop we will calculate the middle element by adding left and right elements and divide the sum by 2. So in this step we have our mid element.

Step 5 − The following Step 4 needs to be recursively done for that particular mid element. Next we will check that the search item is less or greater than the mid item. If it is less than a search term then find it in the left side of the list otherwise search in the right side of the mid element.

Step 6 − Now for checking the search term we will use the middle variable to check the conditions with the mid element. If middleElement is less than the search item then increase the middle count by 1. Otherwise decrease the middle count by 1.

Step 7 − This is how the recursive binary search will work. Now in this step define a sorted list and search item and call the function to get the output.

### Example

// define function for binary search
function itemSearch(list, searchItem) {
let left = 0;
let right = list.length - 1;

// loop for finding the element
while (left <= right) {
const middle = Math.floor((left + right) / 2);
const middleElement = list[middle];

// condition for search item
if (middleElement === searchItem) {
return middle;
} else if (middleElement < searchItem) {
left = middle + 1;
} else {
right = middle - 1;
}
}

return -1;
}

// create search list
const searchList = [11, 13, 15, 17, 19];
const searchingFor = 15;

// calling the function
const foundIndex = itemSearch(searchList, searchingFor);

if (foundIndex === -1) {
} else {
console.log(Element found at index \${foundIndex}.);
}


### Output

 Element fFound at index 2.


The above mentioned code is the very simple code one can think of while looking at the problem statement, But after understanding this logic you can optimize it to the better quality of space and time by making it more flexible and simple.

In the above code we have declared a function taking in the list and search item as input. Then we go step by step , following first to divide the list in half by getting the mid element. And search the required element with the help of this element by comparing it.

In the code, the itemSearch function is used to search for the number in the sorted list [11, 13, 15, 17, 19]. The function returns the index value of 15 that is 2. This shows that the search item is found at index 2.

## Time Complexity

So the length of the sorted list is n, which determines the time complexity of the binary search algorithm, which will be O(log n). This is because the size of the search space is cut in half with each iteration of the loop. As an output, the number of searches need to find an item is equal to the size of the list.

This technique has a constant memory required besides the size of the input list. So the space complexity for this is O(1). Because the algorithm can keep track of the current search space and the location of the search element by just saving a small number of variables.

## Conclusion

This is how we can solve the above problem statement. The most easy and reliable method to search for an element in a sorted list in JavaScript is with the help of a binary search algorithm. This method has a time complexity of O(log n) and a space complexity of O(1). The method continuously divides the sorted list in half and compares the search term with the mid element. And allow it to quickly narrow down the search memory until the search item is found to be not in the list.

Updated on: 23-Aug-2023

215 Views