BCS2122DCS2192 Data Structure and Algorithm - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

BCS2122DCS2192 Data Structure and Algorithm

Description:

If the key sought is not the same as the middle element, it determines whether ... If it is in the first half, then the last half can be dropped. ... – PowerPoint PPT presentation

Number of Views:16
Avg rating:3.0/5.0
Slides: 13
Provided by: notesU
Category:

less

Transcript and Presenter's Notes

Title: BCS2122DCS2192 Data Structure and Algorithm


1
BCS2122/DCS2192Data Structure and Algorithm
  • Module 7 Searching
  • Muhammed Ramiza Ramli

2
Objectives
  • This chapter discusses the following topics
  • Linear sequential search
  • Ordered sequential search
  • Binary search
  • Binary search tree

3
Search Algorithms
  • The most important operation on data processing
    is the search algorithm
  • Using a search algorithm you can
  • Determine whether a particular item is in the
    list
  • If the data are specially organized, find the
    location where a new item can be inserted
  • Find the location of an item to be deleted

4
Search Algorithms
  • The search algorithms performance is crucial
  • If the search is slow, it takes a large amount of
    computer time to accomplish your task
  • If the search is fast you can accomplish your
    task quickly
  • A key (or primary key) is used to facilitate
    searching.

5
Linear Sequential Search
  • The simplest form of search.
  • Items in a list are searched one by one starting
    from the first and compared to a given key value.
  • The search ends when the key value matches with
    the given item.
  • This technique generally works well for a small
    list or when the list is accessed infrequently.

6
Linear Sequential Search Algorithm
  • Consider array A with n elements and the search
    key KEYFIELD. The algorithm
  • 1. Compare the search key KEYFIELD with the first
    element in array. If match is found, then search
    is successful.
  • 2. If no match is found, try to match the next
    element in the array.
  • 3. Repeat step 2 until a match is found. If a
    match is found, it will return the index of the
    element found.

7
Ordered Sequential Search
  • Improved the linear sequential search.
  • Do not need to search the entire list to
    determine if the item is present.
  • A search can be terminated when the data values
    in the remaining list are less than the search
    key.
  • This technique works fine for small lists.

8
Ordered Sequential Search Algorithm
  • Consider array A with n elements and the search
    key KEYFIELD. The algorithm
  • 1. Compare the search key KEYFIELD with the
    largest element in the list. If a match is found,
    the search is successful.
  • 2. If no match is found, determine whether the
    remaining data items are less than the search key
  • If the remaining data items are less than the
    KEYFIELD, terminate the search.
  • Else if not, reduce the size of array for
    searching.
  • Repeat step 2 for different smaller list until a
    match is found. If a match is found, it will
    return the index of the element found. If no
    match is found, terminate the loop.

9
Binary Search
  • The binary search algorithm searches the list by
    first comparing key sought with the middle
    element in the list.
  • If the key sought is not the same as the middle
    element, it determines whether the key sought is
    in the upper half (first half) or in the lower
    half (last half) of the list.

10
Binary Search
  • If it is in the first half, then the last half
    can be dropped.
  • Likewise, if the key sought is in the last half,
    the first half can be dropped.
  • The elimination of either the first half or the
    last half depends on the value of the middle
    element.
  • The process is continued until the target
    elements is found or reduced search list range
    becomes zero.

11
Binary Search Algorithm
  • Given the sorted array A with n elements and the
    search key KEYFIELD. The algorithm
  • 1. Compare the search key KEYFIELD with the
    middle element in the array. The position of the
    middle element is computed from (first last)/2.
    If the match is found, the search is successful.

12
Binary Search Algorithm cont.
  • 2. If no match is found, determine whether the
    KEYFIELD is less or more than the middle value.
  • 2.1 If the key value is less than the middle
    value, search the sublist from the first element
    to the element just before the middle value.
    (position middle 1)
  • 2.2 If the key value is greater than the middle
    value, search the sublist from position middle1
    to the last position.
  • 3. Repeat step 2 for smaller sublist until a
    match is found. If a match is found, it will
    return the index of the element found. If no
    match found, it will terminates the loop.
Write a Comment
User Comments (0)
About PowerShow.com