© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.

Slides:



Advertisements
Similar presentations
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Advertisements

The Efficiency of Algorithms Chapter 4 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
HST 952 Computing for Biomedical Scientists Lecture 10.
Fundamentals of Python: From First Programs Through Data Structures
Chapter 9: Searching, Sorting, and Algorithm Analysis
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Chapter 10 Algorithm Efficiency
Introduction to Analysis of Algorithms
© 2006 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 Algorithm Efficiency and Sorting CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck.
Complexity Analysis (Part I)
© 2006 Pearson Addison-Wesley. All rights reserved6-1 More on Recursion.
Cmpt-225 Algorithm Efficiency.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Algorithm Efficiency and Sorting
The Efficiency of Algorithms
Algorithm Efficiency and Sorting Bina Ramamurthy CSE116A,B.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Pointers Contd.. Causing a Memory Leak int *ptr = new int; *ptr = 8; int *ptr2 = new int; *ptr2 = -5; ptr = ptr2; ptr 8 ptr2 -5 ptr 8 ptr2 -5.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Algorithm Analysis (Big O)
COMP s1 Computing 2 Complexity
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Week 2 CS 361: Advanced Data Structures and Algorithms
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
{ CS203 Lecture 7 John Hurley Cal State LA. 2 Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Algorithm Analysis. Algorithm An algorithm is a clearly specified set of instructions which, when followed, solves a problem. recipes directions for putting.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Chapter 19 Searching, Sorting and Big O
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Analysis of Algorithms
Measuring the Efficiency of Algorithms Analysis of algorithms Provides tools for contrasting the efficiency of different methods of solution Time efficiency,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 9: Algorithm Efficiency and Sorting Data Abstraction &
Algorithm Efficiency Chapter 10 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms.
CS1020 Data Structures and Algorithms I Lecture Note #11 Analysis of Algorithms.
Chapter 10 B Algorithm Efficiency and Sorting. © 2004 Pearson Addison-Wesley. All rights reserved 9 A-2 Sorting Algorithms and Their Efficiency Sorting.
Algorithm Efficiency and Sorting Data Structure & Algorithm.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
© 2006 Pearson Addison-Wesley. All rights reserved10 B-1 Chapter 10 (continued) Algorithm Efficiency and Sorting.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Computer Science and Software Engineering University of Wisconsin - Platteville 8. Comparison of Algorithms Yan Shi CS/SE 2630 Lecture Notes Part of this.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Efficiency of Algorithms. Node - data : Object - link : Node + createNode() + getData() + setData() + getLink() + setLink() + addNodeAfter() + removeNodeAfter()
Algorithm Analysis (Big O)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Searching Topics Sequential Search Binary Search.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 9: Algorithm Efficiency and Sorting Data Abstraction &
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Section 1.7 Comparing Algorithms: Big-O Analysis.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
Algorithm Analysis 1.
Algorithm Efficiency and Sorting
CS 302 Data Structures Algorithm Efficiency.
Introduction to Search Algorithms
Algorithm Efficiency and Sorting
Algorithm Efficiency Chapter 10
Algorithm Analysis Bina Ramamurthy CSE116A,B.
Algorithm Efficiency and Sorting
Algorithm Efficiency and Sorting
Algorithm Efficiency and Sorting
Presentation transcript:

© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting

© 2011 Pearson Addison-Wesley. All rights reserved 10 A-2 Measuring the Efficiency of Algorithms Analysis of algorithms –Provides tools for contrasting the efficiency of different methods of solution A comparison of algorithms –Should focus of significant differences in efficiency –Should not consider reductions in computing costs due to clever coding tricks

© 2011 Pearson Addison-Wesley. All rights reserved 10 A-3 Measuring the Efficiency of Algorithms Three difficulties with comparing programs instead of algorithms –How are the algorithms coded? –What computer should you use? –What data should the programs use? Algorithm analysis should be independent of –Specific implementations –Computers –Data

© 2011 Pearson Addison-Wesley. All rights reserved 10 A-4 The Execution Time of Algorithms Counting an algorithm's operations is a way to access its efficiency –An algorithm’s execution time is related to the number of operations it requires –Example: iteratively sum elements in int[100] array 99 addition operations General: #additions = some function f (#elements) N elements -> N-1 operations

© 2011 Pearson Addison-Wesley. All rights reserved 10 A-5 Algorithm Growth Rates An algorithm’s time requirements can be measured as a function of the problem size An algorithm’s growth rate –Enables the comparison of one algorithm with another –Examples Algorithm A requires time proportional to n 2 Algorithm B requires time proportional to n Algorithm efficiency is typically a concern for large problems only

© 2011 Pearson Addison-Wesley. All rights reserved 10 A-6 Algorithm Growth Rates Figure 10-1 Time requirements as a function of the problem size n

© 2011 Pearson Addison-Wesley. All rights reserved 10 A-7 Order-of-Magnitude Analysis and Big O Notation Definition of the order of an algorithm Algorithm A is order f(n) – denoted O(f(n)) – if constants k and n 0 exist such that A requires no more than k * f(n) time units to solve a problem of size n ≥ n 0 –i.e. we can express work an algorithm has to do as a function of the size of the input for an operation (like sum of integers) Big O notation –A notation that expresses computing time (i.e. complexity) as the term in a function that increases most rapidly relative to size of problem: O(f(n))

© 2011 Pearson Addison-Wesley. All rights reserved 10 A-8 Order-of-Magnitude Analysis and Big O Notation Definition of the order of an algorithm Algorithm A is order f(n) – denoted O(f(n)) – if constants k and n 0 exist such that A requires no more than k * f(n) time units to solve a problem of size n ≥ n 0 –i.e. we can express work an algorithm has to do as a function of the size of the input for an operation (like sum of integers) Big O notation –A notation that expresses computing time (i.e. complexity) as the term in a function that increases most rapidly relative to size of problem: O(f(n))

© 2011 Pearson Addison-Wesley. All rights reserved 10 A-9 Order-of-Magnitude Analysis and Big O Notation Wha?

© 2011 Pearson Addison-Wesley. All rights reserved 10 A-10 Order-of-Magnitude Analysis and Big O Notation Consider: f(N) = N** *N** Big-O notation -> O(N**4) Some multiple (constant) of N**4 dominates f for large N What about 100*N** ? –Plug in N = 100 (100 is small !) F(N) = 101,000,500 N**4 = 100,000,000 (99.01% of total) 100*N**2 = 1,000,000 (0.99% of total) 500 = 500 ( less than % of total)

© 2011 Pearson Addison-Wesley. All rights reserved 10 A-11 Order-of-Magnitude Analysis and Big O Notation Figure 10-3a A comparison of growth-rate functions: a) in tabular form

