Linear search is a very simple search algorithm. In this type of search, a sequential search is done for all items one by one. Every item is checked and if a match is found then that particular item is returned, otherwise the search continues till the end of the data collection. The worst case time complexity of linear search is O(n) and the worst case space complexity is O(1).
Linear Search ( Array A, Value x)
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
#include< stdio.h>
void printArray(int[],int);
int main(void)
{
int my_array[] = {0,21,22,50,55,99,100,120,180,200};
int num,i;
int array_size = sizeof(my_array)/sizeof(my_array[0]);
printf("Array: ");
printArray(my_array,array_size);
printf("Enter a number to find its position in the given array: ");
scanf("%d",&num);
for(i=0;i < array_size;i++)
{
if(my_array[i] == num)
{
printf("%d is present in the array at position %d.\n",num,i+1);
break;
}
}
if(i >= array_size)
printf("%d is not present in the array.\n",num);
return 0;
}
void printArray(int arr[],int get_size)
{
int i;
for(i=0;i < get_size;i++)
printf("%d ",arr[i]);
printf("\n");
}
Array: 0 21 22 50 55 99 100 120 180 200
Enter a number to find its position in the given array: 99
99 is present in the array at position 6.
Data Structures
Data Structures Overview Arrays Linked List Stack Queue Priority QueueExpression parsing
Infix Notation Prefix Notation Postfix notation Notation ConversionHashing
Hash Table and Function Collision ResolutionSearching Techniques
Linear Search Binary Search Interpolation Search Fibonacci SearchSorting
Bubble Sort Bubble Sort in Depth Selection Sort Selection Sort in Depth Insertion Sort Insertion Sort in Depth Shell Sort Merge Sort Quick Sort Quick Sort in Depth Counting Sort Radix Sort Bucket SortTree
Trees and Graphs Binary Tree Types of Binary Trees Binary Search Tree N-ary Tree AVL Tree Red Black Tree Splay tree B tree B+ treeTree Search Traversal Techniques
Breadth First Search (BFS) Depth First Search (DFS) Inorder, Preorder and Postorder TraversalHeaps
Heaps Introduction Binary Heap Heap OperationsTrending
C Programming
Remember to follow community guidelines and don't go off topic.