Sequential search analysis for a list with n items

  • No School
  • AA 1
  • 32

This preview shows page 8 - 19 out of 32 pages.

Sequential Search Analysis For a list with n items, the best case is when the value is equal to the first element of the list, in which case only one comparison is needed. The worst case is when the value is not in the list (or occurs only once at the end of the list), in which case n comparisons are needed.
Image of page 8
Sequential - Pseudocode Forward iteration This Pseudocode describes a typical variant of linear search, where the result of the search is supposed to be either the location of the list item where the desired value was found; or an invalid location Λ, to indicate that the desired element does not occur in the list. For each item in the list: if that item has the desired value, stop the search and return the item's location. Return Λ .
Image of page 9
Sequential Search - Pseudocode In this pseudocode, the last line is executed only after all list items have been examined with none matching. If the list is stored as an array data structure, the location may be the index of the item found (usually between 1 and n, or 0 and n−1). In that case the invalid location Λ can be any index before the first element (such as 0 or −1, respectively) or after the last one (n+1 or n, respectively). If the list is a simply linked list, then the item's location is its reference, and Λ is usually the null pointer.
Image of page 10
Image of page 11
Algorithm – Sequential Search
Image of page 12
Analysis – Sequential Search
Image of page 13
Analysis – Sequential Search
Image of page 14
Sequential Search Program int binarySearch (int list[], int size, int key) { int first = 0, last , mid, position = -1; last = size - 1 int found = 0; while (!found && first <= last) { middle = (first + last) / 2; /* Calculate mid point */ if (list[mid] == key) { /* If value is found at mid */ found = 1; position = mid; } else if (list[mid] > key) /* If value is in lower half */ last = mid - 1; else first = mid + 1; /* If value is in upper half */ } // end while loop return position; } // end of function
Image of page 15
Sequential Search
Image of page 16
Image of page 17
Brute-Force String Matching Given a string of n
Image of page 18
Image of page 19

You've reached the end of your free preview.

Want to read all 32 pages?

  • Fall '19

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

Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes