An array is a data structure that contains a sequence of items of the same type and fixed length. It is commonly employed for managing data collections due to its efficient access through indexing.
Ex: intnumbers = {10, 20, 30, 40, 50};
Searching an Element in an Array
A common task in software development involves searching for a specific item within an array. Enhancing the performance of your code can be significantly enhanced by employing effective search algorithms, whether you are checking for the presence of a specific value, finding the position of an element, or confirming its existence. This article will delve into various techniques for searching elements in an array using the C programming language.
There are primarily two methods to Locate an Element in an Array:
1. Linear Search
A direct search technique employed to find a specific element within an array or list is known as linear search, also sometimes called sequential search. This method involves comparing each element in the array to the desired value in order to identify a match or iterate through the entire array systematically.
The fundamental steps in linear search are as follows:
- Start with the array's topmost elements.
- The target value should be compared to the current element.
- The search is successful if the current element matches the requested value, and then the algorithm can return the element's index or any other desired output.
- Go to the following element in the array if the current element does not match the desired value.
- Until a match is made or the end of the array is reached, repeat steps 2-4.
Program:
#include <stdio.h>
int linearSearch(int arr[], int n, int target) {
for (int i = 0; i< n; i++) {
if (arr[i] == target) {
return i; // Return the index if the target is found
}
}
return -1; // Return -1 if the target is not found
}
int main() {
int arr[] = {5, 2, 8, 12, 3};
int n = sizeof(arr) / sizeof(arr[0]); // Calculate the number of elements in the array
int target = 8;
int result = linearSearch(arr, n, target);
if (result == -1) {
printf("Element not found\n");
} else {
printf("Element found at index %d\n", result);
}
return 0;
}
Output:
An element found at index 2
2. Binary Search
The binary search algorithm is employed to efficiently find a particular item within a sorted array or list. It employs a divide-and-conquer approach, iteratively reducing the search space by half until the desired element is identified or confirmed as not present.
This is how binary search functions:
- Have a sorted array or list as a base.
- Establish two pointers, left and right , with their initial values pointing to the array's first and end members.
- Use (left + right) / 2 to get the index of the center element.
- Compare the target value to the middle element. The search is successful if they are equal, and then the program can return the index or any other required result. The righLogic Practiceer should be moved to the element preceding the middle element if the middle element is greater than the target value. Move the lefLogic Practiceer to the element following the middle element if the middle element's value is less than the target value.
- Steps 3 and 4 should be repeated until the target element is located or the lefLogic Practiceer exceeds the righLogic Practiceer.
- The desired element is not in the array if it cannot be located.
- The search is successful if they are equal, and then the program can return the index or any other required result.
- The righLogic Practiceer should be moved to the element preceding the middle element if the middle element is greater than the target value.
- Move the lefLogic Practiceer to the element following the middle element if the middle element's value is less than the target value.
Program:
#include <stdio.h>
int binarySearch(int arr[], int left, int right, int target) {
while (left <= right) {
int mid = left + (right-left) / 2;
if (arr[mid] == target) {
return mid; // Return the index if the target is found
}
if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid-1;
}
}
return -1; // Return -1 if the target is not found
}
int main() {
int arr[] = {2, 5, 8, 12, 20, 23, 28};
int n = sizeof(arr) / sizeof(arr[0]); // Calculate the number of elements in the array
int target = 20;
int result = binarySearch(arr, 0, n - 1, target);
if (result == -1) {
printf("Element not found\n");
} else {
printf("Element found at index %d\n", result);
}
return 0;
}
Output:
An element found at index 4