Simple Linear Search Example Program Using Functions in C


Definition:

  • Linear search is also called sequential search
  • Linear search is a method for searching a value within a array. 
  • It sequentially checks one by one of the array for the target element until a match is found or until all the elements have been searched of that array.

Simple Linear Search Example Using functions Program (Sequential search)

/* Simple Linear Search Program Using Functions in C*/
/* Data Structure Programs,C Array Examples */

#include<stdio.h>
#include<conio.h>

#define MAX_SIZE 5

void linear_search(int[], int);

int main() {
  int arr_search[MAX_SIZE], i, element;

  printf("Simple Linear Search Example - Array and Functions\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);

  //Linear Search Function
  linear_search(arr_search, element);

  getch();
}

void linear_search(int fn_arr[], int element) {
  int i;

  /* for : Check elements one by one - Linear */
  for (i = 0; i < MAX_SIZE; i++) {
    /* If for Check element found or not */
    if (fn_arr[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);
}

Sample Output:

Output 1:

Simple Linear Search Example - Array and Functions

Enter 5 Elements for Searching :
900
333
21
16
24
Enter Element to Search : 16
Linear Search : 16 is Found at array : 4.

Output 2:

Simple Linear Search Example - Array and Functions

Enter 5 Elements for Searching :
90
32
323
11
22
Enter Element to Search : 33

Search Element : 33  : Not Found