Upload
douglas-thompson
View
219
Download
0
Embed Size (px)
DESCRIPTION
Algorithm 1 position = 0, equal=true IF different lengths THEN equal=false ELSE WHILE Position < string length IF characters at position do not match THEN equal=false add one to position RETURN equal
Citation preview
Complexity and Efficiency
Computer Science 3Gerb
Objective: Understand complexity and efficiency of algorithms
Complexity and Efficiency
• Algorithms that run more quickly are said to be more efficient.
• Algorthims that run less quickly are said to be more complex.
• For example, consider two algorithms for determining if two strings are equal
Algorithm 1position = 0, equal=trueIF different lengths THEN equal=falseELSE WHILE Position < string length IF characters at position do not match THEN equal=false add one to positionRETURN equal
Algorithm 2position = 0, equal=trueIF different lengths THEN equal=falseELSE WHILE Position < string length AND characters at position match Add one to position equal = position >= string lengthRETURN equal
Algorithm 2 is more efficient
• Algorithm 1 keeps searching to the end of the string even if the first characters don’t match.– PERFORMANCE– yyyyyynnnnn– PERFORATION
• Algorithm 2 stops searching when it finds a mismatch.– PERFORMANCE– yyyyyyn– PERFORATION
• Algorithm 2 is more efficient. Algorithm 1 is more complex.
Another example• Example: Find your name in an alphabetically ordered
list of N names.• Algorithm 1: Start at the first. Keep looking at the next
until you find it. Called sequential search.• Algorithm 2: Called binary search:Look at the exact middle nameIF it’s your name STOPELSE IF it’s earlier Binary search the 2nd halfELSE Binary search the 1st half
Example: Search for “Jacob” in a list of names
AbrahamDeborahEvelynGabrielGayHenryJacobPamelaPaulPeterWinston
Sequential Search: Start at the first name
AbrahamDeborahEvelynGabrielGayHenryJacobPamelaPaulPeterWinston
Sequential Search: Progress to the next name until you find Jacob
AbrahamDeborahEvelynGabrielGayHenryJacobPamelaPaulPeterWinston
Sequential Search: Progress to the next name until you find Jacob
AbrahamDeborahEvelynGabrielGayHenryJacobPamelaPaulPeterWinston
Sequential Search: Progress to the next name until you find Jacob
AbrahamDeborahEvelynGabrielGayHenryJacobPamelaPaulPeterWinston
Sequential Search: Progress to the next name until you find Jacob
AbrahamDeborahEvelynGabrielGayHenryJacobPamelaPaulPeterWinston
Sequential Search: Progress to the next name until you find Jacob
AbrahamDeborahEvelynGabrielGayHenryJacobPamelaPaulPeterWinston
Found!AbrahamDeborahEvelynGabrielGayHenryJacobPamelaPaulPeterWinston
Binary Search: Look at the middle of the list
AbrahamDeborahEvelynGabrielGayHenryJacobPamelaPaulPeterWinston
The one we want is later in the alphabet, so binary search the 2nd half
AbrahamDeborahEvelynGabrielGayHenryJacobPamelaPaulPeterWinston
{
Look at the middle of the new listAbrahamDeborahEvelynGabrielGayHenryJacobPamelaPaulPeterWinston
{
Jacob is earlier in alphabetical order, so search the first half of the new list
AbrahamDeborahEvelynGabrielGayHenryJacobPamelaPaulPeterWinston
{
Look at the middle of the new list. We round down in this case
AbrahamDeborahEvelynGabrielGayHenryJacobPamelaPaulPeterWinston
{
Jacob is middle of the new list. Found!
AbrahamDeborahEvelynGabrielGayHenryJacobPamelaPaulPeterWinston
{
Compare Binary and Sequential Search
• Algorithm 1 requires looking at an average of N/2 names.
• Algorithm 2, can double the size of the list, still only need to look at one more name
• I.e. requires you to look at Log2N names.
• Algorithm 1 is more complex. Algorithm 2 is more efficient.
If N is Need to look at1 1 Name2-3 2 Names4-7 3 Names8-15 4 Names16-31 5 Names…2048-4095 12 Names…1-2 Million 21 Names
Summary
• Complex algorithms take longer than efficient algorithms
• Sequential search (looking through a list until you find an item) is more complex than binary search (eliminate half the list each time).