Linear Search in C (Algorithm, Pseudocode and output)
Linear search is the basic Search Algorithm used in data structures. It is also called as sequential search. Linear search is used to find a particular element in an array.
How it Works
A linear search scans one item at a time, without jumping to any item .
- The worst case complexity is O(n), sometimes known an O(n) search
- Time taken to search elements keep increasing as the number of elements are increased.
Algorithm
Linear Search ( Array A, Value x) Step 1: Set i to 1 Step 2: if i > n then go to step 7 Step 3: if A[i] = x then go to step 6 Step 4: Set i to i + 1 Step 5: Go to Step 2 Step 6: Print Element x Found at index i and go to step 8 Step 7: Print element not found Step 8: Exit
Pseudocode
procedure linear_search (list, value) for each item in the list if match item == value return the item's location end if end for end procedure
Linear Search Program
#include
#define MAX 20
// array of items on which linear search will be conducted.
int intArray[MAX] = {1,2,3,4,6,7,9,11,12,14,15,16,17,19,33,34,43,45,55,66};
void printline(int count) {
int i;
for(i = 0;i <count-1;i++) {
printf("=");
}
printf("=\n");
}
// this method makes a linear search.
int find(int data) {
int comparisons = 0;
int index = -1;
int i;
// navigate through all items
for(i = 0;i<MAX;i++) {
// count the comparisons made
comparisons++;
// if data found, break the loop
if(data == intArray[i]) {
index = i;
break;
}
}
printf("Total comparisons made: %d", comparisons);
return index;
}
void display() {
int i;
printf("[");
// navigate through all items
for(i = 0;i<MAX;i++) {
printf("%d ",intArray[i]);
}
printf("]\n");
}
void main() {
printf("Input Array: ");
display();
printline(50);
//find location of 1
int location = find(55);
// if element was found
if(location != -1)
printf("\nElement found at location: %d" ,(location+1));
else
printf("Element not found.");
}
If we compile and run the above program, it will produce the following result −
Output
Input Array: [1 2 3 4 6 7 9 11 12 14 15 16 17 19 33 34 43 45 55 66 ]
==================================================
Total comparisons made: 19
Element found at location: 19
Linear Search in C
Here you will find another practice program for linear search in C.
Output
How many elements?4
Enter array elements:
6 8 9 1
Enter array elements:
6 8 9 1
Enter element to search:9
Element found at index 2
Element found at index 2
Applications :
Linear search is usually very simple to implement, and is practical when the list has only a few elements, or when performing a single search in an unordered list.
But when many values have to be searched in the same list, it often pays to pre-process the list in order to use a faster method.
For an example, one may sort the list and use binary search, or build an efficient search data structure from it. Should the content of the list change frequently ?
. The repeated re-organization may be more trouble than it is worth.
As a result, even though in theory other search algorithms may be faster than linear search (for instance binary search), in practice even on medium-sized arrays (around 100 items or less) it might be infeasible to use anything else.
On larger arrays, it only makes sense to use other, faster search methods if the data is large enough, because the initial time to prepare (sort) the data is comparable to many linear searches where the Binary search comes into play which we will be discussing in the next Searching Tutorial so stay Tuned for more!!!
No comments
If you have any doubts, please let us Know.