
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
C/C++ Program for Linear Search?
In linear search algorithm, we compare targeted element with each element of the array. If the element is found then its position is displayed.
The worst case time complexity for linear search is O(n).
Input: arr[] = { 12, 35, 69, 74, 165, 54} Sea=165 Output: 165 is present at location 5.
Explanation
linear search (Searching algorithm) which is used to find whether a given number is present in an array and if it is present then at what location it occurs. It is also known as sequential search. It is straightforward and works as follows: We keep on comparing each element with the element to search until it is found or the list ends.
Example
#include <iostream> using namespace std; int main() { int sea, c, n=6; int arr[] = { 12, 35, 69, 74, 165, 54}; sea=165; for (c = 0; c < n; c++) { if (arr[c] == sea) { printf("%d is present at location %d.\n", search, c+1); break; } } if (c == n) printf("%d isn't present in the array.\n", search); return 0; }
- Related Articles
- Python Program for Linear Search
- Write a program for Linear Search in Python
- Linear Search in Python Program
- Java program to implement linear search
- 8085 Program to perform linear search
- Linear Search
- C Program for Anagram Substring Search
- Program to perform linear search in 8085 Microprocessor
- C++ Program to Find Minimum Element in an Array using Linear Search
- Linear search using Multi-threading in C
- Difference Between Linear Search and Binary Search
- Linear search in Java.
- C Program for Binary Search (Recursive and Iterative)?
- C++ Program to Search for an Element in a Binary Search Tree
- C++ Program to Implement a Binary Search Algorithm for a Specific Search Sequence

Advertisements