Algorithm
Algorithm
Ye
• Searching
• Efficiency of algorithms
• Sorting
Searching
• Searching is the process of finding a target element within
a group of items called the search pool; the target may or
may not be in the search pool.
• The search pool can have different data structure. Here we
examine sequences (we use arrays).
• A searching function can return a Boolean value whether
the target is found in the array.
• A searching function can also return the index of the target
• Let's look at two classic searching approaches: linear
search and binary search
2
Searching
Linear Search
• A linear search begins at one end of an array and
examines each element in turn
• Eventually, either the item is found or the end of the array
is encountered
• Implement Linear Search function:
– the function takes an array of values and a target value as input
– the function returns true if the target value is found in the array
or false otherwise
– What about returning the index of the target value assuming
there is no duplicates ? (if not found, return -1)
3
Searching
Linear Search (cont)
• Let’s examine two different implementations of the linear
search algorithm using three different inputs.
• See Week 12 handout 1 …
4
Linear Search (cont) Searching
Conclusion:
-- It takes the longest time to complete the linear search
when the target value is not in the search pool -- we call this
“worst case” when talking about the efficiency of algorithms
• Questions:
In the worst case (the target is not in), for a sorted array of
size n, how many elements have to be checked, in other
words, how many times the critical comparison operation
has to be executed before knowing that the value is not
there when doing binary search?
7
Efficiency of Algorithms
• time efficiency and space efficiency
• How can we measure how long it takes for an algorithm
to solve a problem of a certain size?
– Usually we measure the number of critical operation(s) needed
based on the problem size n, which can be expressed as a function
f on n: f(n)
• For example:
– sorting an array of test scores in ascending numeric order
– sorting an array of people alphabetically by last name
11
Sorting
Selection Sort (cont)
• An example:
original array: 3 9 6 1 2
smallest is 1 at index 3
1 9 6 3 2
smallest is 2 at index 4
1 2 6 3 9
smallest is 3 at index 3
1 2 3 6 9
smallest is 6 at index 3
1 2 3 6 9
13
Sorting
Bubble Sort
14
Sorting
Insertion Sort
Suppose we want to sort an array in ascending order:
• consider the first item to be a sorted subarray (of one item)
• insert the second item into the sorted subarray, shifting the
first item as needed to make room to insert the new
addition
• insert the third item into the sorted subarray (of two items),
shifting items as necessary
• repeat until all values are inserted into their proper
positions
15
Sorting
Insertion Sort (cont)
• An example:
original: 3 9 6 1 2
[3] is sorted, insert 9:
3 9 6 1 2
[3 9] is sorted, insert 6:
3 6 9 1 2
[3 6 9] is sorted, insert 1:
1 3 6 9 2
[1 3 6 9] is sorted, insert 2
1 2 3 6 9
entire array is sorted
16
Sorting
17