Search for an Element in an Array
To search for an element in an array using C, we can implement a linear search. This method involves iterating through each element of the array and comparing it with the target value. If a match is found, we return the index of the element; otherwise, we indicate that the element is not present in the array.
Examples of Searching an Element in an Array
1. Basic Linear Search in an Integer Array
In this example, we will implement a simple linear search to find an element in an integer array. If the element is found, we print its index; otherwise, we indicate that it is not present.
main.c
#include <stdio.h>
int linearSearch(int arr[], int size, int key) {
for (int i = 0; i < size; i++) {
if (arr[i] == key) {
return i; // Return the index if found
}
}
return -1; // Return -1 if not found
}
int main() {
int numbers[] = {10, 20, 30, 40, 50};
int size = sizeof(numbers) / sizeof(numbers[0]);
int key = 30;
int result = linearSearch(numbers, size, key);
if (result != -1) {
printf("Element %d found at index %d\n", key, result);
} else {
printf("Element %d not found in the array\n", key);
}
return 0;
}
Explanation:
- We define a function
linearSearch()
that takes an array, its size, and the element to search (key
) as arguments. - We use a
for
loop to iterate through the array and compare each element withkey
. - If a match is found, we return the index of the matching element.
- If the loop completes without finding the element, we return
-1
to indicate that the element is not in the array. - In the
main()
function, we define an integer array and a key to search for. - We call
linearSearch()
and store the result. - If the result is not
-1
, we print the index; otherwise, we indicate that the element is not found.
Output:
Element 30 found at index 2
2. Linear Search in a Character Array
In this example, we will implement a linear search to find a character in an array of characters (string). If the character is found, we print its position; otherwise, we indicate that it is not present.
main.c
#include <stdio.h>
int linearSearchChar(char arr[], int size, char key) {
for (int i = 0; i < size; i++) {
if (arr[i] == key) {
return i; // Return index if found
}
}
return -1; // Return -1 if not found
}
int main() {
char letters[] = {'a', 'b', 'c', 'd', 'e', 'f'};
int size = sizeof(letters) / sizeof(letters[0]);
char key = 'd';
int result = linearSearchChar(letters, size, key);
if (result != -1) {
printf("Character '%c' found at index %d\n", key, result);
} else {
printf("Character '%c' not found in the array\n", key);
}
return 0;
}
Explanation:
- We define a function
linearSearchChar()
that takes a character array, its size, and a character key as input. - We iterate through the array using a
for
loop and compare each character with the key. - If a match is found, we return the index of the character.
- If the loop completes without finding the character, we return
-1
. - In
main()
, we declare a character array containing lowercase letters. - We call
linearSearchChar()
with the array and the character to search. - Based on the return value, we print whether the character is found or not.
Output:
Character 'd' found at index 3
Conclusion
In this tutorial, we learned how to perform a linear search in C using different types of arrays:
- Integer Array: We searched for an integer value using a loop.
- Character Array: We applied the same logic to find a character in an array.
The linear search algorithm is simple and effective for small datasets. However, for large datasets, more efficient search algorithms like binary search
(for sorted arrays) should be considered.