TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Linear search
Kaushik S
Apr 01
2016
Code
708
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
//Linear Search
//Time complexity :Worst Case O(n)
//Best Case :O(1)
#include <stdio.h>
int
Search(
int
arr[],
int
n,
int
search);
int
main()
{
int
n, search, result, m = 0, arr[100];
printf(
"Program on Linear Search\n"
);
printf(
"-------\n"
);
printf(
"Enter the total elements in the array\n"
);
scanf(
"%d"
, &n);
printf(
"Enter the array elements\n"
);
for
(
int
i = 0; i < n; i++)
{
scanf(
"%d"
, &arr[i]);
}
printf(
"Check whether these are the elements which you have entered\n"
);
for
(
int
i = 0; i < n; i++)
{
printf(
"%d\n"
, arr[i]);
}
printf(
"Enter the element to search \n"
);
scanf(
"%d"
, &search);
result = Search(arr, n, search);
if
(result != 0)
{
printf(
"Element found at pos %d\n "
, result);
}
else
{
printf(
"Element not found"
);
}
return
0;
}
int
Search(
int
arr[],
int
n,
int
search)
{
int
arrpos = 0;
for
(
int
i = 0; i < n; i++)
{
if
(arr[i] == search)
{
arrpos = i + 1;
break
;
}
}
return
arrpos;
}
Download link:https://github.com/kaushiks90/SearchingAlogithms
C
Linear search