Algorithms 2
Algorithms 2
Previously…
Characteristics of algorithms
Pseudocode
Examples: Max algorithm
Today…
Examples: Sum algorithm
Problem of searching an ordered list
Linear search & binary search
algorithms
Sorting problem
Bubble sort & insertion sort algorithms
Practice Exercises
Devise an algorithm that finds the sum of all
the integers in a list.
x x x x
Search Alg. #2: Binary Search
Search Example
Search for 19 in the
Index: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
16 list
1, 2, 3, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22
i using linear search i
i j j
comparison.
5 2 do nothing. Move to the next
comparison.
5 3 do nothing. Move to the next
Insertion Sort Algorithm
Efficiency of Algorithms
Intuitively we see that binary search is much
faster than linear search,
Also, we may see that insertion sort is better
than bubblesort in some cases (why?
when?),
But how do we analyze the efficiency of
algorithms formally?
Use methods of algorithmic complexity,
which utilize the order-of-growth