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,
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
© 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.
Data Structures Data Structures Topic #7. Today’s Agenda How to measure the efficiency of algorithms? Discuss program #3 in detail Review for the midterm.
© 2006 Pearson Addison-Wesley. All rights reserved6-1 More on Recursion.
Cmpt-225 Algorithm Efficiency.
Algorithm Efficiency and Sorting
The Efficiency of Algorithms
Algorithm Efficiency and Sorting Bina Ramamurthy CSE116A,B.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Cmpt-225 Simulation. Application: Simulation Simulation  A technique for modeling the behavior of both natural and human-made systems  Goal Generate.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved ADT Implementation:
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
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.
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms 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.
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
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.
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()
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.
© 2006 Pearson Addison-Wesley. All rights reserved15 A-1 Chapter 15 External Methods.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
Algorithm Analysis 1.
Chapter 10 Algorithm Efficiency
CS 302 Data Structures Algorithm Efficiency.
Analysis of Algorithms
Introduction to Search Algorithms
Chapter 9: Searching, Sorting, and Algorithm Analysis
Algorithm Efficiency and Sorting
Analysis of Algorithms
External Methods Chapter 15 (continued)
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Algorithm Efficiency and Sorting
Objectives At the end of the class, students are expected to be able to do the following: Understand the purpose of sorting technique as operations on.
Algorithm Efficiency Chapter 10.
Algorithm Efficiency and Sorting
Analysis of Algorithms
Algorithm Efficiency: Searching and Sorting Algorithms
Algorithm Efficiency Chapter 10
Algorithm Analysis Bina Ramamurthy CSE116A,B.
Advanced Implementation of Tables
Algorithm Efficiency and Sorting
The Efficiency of Algorithms
Algorithm Efficiency and Sorting
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Algorithm Efficiency and Sorting
Algorithm Efficiency and Sorting
Algorithm Efficiency and Sorting
Analysis of Algorithms
Chapter 13 Hashing © 2011 Pearson Addison-Wesley. All rights reserved.
Algorithm Efficiency and Sorting
Algorithm Efficiency and Sorting
Algorithm Efficiency and Sorting
Recursion: The Mirrors
Presentation transcript:

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

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

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

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 Examples Traversal of a linked list The Towers of Hanoi Nested Loops © 2011 Pearson Addison-Wesley. All rights reserved

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 n2 Algorithm B requires time proportional to n Algorithm efficiency is typically a concern for large problems only © 2011 Pearson Addison-Wesley. All rights reserved

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

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 n0 exist such that A requires no more than k * f(n) time units to solve a problem of size n ≥ n0 Growth-rate function A mathematical function used to specify an algorithm’s order in terms of the size of the problem Big O notation A notation that uses the capital letter O to specify an algorithm’s order Example: O(f(n)) © 2011 Pearson Addison-Wesley. All rights reserved

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

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

Order-of-Magnitude Analysis and Big O Notation Order of growth of some common functions O(1) < O(log2n) < O(n) < O(n * log2n) < O(n2) < O(n3) < O(2n) Properties of growth-rate functions You can ignore low-order terms You can ignore a multiplicative constant in the high-order term O(f(n)) + O(g(n)) = O(f(n) + g(n)) © 2011 Pearson Addison-Wesley. All rights reserved

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 © 2011 Pearson Addison-Wesley. All rights reserved

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

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

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

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

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(log2n) For large arrays, the binary search has an enormous advantage over a sequential search © 2011 Pearson Addison-Wesley. All rights reserved

Sorting Algorithms and Their Efficiency 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

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

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

Selection Sort Analysis Advantage of selection sort Selection sort is O(n2) 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

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