L11 Sorting&Searching
L11 Sorting&Searching
THREADED TREES.
✘ Binary tree traversal algorithms are written using either recursion or
programmer-written stacks. If the tree must be traversed frequently,
using stacks rather than recursion may be more efficient.
✘ A third alternative is a threaded tree. In a threaded tree, null pointers are
replaced with pointers to their successor nodes.
✘ To build a threaded tree ----> first build a standard binary search tree.
✘ Then traverse the tree, changing the null right pointers to point to their
successors.
✘ The traversal for a threaded tree is straightforward. Once you locate the
far-left node, loop happens, following the thread (the right pointer) to the
next node. No recursion or stack is needed. When you find a null thread
(right pointer), the traversal is complete.
THREADED TREES. Inorder traversal (LNR)
1 3 5 6 7 8 9 11 13
Internal External
THREE INTERNAL SORTS.
Selection sort
Shell sort
Bubble sort
SORT ORDER.
Data may be sorted in either ascending or descending
sequence.
input
order
output
SORT EFFICIENCY.
Is a measure of the relative efficiency of a sort,
usually an estimate of the number of comparisons
and moves required to order an unordered list.
Example 2
Selection Sort Efficiency.
✘ It contains the two loops.
split
22 18 12 -4 58 7 31 42
split split
22 18 12 -4 58 7 31 42
split split split split
22 18 12 -4 58 7 31 42
merge merge merge merge
18 22 -4 12 7 58 31 42
merge merge
-4 12 18 22 7 31 42 58
merge
-4 7 12 18 22 31 42 58
Merge Sort
example.
SEARCHING.
✘ One MORE common and time-consuming operations in
computer science is searching
✘ The process used to find the location of a target among a
list of objects.
✘ The two basic search algorithms:
✘ Sequential search including three interesting variations
and,
✘ Binary search.
SEQUENTIAL SEARCH.
✘ Used whenever the list is not ordered.
✘ Generally, technique used only for small lists or lists that are not
searched often.
✘ Process: Start searching for the target at the beginning of the list and
continue until target found or it is not in the list.
✘ This approach has two possibilities: Find element (successful) or
reach end of list (unsuccessful).
Sequential Search
Example for
Successful search.
Sequential Search
Example for
Unsuccessful search.
Efficiency of the
sequential
search is O(n).
BINARY SEARCH.
✘ Sequential search algorithm is very slow. If an array of
1000 elements, exists, 1000 comparisons are made in
worst case.
✘ If the array is not sorted, the sequential search is the only
solution.
✘ However, if the array is sorted, we can use a more efficient
algorithm called binary search.
✘ Generally speaking, Binary search used whenever the list
starts to become large.
BINARY SEARCH.
✘ Begins by testing the data in the element at the middle of the array to
determine if the target is in the first or the second half of the list.
✘ If target in first half, there is NO need to check the second half.
✘ If target in second half, NO need to test the first half.
✘ In other words, half the list is eliminated from further consideration with
just one comparison.
✘ This process repeated, eliminating half of the remaining list with each
test, until target if found or does not exist in the list.
✘ To find the middle of the list, three variables needed: one to identify the
beginning of the list, one to identify the middle of the list, and one to
identify the end of the list.
Binary Search
Example for
Successful search.
Binary Search
Example for
Unsuccessful search.
Efficiency of the
binary search is
O(log n).
✘ End for today.
THE END FOR TODAY
15 JUNE 2021.
✘ End for today.
THE END FOR TODAY
15 JUNE 2021.