L12 Searching Techniques (Student)

Embed Size (px)

Citation preview

  • 8/2/2019 L12 Searching Techniques (Student)

    1/20

    Temasek Polytechnic School of Informatics & IT

    L12 Searching Techniques

    CGE2C10 Data Structures and Algorithms

  • 8/2/2019 L12 Searching Techniques (Student)

    2/20

    Temasek Polytechnic School of Informatics & IT 2

    Quote of the day

    Actions lie louder than words.Carolyn Wells

  • 8/2/2019 L12 Searching Techniques (Student)

    3/20

    Temasek Polytechnic School of Informatics & IT 3

    Learning Objectives

    At the end of this lesson, you should beable to

    describe the steps of a sequential search

    do a simulation by hand of a sequential search

    describe the steps of a binary search

    do a simulation by hand of a binary search

  • 8/2/2019 L12 Searching Techniques (Student)

    4/20

  • 8/2/2019 L12 Searching Techniques (Student)

    5/20

    Temasek Polytechnic School of Informatics & IT 5

    Sequential Search

    Also known aslinear searchorserialsearch.

    How does it perform the search?

    Step through the elements one at a timelooking for the desired element.

    Stop

    when the element is found or

    when the search has examined all elements andcould not find the desired element

  • 8/2/2019 L12 Searching Techniques (Student)

    6/20

    Temasek Polytechnic School of Informatics & IT 6

    Sequential Search Example

    Search for the value 5 in the array, num:

    Start the search at the first elementnum[0]:

    4 8 3 5 9 10

    0 1 2 3 4 5

    num

    4 8 3 5 9 10

    0 1 2 3 4 5

    num

  • 8/2/2019 L12 Searching Techniques (Student)

    7/20Temasek Polytechnic School of Informatics & IT 7

    Sequential Search Example

    Continue with the next element, num[1]:

    4 8 3 5 9 10

    0 1 2 3 4 5

    num

    4 8 3 5 9 10

    0 1 2 3 4 5

    num

    4 8 3 5 9 10num

    0 1 2 3 4 5

    5 found!

  • 8/2/2019 L12 Searching Techniques (Student)

    8/20

    Temasek Polytechnic School of Informatics & IT 8

    Sequential Search - Algorithm

    What do we need to step through eachelement in the array?

    What should the search method take asinput?

    A loop (for, while, or do-while)

    (1) The array, (2) item to be search

  • 8/2/2019 L12 Searching Techniques (Student)

    9/20

  • 8/2/2019 L12 Searching Techniques (Student)

    10/20

    Temasek Polytechnic School of Informatics & IT 10

    Binary Search

    Method: The elements are stored in a sorted array.

    Divide and Conquer.

    Algorithm:look at the middle element in the array

    if it matches n, then finished

    else

    if n is less than the middle element,

    search on the elements left to the middle

    if n is greater than the middle element,

    search on the elements right to the middle

  • 8/2/2019 L12 Searching Techniques (Student)

    11/20

    Temasek Polytechnic School of Informatics & IT 11

    Binary Search Example I

    Looking for the value 3 in the following array. Weuse high and low to mark the extreme ends ofthe array and mid to designate the middleelement.

    1 3 4 6 7 10 13 15 16 190 1 2 3 4 5 6 7 8 9

    low mid high

    Is 3 ==num[mid](7) ?

    num

    (mid = (low + high) / 2)

  • 8/2/2019 L12 Searching Techniques (Student)

    12/20

    Temasek Polytechnic School of Informatics & IT 12

    Binary Search Example I

    3 is not equal to 7, 3 is less than 7, move high to mid -1

    1 3 4 6 7 10 13 15 16 19

    0 1 2 3 4 5 6 7 8 9

    low highmid

    num Is 3 ==num[mid](3) ?

  • 8/2/2019 L12 Searching Techniques (Student)

    13/20

    Temasek Polytechnic School of Informatics & IT 13

    Binary Search Example I

    3 is equal to 3, found it!

    1 3 4 6 7 10 13 15 16 19

    0 1 2 3 4 5 6 7 8 9

    lowhighmid

    Found 3!

    num

  • 8/2/2019 L12 Searching Techniques (Student)

    14/20

    Temasek Polytechnic School of Informatics & IT 14

    Binary Search Example II

    Looking for the value 5 in the following array. Weuse high and low to mark the extreme ends of thearray and mid to designate the middle element.

    1 3 4 6 7 10 13 15 16 190 1 2 3 4 5 6 7 8 9

    low mid high

    num Is 5 ==num[mid](7) ?

    (mid = (low + high) / 2)

  • 8/2/2019 L12 Searching Techniques (Student)

    15/20

    Temasek Polytechnic School of Informatics & IT 15

    Binary Search Example II

    5 is not equal to 7, 5 is less than 7, move high tomid-1

    1 3 4 6 7 10 13 15 16 19

    0 1 2 3 4 5 6 7 8 9

    low highmid

    numIs 5 ==num[mid](3) ?

  • 8/2/2019 L12 Searching Techniques (Student)

    16/20

    Temasek Polytechnic School of Informatics & IT 16

    Binary Search Example II

    5 is not equal to 3, 5 is greater than 3, move low tomid+1

    1 3 4 6 7 10 13 15 16 19

    0 1 2 3 4 5 6 7 8 9

    low mid high

    num Is 5 ==num[mid](4) ?

  • 8/2/2019 L12 Searching Techniques (Student)

    17/20

    Temasek Polytechnic School of Informatics & IT 17

    Binary Search Example II

    5 is not equal to 4, 5 is greater than 4, move low to

    mid+1

    1 3 4 6 7 10 13 15 16 19

    0 1 2 3 4 5 6 7 8 9

    mid

    low high

    num Is 5 ==num[mid](6) ?

  • 8/2/2019 L12 Searching Techniques (Student)

    18/20

    Temasek Polytechnic School of Informatics & IT 18

    Binary Search Example II

    5 is not equal to 6, 5 is less than 4, move high tomid-1, high is less than mid, terminate search.

    1 3 4 6 7 10 13 15 16 19

    0 1 2 3 4 5 6 7 8 9

    mid

    lowhigh

    num

    Not found!

  • 8/2/2019 L12 Searching Techniques (Student)

    19/20

    Temasek Polytechnic School of Informatics & IT 19

    Quiz

    What are the 2 search techniquesintroduced?

    What programming construct is used toimplement sequential search?

    When is sequential search inefficient?

  • 8/2/2019 L12 Searching Techniques (Student)

    20/20

    Temasek Polytechnic School of Informatics & IT 20

    References

    [Lafore 2002] Robert Lafore, Data Structures and Algorithms inJava