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

Info icon This 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.
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.
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 ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern