Download presentation
Presentation is loading. Please wait.
Published byLesley Chandler Modified over 6 years ago
1
Search Recall the employee database used in class examples: employeeRecordT workforce[5] = Given "Vance, Emelline", how can we find her salary? Name Empl. ID Dept. Salary Fudge, Cornelius admin Weasley, Percy admin Bones, Amelia legal Vance, Emelline research Doge, Elphias sales
2
Linear Search Obvious answer: Look through all the names until we find a match: int find_salary (employeeRecordT workforce[ ], char *name) { for (int i = 0; i < SIZE; i++) if (strcmp(name, workforce[i].name) ==0 ) return workforce[i].salary; return -1; /* Not found */ }
3
Linear Search Not clever: Better idea:
To find 1 entry, we may have to test all SIZE entries Better idea: Sort first (by name) This will take some time, but if we are planning to do many searches, it will be worth it in the end. Then search more efficiently Think: how would you search the phonebook?
4
Binary Search MAIN IDEA:
Find middle of list Compare target to element at middle Is this it? If yes, we are done. If no, which half has the target? Keep only that half and ignore the other. Repeat until you find the target or discover that it's not in the list. Important: This type of search can only be used on sorted data.
5
Binary Search Target: Core idea: Where is "Logan, James"? Ba Ca Ce Fa
Read middle of list to split it into 2 halves. Where is "Logan, James"? Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na Ne No Nu Pa Ph Qu Ra
6
Binary Search Target: Core idea: Where is "Logan, James"? Ba Ca Ce Fa
Read middle of list to split it into 2 halves. Compare target to middle element: no match Where is "Logan, James"? Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na Ne No Nu Pa Ph Qu Ra
7
Binary Search Target: Core idea: Where is "Logan, James"? Lo < ? La
Read middle of list to split it into 2 halves. Compare target to middle element: no match Which half would "Low, Sampson" be in? Where is "Logan, James"? Lo < ? La Lo > ? La Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na Ne No Nu Pa Ph Qu Ra
8
Binary Search Target: Core idea: Where is "Logan, James"? Lo < ? La
Read middle of list to split it into 2 halves. Compare target to middle element: no match Which half would "Low, Sampson" be in? Keep only that half. Repeat until answer found. Where is "Logan, James"? Lo < ? La Lo > ? La Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na Ne No Nu Pa Ph Qu Ra
9
Binary Search Target: Implementation: Where is "Logan, James"? Ba Ca
At each step, indices left, right define the subarray we are working on. Where is "Logan, James"? Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na Ne No Nu Pa Ph Qu Ra left right left=0, right=19
10
Binary Search Target: Implementation: Where is "Logan, James"? Ba Ca
Find the midpoint: workforce[ (left+right) / 2 ] Where is "Logan, James"? Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na Ne No Nu Pa Ph Qu Ra left right mid left=0, right=19, mid=9
11
Binary Search Target: Implementation: Where is "Logan, James"? Ba Ca
If the data at the midpoint is equal to the target, return the index: if (strcmp(workforce[mid], name) == 0) return mid; But it's not... Where is "Logan, James"? Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na Ne No Nu Pa Ph Qu Ra left right mid
12
Binary Search Where is "Logan, James"? Target: Implementation: Ba Ca
if the data at the midpoint is greater than the target, move right to the left of the midpoint so you can work on the first half: else if (strcmp(workforce[mid], name) > 0) right = mid – 1; But it's not... Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na Ne No Nu Pa Ph Qu Ra left right mid
13
Binary Search Where is "Logan, James"? Target: Implementation: Ba Ca
If the data at the midpoint is less than the target, move left to the right of the midpoint so you can work on the second half. else left = mid + 1; Indeed, "Lo" > "La Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na Ne No Nu Pa Ph Qu Ra left right
14
Binary Search Where is "Logan, James"? Target: Implementation: Ba Ca
Updated left-hand and right-hand indices define the new subarray: Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na Ne No Nu Pa Ph Qu Ra left right left=10, right=19
15
Binary Search Where is "Logan, James"? Target: Implementation: Ba Ca
Repeat: find the midpoint Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na Ne No Nu Pa Ph Qu Ra left mid right left=10, right=19 mid = (10+19)/2
16
Binary Search Where is "Logan, James"? Target: Implementation: Ba Ca
"Lo" < "No", so move right to the left of mid. Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na Ne No Nu Pa Ph Qu Ra left right left=10, right=13
17
Binary Search Where is "Logan, James"? Target: Implementation: Ba Ca
Repeat: find the new midpoint: Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na Ne No Nu Pa Ph Qu Ra left right mid left=10, right=13 mid=(10+13)/2
18
Binary Search Where is "Logan, James"? Target: Implementation: Ba Ca
"Lo" < "Mi", so move the right index to the left of mid. Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na Ne No Nu Pa Ph Qu Ra left right left=10, right=10
19
Binary Search Where is "Logan, James"? Target: Implementation: Ba Ca
Repeat: find the new midpoint Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na Ne No Nu Pa Ph Qu Ra left left=10 right=10 mid=10 right mid
20
Binary Search Where is "Logan, James"? Target: Implementation: Ba Ca
We have a match, return index mid. What if the target is not in the list? How will the loop terminate? Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na Ne No Nu Pa Ph Qu Ra
21
Binary Search Where is "Morgan, Elliot"? Target: Implementation: Ba Ca
The first few steps are the same as before: Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na Ne No Nu Pa Ph Qu Ra left right mid left=10, right=13 mid=(10+13)/2
22
Binary Search Where is "Morgan, Elliot"? Target: Implementation: Ba Ca
"Mo" > "Mi", so move the left index to the right of mid. Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na Ne No Nu Pa Ph Qu Ra right left left=12, right=13
23
Binary Search Where is "Morgan, Elliot"? Target: Implementation: Ba Ca
Repeat: find the midpoint Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na Ne No Nu Pa Ph Qu Ra left right mid left=12, right=13 mid = 12
24
Binary Search Where is "Morgan, Elliot"? Target: Implementation: Ba Ca
"Na" > "Mo" so move the right index to the left of mid. Ba Ca Ce Fa Fe Fo Ga He Ho La Lo Mi Na Ne No Nu Pa Ph Qu Ra left right left=12, right=11
25
Binary Search Where is "Morgan, Elliot"? Target: Implementation:
What happened? The left and right indices have swapped (i.e right is to the left and left is to the right). Now we know that the element cannot possibly be in the list. Return -1 (invalid index) to indicate this Alternatively, we could have checked one step earlier whether left == right and whether the element they refer to matches the target.
26
Why Bother? HUGE speed improvement!
Every iteration cuts list length by half Cost for a list of length 2k is just k iterations. For one search of a list of length N: Linear search cost : N Binary search cost : log2(N) Example: search a 32-million-element list Linear search : up to 32 million iterations (comparisons) Binary search : up to 25 iterations (comparisons)
27
Sorting Sorting = arranging data in some order according to their values Example: arranging a sequence of numbers from largest to smallest arranging a sequence of names in alphabetical order arranging a sequence of records based on a "key" (e.g. the name field or the salary field)
28
Sorting There are several sorting algorithms, some more efficient than others. We will discuss: selection sort insertion sort In all of our examples, we will assume that the items to be sorted are stored in an array we want to sort the items in ascending order
29
Selection Sort Idea: Select item that should be first in order
How? Traverse the array keeping track of the largest element as you go. Swap it with the actual first item in the array. The part of the array containing the first item is now sorted. From now on, ignore it. The rest of the array is still unsorted. Repeat the process on the unsorted part of the array.
30
Selection Sort As we "move" items to the sorted part of the array, this imaginary wall between the parts moves towards the end of the array. When it reaches the edge, we are done!
31
Selection Sort Selection Sort
32
Insertion Sort Idea: Divide the array in two imaginary parts: sorted, unsorted The sorted part is initially empty Pick the first element from the unsorted part and insert it in its correct slot in the sorted part. Do the insertion by traversing the sorted part to find where the element should be placed. Shift the other elements over to make room for it. Repeat the process. This algorithm is very efficient for sorting small lists Example: sorting homeworks by name
33
Insertion Sort As we "move" items to the sorted part of the array, this imaginary wall between the parts moves towards the end of the array. When it reaches the edge, we are done!
34
Insertion Sort
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.