09AlgorithmEfficiencyAndSorting

# 09AlgorithmEfficiencyAndSorting - CS 240 Chapter 9...

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

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

View Full Document

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

View Full Document

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.

Unformatted text preview: CS 240 Chapter 9 Algorithm Efficiency & CS 240 Chapter 9 Algorithm Efficiency & 1Page 1 Chapter 9 Algorithm Efficiency & Sorting Programming efficiently has been important up to this point, but were now going to examine the methods that have been developed to determine just how efficient a program is. Measuring Algorithm Efficiency Example: Sorting Algorithms Example: Searching Algorithms CS 240 Chapter 9 Algorithm Efficiency & CS 240 Chapter 9 Algorithm Efficiency & 2Page 2 Function T(n) is said to be O(f(n)) if there are positive constants c and n0 such that T(n) &#14; c f(n) for every n &#0; n0. Example: n3 + 3n2 + 6n + 5 is O(n3). (Use c = 15 and n0 = 1.) Example: n2 + n logn is O(n2). (Use c = 2 and n0 = 1.) Time Complexity Terminology: Big-O g(n) r(n) ng nr r(n) is O(g(n)) since (1)g(n) exceeds r(n) for all n-values past ng g(n) is O(r(n)) since (3)r(n) exceeds g(n) for all n-values past nr CS 240 Chapter 9 Algorithm Efficiency & CS 240 Chapter 9 Algorithm Efficiency & 3Page 3 Both algorithms below have O(n3) time complexity. (In fact, the execution time for Algorithm A is n3 + n2 + n, and the execution time for Algorithm B is n3 + 101n2 + n.) Demonstrating The Big-O Concept 1,110 11,110 1,010,100 2,010,100 1,001,001,000 1,101,001,000 1,000,100,010,000 1,010,100,010,000 1,000,010,000,100,000 1,001,010,000,100,000 1,000,001,000,001,000,000 1,000,101,000,001,000,000 10 100 1,000 10,000 100,000 1,000,000 A B ALGORITHM Input Size n CS 240 Chapter 9 Algorithm Efficiency & CS 240 Chapter 9 Algorithm Efficiency & 4Page 4 Both algorithms below have O(n2) time complexity. (In fact, the execution time for Algorithm C is n2 + 2n + 3, and the execution time for Algorithm D is n2 + 1002n + 3.) A Second Big-O Demonstration 123 10,123 10,203 110,203 1,002,003 2,002,003 100,020,003 110,020,003 10,000,200,003 10,100,200,003 1,000,002,000,003 1,001,002,000,003 10 100 1,000 10,000 100,000 1,000,000 C D ALGORITHM Input Size n CS 240 Chapter 9 Algorithm Efficiency & CS 240 Chapter 9 Algorithm Efficiency & 5Page 5 Both algorithms below have O(nlogn) time complexity. (In fact, the execution time for Algorithm E is nlogn + 5n, and the execution time for Algorithm F is nlogn + 105n. Note that the linear term for Algorithm F will dominate until n = 2105.) One More, Rather Complex Big-O Demonstration 90 1,090 1,200 11,200 15,000 115,000 190,000 1,190,000 2,200,000 12,200,000 25,000,000 125,000,000 10 100 1,000 10,000 100,000 1,000,000 E F ALGORITHM Input Size n CS 240 Chapter 9 Algorithm Efficiency & CS 240 Chapter 9 Algorithm Efficiency &...
View Full Document

## This note was uploaded on 08/26/2009 for the course CS 240 taught by Professor Klein,s during the Spring '08 term at Southern Illinois University Edwardsville.

### Page1 / 68

09AlgorithmEfficiencyAndSorting - CS 240 Chapter 9...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online