algorithmFall09

algorithmFall09 - Algorithms An algorithm is a sequence of...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Algorithms An algorithm is a sequence of computational steps that transform some input into some output. It is a tool for solving a well-defined 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.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
O-notation -- 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.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 6

algorithmFall09 - Algorithms An algorithm is a sequence of...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online