Sequential Search Program in C

Sequential Search Program in C. The technique of identifying a certain value position in a list of values is known as searching. It determines whether or not a search key exists in the data. It’s the mathematical technique of locating a certain object inside a set of items. It can be performed on both internal and external data structures.

#include <stdio.h>
int main()
{
int arr[50], search, cnt, num;
printf("Enter the numbers\n");
scanf("%d",&num);
printf("Enter %d integer(s)\n", num);
for (cnt = 0; cnt < num; cnt++)
scanf("%d", &arr[cnt]);
printf("Enter the number to search\n");
scanf("%d", &search);
for (cnt = 0; cnt < num; cnt++)
{
if (arr[cnt] == search)
{
printf("%d is present at location %d.\n",search,cnt+1);
break;
}
}
if (cnt == num)
printf("%d is not present in array.\n",search);
return 0;
}

 

Output:

Enter the numbers
3
Enter 3 integer(s)
34
56
78
Enter the number to search
34
34 is present at location 1.

Searching Algorithm in C

There are two types of search algorithms: linear search and binary search. Linear search is simple and has a complexity of O(n), whereas binary search is fast and has a complexity of (logn), but it can only be used with a sorted list of elements. 

Binary Search Algorithm in C

Binary search is a fast algorithm for finding a single item in a sorted list of items. It works by repeatedly dividing the portion of the list that could contain the item in half until you’ve reduced the number of possible locations to just one.

Linear Search Algorithm in C

A linear search, also known as a sequential search, is a technique for locating an element within a list. It iteratively checks each element of the list until a match is found or the entire list has been searched. A linear search is the most basic way to search a data set. Each item of data is examined until a match is found, beginning at the beginning of the data set. The search is over once the item is located. If no match is found, the algorithm must deal with it.

Count to Execute Sequential Search Program in C

Counting is used to find how many numbers or items there are in a list. Counting can be used to keep track of how many iterations your program has gone through in a loop.