# What Is Searching in Data Structure With Example?

//

Larry Thompson

Data structures are an essential part of computer science and programming. They provide a way to store and organize data efficiently, allowing for quick and easy access.

One fundamental operation in data structures is searching. In this article, we will explore what searching is in the context of data structures, and provide examples to illustrate its importance.

## What is Searching?

Searching refers to the process of finding a specific element or value within a collection of data. It is a common operation performed on various data structures such as arrays, linked lists, trees, and graphs. The goal of searching is to determine whether the desired element exists in the data structure and, if so, its location or any associated information.

## Types of Searching Algorithms

There are several algorithms that can be used for searching in data structures. Here are some commonly used ones:

• Linear Search: This algorithm checks each element in the collection one by one until it finds the desired element or reaches the end of the collection.
• Binary Search: Binary search is a more efficient algorithm that can only be performed on sorted collections. It repeatedly divides the collection into halves until it finds the desired element.
• Hashing: Hashing involves using a hash function to map elements to their storage locations in a data structure called a hash table. This enables constant-time lookup for retrieving elements.

## Example: Linear Search

To understand searching better, let’s look at an example using linear search on an array:

``````<html>
<title>Linear Search Example</title>
<body>
<script>
function linearSearch(arr, key) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === key) {
return i;
}
}
return -1;
}

const array = [4, 7, 2, 9, 1];
const key = 9;

const result = linearSearch(array, key);
console.log("Element", key, "found at index", result);
</script>
</body>
</html>```
```

In this example, we define a function called linearSearch that takes an array and a key as parameters. It iterates through each element of the array and checks if it matches the key.

If a match is found, the function returns the index of the element. Otherwise, it returns -1 to indicate that the element was not found.

We then create an array called array containing some numbers and define a variable key with the value of 9. We call the linearSearch function with these values and store the result in a variable called result. Finally, we log the result to the console.

### Conclusion

In conclusion, searching is a fundamental operation in data structures that allows us to find specific elements efficiently. By understanding different searching algorithms and their implementations, we can optimize our programs for better performance. Whether it’s linear search for simple arrays or more complex algorithms like binary search or hashing for larger data structures, mastering searching techniques is crucial for effective programming.

I hope this article has given you a clear understanding of what searching is in the context of data structures. Feel free to explore more about searching algorithms and experiment with different implementations to deepen your knowledge.