{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

cs2050-02-Complexity

# cs2050-02-Complexity - COMPUTATIONAL COMPLEXITY(Chapter 1...

This preview shows pages 1–8. Sign up to view the full content.

COMPUTATIONAL COMPLEXITY (Chapter 1 in Main text)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
How long does it take? Suppose you have an array of size 100. How long does it take to access a particular element? does it take to access a particular element? Suppose it takes 1 unit of time to access a single element. What if the size of the array is increased to 5000? It would still take 1 unit of time to access l an element. What if the array has N elements? It would still take only 1 unit of time to access a single element no matter what the value of N is.
How long does it take? Suppose you have an integer array of size 100. How long does it take to sum the 100 elements? If it takes 100 units of time to sum 100 elements, how long would it take to sum the values in an how long would it take to sum the values in an array of size N? It would take N units of time. It takes 1 unit of time to access a single element of an array – the time is independent of the size of the array. S i th l t f t k ti th t Summing the elements of an array takes time that is proportional to the size of the array .

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Algorithm Efficiency How can two different algorithms be compared, i i h t l ith b id t i.e., in what sense can one algorithm be said to be "better" – or more efficient - than another algorithm?
Consider the layout of a telephone directory: If the listings are not in any useful order (e.g., ordered according to the date on which each person was assigned th i h b ) th fi di th li ti f ifi d their phone number), then finding the listing for a specified name would begin with a sequential scan of the entire phone book starting with the first listing on page 1 and ending when the desired listing is found. There is not a more efficient algorithm. If there are N listings, then the number of listings examined during a scan of an unordered phone book is N in the worst case ( hen the desired listing is the er last one) and is N/2 case (when the desired listing is the very last one), and is N/2 on average.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
If the listings in the phone book are sorted according to surname then it is possible to apply a "binary search" surname, then it is possible to apply a binary search algorithm to find the listing we need: 1 O th h b k d h k th iddl 1. Open the phone book and check the middle page 2. If the surname we want comes before the surnames on the page, then we can ignore all of the subsequent listings and apply binary search to the remaining ones 3. If the surname we want comes after the surnames on the page, then we ignore all of the previous listings d l bi h t th i i and apply binary search to the remaining ones Applying binary search allows us to find the desired listing - or determine it isn’t present - after examining approximately log 2 (N) listings.
The decision about how to list names and phone numbers in a phone book comes down to a decision between two data structures: Ordered versus unordered data structures: .

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern