Linear Search C
Linear Search C
h>
// Elements to search
int target1 = 45;
int target2 = 55;
// Print results
if (result1 != -1) {
printf("%d found at index %d\n", target1, result1);
} else {
printf("%d not found in the array\n", target1);
}
if (result2 != -1) {
printf("%d found at index %d\n", target2, result2);
} else {
printf("%d not found in the array\n", target2);
}
return 0;
}
/*
Time Complexity:
- Best Case: O(1) - when the target is the first element
- Worst Case: O(n) - when the target is the last element or not present
- Average Case: O(n)