© 2011 Pearson Addison-Wesley. All rights reserved 10 A-12 Order-of-Magnitude Analysis and Big O Notation Figure 10-3b A comparison of growth-rate functions: b) in graphical form

© 2011 Pearson Addison-Wesley. All rights reserved 10 A-13 Order-of-Magnitude Analysis and Big O Notation Order of growth of some common functions O(1) < O(log 2 n) < O(n) < O(n * log 2 n) < O(n 2 ) < O(n 3 ) < O(2 n ) –O(1) : bounded, or constant time –O(log 2 n) : logarithmic time (in base 2) –O(n): linear time –O(n * log 2 n): umm, n log n time –O(n 2 ): quadratic time –O(2 n ): exponential time

© 2011 Pearson Addison-Wesley. All rights reserved 10 A-14 Order-of-Magnitude Analysis and Big O Notation Worst-case and average-case analyses –An algorithm can require different times to solve different problems of the same size Worst-case analysis –A determination of the maximum amount of time that an algorithm requires to solve problems of size n Average-case analysis –A determination of the average amount of time that an algorithm requires to solve problems of size n What about best-case analysis?

© 2011 Pearson Addison-Wesley. All rights reserved 10 A-15 Keeping Your Perspective Throughout the course of an analysis, keep in mind that you are interested only in significant differences in efficiency When choosing an ADT’s implementation, consider how frequently particular ADT operations occur in a given application Some seldom-used but critical operations must be efficient

© 2011 Pearson Addison-Wesley. All rights reserved 10 A-16 Keeping Your Perspective If the problem size is always small, you can probably ignore an algorithm’s efficiency Weigh the trade-offs between an algorithm’s time requirements and its memory requirements Compare algorithms for both style and efficiency Order-of-magnitude analysis focuses on large problems

© 2011 Pearson Addison-Wesley. All rights reserved 10 A-17 The Efficiency of Searching Algorithms Sequential search –Strategy Look at each item in the data collection in turn, beginning with the first one Stop when –You find the desired item –You reach the end of the data collection

© 2011 Pearson Addison-Wesley. All rights reserved 10 A-18 The Efficiency of Searching Algorithms Sequential search –Efficiency Worst case: O(n) Average case: O(n) Best case: O(1)

© 2011 Pearson Addison-Wesley. All rights reserved 10 A-19 The Efficiency of Searching Algorithms Binary search –Strategy To search a sorted array for a particular item –Repeatedly divide the array in half –Determine which half the item must be in, if it is indeed present, and discard the other half –Efficiency Worst case: O(log 2 n) For large arrays, the binary search has an enormous advantage over a sequential search

© 2011 Pearson Addison-Wesley. All rights reserved 10 A-20 Sorting Algorithms and Their Efficiency Sorting –A process that organizes a collection of data into either ascending or descending order Categories of sorting algorithms –An internal sort Requires that the collection of data fit entirely in the computer’s main memory –An external sort The collection of data will not fit in the computer’s main memory all at once but must reside in secondary storage

© 2011 Pearson Addison-Wesley. All rights reserved 10 A-21 Sorting Algorithms and Their Efficiency Data items to be sorted can be –Integers –Character strings –Objects Sort key –The part of a record that determines the sorted order of the entire record within a collection of records

© 2011 Pearson Addison-Wesley. All rights reserved 10 A-22 Selection Sort Selection sort –Strategy Select the largest item and put it in its correct place Select the next largest item and put it in its correct place, etc. Figure 10-4 A selection sort of an array of five integers

© 2011 Pearson Addison-Wesley. All rights reserved 10 A-23 Selection Sort Analysis –Selection sort is O(n 2 ) Advantage of selection sort –It does not depend on the initial arrangement of the data Disadvantage of selection sort –It is only appropriate for small n

© 2011 Pearson Addison-Wesley. All rights reserved 10 A-24 Please open file carrano_ppt10_B.ppt to continue viewing chapter 10.