Algorithms
An algorithm
is a sequence of computational steps that transform some input into some output.
It is a tool for solving a welldefined computational problem.
Examples of such problems  searching, sorting
Searching
Linear Search Algorithm
 looks sequentially at each element of a given collection of elements until we either come to the end of the sequence of elements
or find an item that equals to X (the element that we are searching for).
public int linearSearch(int[] A, int x)
{
constant time to execute
times to execute
for (int i=0; i<A.length, i++)
C1
n+1 times
{
if (A[i] = = x )
C2
n times
return i;
C3
0 or 1 time
}
return (–1);
// returns –1 when the element is not found
C4
1 or 0 time
}
The complexity of Linear Search Algorithm is order of
n
(
O(n)
), where
n
is the number of elements.
The running time T(n) is bounded by
C1*(n+1) + C2*n + C3 + C4 = (C1+C2)*n + (C1+C3+C4), it is a linear function of n.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentOnotation
 upper bound
The (worst case) running time of the linear search can be expressed as a function of n, where n is an input size.
T(n) = c1 * n + c2
where c1 and c2 are positive constants.
Intuitively, we can see that when n, input size, gets larger, the time that takes to execute the linear search grows.
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '08
 Matasu
 Algorithms, Sort, Big O notation, Divide and conquer algorithm, selection sort algorithm

Click to edit the document details