# What Is Searching in Data Structure and Its Type?

//

Scott Campbell

Searching in data structure is the process of finding a specific element or value in a given collection of data. It is an essential operation that allows us to efficiently retrieve information from large datasets. In this article, we will explore the concept of searching in data structures and discuss its different types.

## Linear Search

Linear search, also known as sequential search, is the simplest and most straightforward searching algorithm. It involves sequentially checking each element in a collection until the desired element is found or the entire collection has been traversed.

To perform a linear search, we start at the beginning of the collection and compare each element with the Target value. If a match is found, we can terminate the search and return the position of the element. If no match is found after checking all elements, we can conclude that the Target value is not present in the collection.

Example:

``````
function linearSearch(arr, Target) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === Target) {
return i;
}
}
return -1;
}

const numbers = [5, 10, 15, 20, 25];
const TargetNumber = 15;

const result = linearSearch(numbers, TargetNumber);
console.log("Element found at index:", result);
```
```

## Binary Search

Binary search is an efficient searching algorithm applicable to sorted collections. It follows a divide-and-conquer approach by repeatedly dividing the search interval in half until the desired element is found or it can be determined that the element does not exist in the collection.

To perform a binary search, we compare the Target value with the middle element of the collection. If they are equal, the search is successful.

If the Target value is less than the middle element, we continue the search on the left half of the collection. If the Target value is greater, we continue on the right half. This process is repeated until a match is found or the search interval becomes empty.

Example:

``````
function binarySearch(arr, Target) {
let left = 0;
let right = arr.length - 1;

while (left <= right) {
const mid = Math.floor((left + right) / 2);

if (arr[mid] === Target) {
return mid;
}

if (arr[mid] < Target) {
left = mid + 1;
} else {
right = mid - 1;
}
}

return -1;
}

const result = binarySearch(numbers, TargetNumber);
console.log("Element found at index:", result);
```
```

## Hashing

Hashing is a technique that allows us to perform constant-time searching by using a hash function to map keys to indices in an array called a hash table. The hash function converts a key into a unique index where the corresponding value is stored.

To perform a search using hashing, we apply the same hash function to the key and retrieve the value stored at that index in the hash table. If the retrieved value matches our search criteria, we can conclude that the element exists in the collection. Otherwise, it indicates that either there was a collision (two different keys mapped to the same index) or that there is no such element in the collection.

Example:

``````
class HashTable {
constructor() {
this.table = new Array(100);
}

hash(key) {
let hash = 0;
for (let i = 0; i < key.length; i++) {
hash += key.charCodeAt(i);
}
return hash % this.table.length;
}

insert(key, value) {
const index = this.hash(key);
this.table[index] = value;
}

search(key) {
const index = this.hash(key);
return this.table[index];
}
}

const table = new HashTable();
table.insert("apple", "A fruit");
table.insert("banana", "Another fruit");

const result = table.search("apple");
console.log("Result:", result);
```
```

### Conclusion

Searching in data structures is a fundamental operation that allows us to locate specific elements within collections. By understanding different searching algorithms such as linear search, binary search, and hashing, we can choose the most appropriate method based on the characteristics of our data and the efficiency required.

Remember to consider factors such as the size of the dataset, whether it is sorted or not, and the time complexity of each algorithm when selecting a searching technique for a particular problem.