Lectures 9 and 10

Embed Size (px)

Citation preview

  • 7/30/2019 Lectures 9 and 10

    1/16

    Searching

    Ref.: D.S. Malik, Data Structures

    Using C++

  • 7/30/2019 Lectures 9 and 10

    2/16

    Searching

    n size of the list

    f(n) the number of comparisons done by the

    search algorithm

  • 7/30/2019 Lectures 9 and 10

    3/16

    Array-Based Lists

    search for 27

    compare 27 with list[0] X

    compare 27 with list[1] X

    compare 27 with list[2]

    35

    [0] [1] [2] [3] [4] [5] [6] [7]

    12 27 18 45 16 38 list

  • 7/30/2019 Lectures 9 and 10

    4/16

    Array-Based Lists

    search for 10

    compare 10 with list[0] X

    keep going until there is no more data

  • 7/30/2019 Lectures 9 and 10

    5/16

    Program fragment to find where item

    is in the listint loc;

    bool found = false;

    for (loc = 0; loc < length; loc++)

    if (list[loc] == item)

    { found = true;

    break;

    }

    if found

    answer = loc;else

    answer = -1;

  • 7/30/2019 Lectures 9 and 10

    6/16

    Unordered Linked Lists Program

    fragment to see if item is in the list

    nodeType *current;

    bool found = false;

    current = first;

    while (current != NULL && !found)

    if (current->info == item)

    found = true;

    else

    current = current->link;

  • 7/30/2019 Lectures 9 and 10

    7/16

    Ordered Linked Lists Program

    fragment to see if item is in the list

    nodeType *current;

    bool found = false;

    current = first;

    while (current != NULL && !found)

    if (current->info >= item)

    found = true;

    else

    current = current->link;if (found)

    found = (current->info == item);

  • 7/30/2019 Lectures 9 and 10

    8/16

    Doubly Linked Lists

    e.g.

    first

    last

    struct nodeType

    {

    Type info;

    nodeType *next;

    nodeType *back;

    };

  • 7/30/2019 Lectures 9 and 10

    9/16

    Program fragment to see if item is in

    an ordered list

    nodeType *current;

    bool found = false;

    current = first;

    while (current != NULL && !found)

    if (current->info >= item)

    found = true;

    else

    current = current->next;if (found)

    found = (current->info == item);

  • 7/30/2019 Lectures 9 and 10

    10/16

    Binary Search

    Here is a sorted list of 12 numbers:

    4

    [0] [1] [2] [3] [4] [5] [6] [7]

    8 19 25 34 39 45 48list

    [8] [9] [10] [11]

    66 75 89 95

    We want to find whether 75 is in the list.

  • 7/30/2019 Lectures 9 and 10

    11/16

    Binary Search

    First, a number in the middle of the whole list:

    4

    [0] [1] [2] [3] [4] [5] [6] [7]

    8 19 25 34 39 45 48list

    [8] [9] [10] [11]

    66 75 89 95

    list[5] 75

    list[5] < 75, therefore, restrict the search tolist[6] list[11]

    Continue in this way until we find 75.

  • 7/30/2019 Lectures 9 and 10

    12/16

    int first = 0;

    int last = length -1;

    int mid;

    bool found = false;

    while (first item)

    last = mid -1

    else

    first = mid + 1;

    }if (found)

    answer = mid;

    else

    answer = -1;

  • 7/30/2019 Lectures 9 and 10

    13/16

    Binary Search

    Searching for 89:

    4

    [0] [1] [2] [3] [4] [5] [6] [7]

    8 19 25 34 39 45 48list

    [8] [9] [10] [11]

    66 75 89 95

    Iteration first last mid list[mid]

    1 0 11 5 39

    Student exercise: Complete the table until 89

    is found.

  • 7/30/2019 Lectures 9 and 10

    14/16

    Binary Search

    Student exercises:

    1. Create a new table, and search for 34.

    2. Create a new table, and search for 22.

  • 7/30/2019 Lectures 9 and 10

    15/16

    Binary Search

    In the following slide is the definition ofclass

    orderedArrayListType that implements the

    search algorithms for array-based lists.

    Important student exercise: Try to understand

    every part of this code.

  • 7/30/2019 Lectures 9 and 10

    16/16

    Binary Search

    template

    class orderedArrayListType: publicarrayListType

    {

    public:

    int binarySearch(const elemType& item);

    orderedArrayListType(int n = 100);

    };