Sei sulla pagina 1di 5

OBJECTIVE: IMPLEMENTING LINEAR SEARCH

DESCRIPTION:
In computer science a linear search or sequential search is a method for finding an element
within a list. It sequentially checks each element of the list until a match is found or the whole
list has been searched.
A linear search runs in at worst linear search and makes at most n comparisons, where n is the
length of the list. If each element is equally likely to be searched, then linear search has an
average case of n/2 comparisons, but the average case can be affected if the search probabilities
for each element vary. Linear search is rarely practical because other search algorithms and
schemes, such as the binary search algorithm and hash tables, allow significantly faster searching
for all but short lists.

Basic algorithm
Given a list L of n elements with values or records L0 .... Ln−1, and target value T, the
following subroutine uses linear search to find the index of the target Tin L.

1. Set i to 0.
2. If Li = T, the search terminates successfully; return i.
3. Increase i by 1.
4. If i < n, go to step 2. Otherwise, the search terminates unsuccessfully.

The time complexity of above algorithm is O(n).

Best case complexity of above algorithm is 1.

PROGRAM:

//made by chinmay jain

#include<stdio.h>

int main()

int a[] = {31,72,65,85,56};

int n = sizeof(a)/sizeof(int);

int i, e=72 , pos =0;

1|Page
CHINMAY JAIN -18100BTCSAII02837
printf("Array:");

for (i=0;i<n;i++)

printf(" %d", a[i]);

for (i=0;i<n;i++)

if(a[i] == e)

pos=i;

break;

if (pos >= 0)

printf("\nElement at %d", pos+1);

else

printf("\nElement not in array");

return 0;}

OUTPUT:

2|Page
CHINMAY JAIN -18100BTCSAII02837
OBJECTIVE:IMPLEMENTING BINARY SEARCH

DESCRIPTION:
In computer science a linear search or sequential search is a method for finding an element
within a list. It sequentially checks each element of the list until a match is found or the whole
list has been searched.
A linear search runs in at worst linear search and makes at most n comparisons, where n is the
length of the list. If each element is equally likely to be searched, then linear search has an
average case of n/2 comparisons, but the average case can be affected if the search probabilities
for each element vary. Linear search is rarely practical because other search algorithms and
schemes, such as the binary search algorithm and hash tables, allow significantly faster searching
for all but short lists. In cs binary search, also known as half-interval search,\logarithmic search,
or binary chop, is a search algo that finds the position of a target value within a sorted
array.[4][5] Binary search compares the target value to the middle element of the array. If they are
not equal, the half in which the target cannot lie is eliminated and the search continues on the
remaining half, again taking the middle element to compare to the target value, and repeating this
until the target value is found. If the search ends with the remaining half being empty, the target
is not in the array. Even though the idea is simple, implementing binary search correctly requires
attention to some subtleties about its exit conditions and midpoint calculation, particularly if the
values in the array are not all of the whole numbers in the range.
Binary search runs in log time in the worst case, making O(log n) comparisons, where n is the
number of elements in the array, the O is big o notation, and log is the log. Binary search takes
constant (O(1)) space, meaning that the space taken by the algorithm is the same for any number
of elements in the array. Binary search is faster than linear search except for small arrays, but the
array must be sorted first. Although specialized data structure designed for fast searching, such
as hash tables, can be searched more efficiently, binary search applies to a wider range of
problems.

The time complexity of above algorithm is O(log n).

Best case complexity of above algorithm is 1.

PROGRAM:

//made by chinmay jain

#include <stdio.h>

int main()

int c, first, last, middle, n, search, array[100];

3|Page
CHINMAY JAIN -18100BTCSAII02837
printf("Enter number of elements\n");

scanf("%d",&n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)

scanf("%d",&array[c]);

printf("Enter value to find\n");

scanf("%d", &search);

first = 0;

last = n - 1;

middle = (first+last)/2;

while (first <= last) {

if (array[middle] < search)

first = middle + 1;

else if (array[middle] == search) {

printf("%d found at location %d.\n", search, middle+1);

break;

else

last = middle - 1;

4|Page
CHINMAY JAIN -18100BTCSAII02837
middle = (first + last)/2;

if (first > last)

printf("Not found! %d isn't present in the list.\n", search);

return 0;

OUTPUT:

5|Page
CHINMAY JAIN -18100BTCSAII02837

Potrebbero piacerti anche