Simple Linear Search Example Program in C
Definition:
- Linear search is also called sequential search
- Linear search is a method for searching a value within an array.
- It sequentially checks one by one of the arrays for the target element until a match is found or until all the elements have been searched of that array.
Why is the linear search also called sequential search?
- Linear search sequentially checks one by one of the collection(from 0th position to end) for the search element. So It is called sequential search.
Simple Linear Search Example Program (Sequential search)
/* Simple Linear Search Program in C*/
/* Data Structure Programs,C Array Examples */
#include<stdio.h>
#include<conio.h>
#define MAX_SIZE 5
int main() {
int arr_search[MAX_SIZE], i, element;
printf("Simple Linear Search Example - Array\n");
printf("\nEnter %d Elements for Searching : \n", MAX_SIZE);
for (i = 0; i < MAX_SIZE; i++)
scanf("%d", &arr_search[i]);
printf("Enter Element to Search : ");
scanf("%d", &element);
/* for : Check elements one by one - Linear */
for (i = 0; i < MAX_SIZE; i++) {
/* If for Check element found or not */
if (arr_search[i] == element) {
printf("Linear Search : %d is Found at array : %d.\n", element, i + 1);
break;
}
}
if (i == MAX_SIZE)
printf("\nSearch Element : %d : Not Found \n", element);
getch();
}
Sample Output:
Simple Linear Search Example - Array
Enter 5 Elements for Searching :
500
400
300
200
111
Enter Element to Search : 200
Linear Search : 200 is Found at array : 4.
Read More Articles
- Use of getch(),getche() and getchar() in C
- Switch Case Statement Example Program In C Programming Language
- C Character Set
- Convert a Floating-point value to an Integer in C
- Data Input and Output gets and puts Example Program In C
- Special Operators In C
- Pointer Representation and Pointer Example Programs
- C Data Input and Data Output
- Simple While Loop Example Program In C Programming Language
- Data Output printf and putchar Example Program In C
- C Introduction
- C Operators
- Storage Classes In C
- C Pointers
- File Management
- C Identifiers
- Loop Control Statements
- Hello World - Simple C Program
- C Array
- Single Character Output Function : putchar()
- C Reserve Words
- C Specific Properties and Implementation
- If else Statement Example Program In C Programming Language
- If Statement Example Program In C Programming Language
- Confusing Array in C ( Array Representation and Initialization )