© 2006 Pearson Addison-Wesley. All rights reserved10 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

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
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
Faster Sorting Methods Chapter 12 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then.
© 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.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
CHAPTER 11 Sorting.
© 2006 Pearson Addison-Wesley. All rights reserved6-1 More on Recursion.
Cmpt-225 Algorithm Efficiency.
© 2006 Pearson Addison-Wesley. All rights reserved10 B-1 Chapter 10 Sorting.
Sorting Chapter 10.
Algorithm Efficiency and Sorting
Algorithm Efficiency and Sorting Bina Ramamurthy CSE116A,B.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Sorting Algorithms and their Efficiency Chapter 11 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
(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.
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.
© 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.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
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.
Algorithm Efficiency and Sorting
Sorting Algorithms and their Efficiency
Sorting.
Figure 9.1 Time requirements as a function of the problem size n.
Introduction to Search Algorithms
Algorithm Efficiency and Sorting
Sorting Algorithms and their Efficiency
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 and Sorting
Algorithm Efficiency and Sorting
Algorithm Efficiency and Sorting
Algorithm Efficiency and Sorting
Algorithm Efficiency and Sorting
Algorithm Efficiency and Sorting
Algorithm Efficiency and Sorting
Algorithm Efficiency and Sorting
Algorithm Efficiency and Sorting
Presentation transcript:

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

© 2006 Pearson Addison-Wesley. All rights reserved10 A-2 Measuring the Efficiency of Algorithms Analysis of algorithms –contrasts the efficiency of different methods of solution A comparison of algorithms –Should focus on significant differences in efficiency –Should not consider reductions in computing costs due to clever coding tricks

© 2006 Pearson Addison-Wesley. All rights reserved10 A-3 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

© 2006 Pearson Addison-Wesley. All rights reserved10 A-4 Algorithm Growth Rates An algorithm’s time requirements can be measured as a function of the input size Algorithm efficiency is typically a concern for large data sets only

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

© 2006 Pearson Addison-Wesley. All rights reserved10 A-6 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 Big O notation –A notation that uses the capital letter O to specify an algorithm’s order of growth –Example: O(f(n))

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

© 2006 Pearson Addison-Wesley. All rights reserved10 A-8 Order-of-Magnitude Analysis and Big O Notation Order of growth of some common functions O(1) < O(log 2 n) < O(n) < O(nlog 2 n) < O(n 2 ) < O(n 3 ) < O(2 n ) 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))

© 2006 Pearson Addison-Wesley. All rights reserved10 A-9 "Big Oh" - Upper Bounding Running Time Definition: f(n)  O(g(n)) if  c > 0 and n 0 > 0 such that f(n)  cg(n) for all n  n 0 ( often written as f(n) = O(g(n)) ). Intuition: f(n) = O(g(n)) means f(n) is “of order at most”, or “less than or equal to” g(n) when we ignore small values of n f(n) will run faster than some constant multiple of g(n) ( i.e., constant multiple of g(n) is an upper bound for f(n)) for large enough n

© 2006 Pearson Addison-Wesley. All rights reserved10 A-10 Order-of-Magnitude Analysis and Big O Notation Worst-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

© 2006 Pearson Addison-Wesley. All rights reserved10 A-11 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

© 2006 Pearson Addison-Wesley. All rights reserved10 A-12 The Efficiency of Searching Algorithms Sequential search –Efficiency Worst case: O(n) Average case: O(n) Best case: O(1)

© 2006 Pearson Addison-Wesley. All rights reserved10 A-13 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)

© 2006 Pearson Addison-Wesley. All rights reserved10 A-14 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

© 2006 Pearson Addison-Wesley. All rights reserved10 A-15 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

© 2006 Pearson Addison-Wesley. All rights reserved10 A-16 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

© 2006 Pearson Addison-Wesley. All rights reserved10 A-17 Selection Sort Analysis –Selection sort is O(n 2 ) Advantage of selection sort –The running time does not depend on the initial arrangement of the data (worst case running time is same as best case running time on all data sets) Disadvantage of selection sort –It is only appropriate for small n

© 2006 Pearson Addison-Wesley. All rights reserved10 A-18 Bubble Sort Bubble sort –Strategy Compare adjacent elements and exchange them if they are out of order –Comparing the first two elements, the second and third elements, and so on, will move the largest elements to the end of the array –Repeating this process will eventually sort the array into ascending order

© 2006 Pearson Addison-Wesley. All rights reserved10 A-19 Bubble Sort Figure 10-5 The first two passes of a bubble sort of an array of five integers: a) pass 1; b) pass 2

© 2006 Pearson Addison-Wesley. All rights reserved10 A-20 Bubble Sort Analysis –Worst case: O(n 2 ) –Best case: O(n)

© 2006 Pearson Addison-Wesley. All rights reserved10 A-21 Insertion Sort Insertion sort –Strategy Partition the array into two regions: sorted and unsorted Take each item from the unsorted region and insert it into its correct order in the sorted region Figure 10-6 An insertion sort partitions the array into two regions

© 2006 Pearson Addison-Wesley. All rights reserved10 A-22 Insertion Sort Figure 10-7 An insertion sort of an array of five integers.

© 2006 Pearson Addison-Wesley. All rights reserved10 A-23 Insertion Sort Analysis –Worst case: O(n 2 ) –For small arrays Insertion sort is appropriate due to its simplicity –For large arrays Insertion sort is prohibitively inefficient

© 2006 Pearson Addison-Wesley. All rights reserved10 A-24 Mergesort Important divide-and-conquer sorting algorithms –Mergesort –Quicksort Mergesort –A recursive sorting algorithm –Gives the same performance, regardless of the initial order of the array items –Strategy Divide an array into halves Sort each half Merge the sorted halves into one sorted array

© 2006 Pearson Addison-Wesley. All rights reserved10 A-25 Mergesort Figure 10-8 A mergesort with an auxiliary temporary array

© 2006 Pearson Addison-Wesley. All rights reserved10 A-26 Mergesort Figure 10-9 A mergesort of an array of six integers

© 2006 Pearson Addison-Wesley. All rights reserved10 A-27 Mergesort Analysis –Worst case: O(n log 2 n) –Average case: O(n log 2 n) –Advantage It is an extremely efficient algorithm with respect to time –Drawback It requires a second array as large as the original array

© 2006 Pearson Addison-Wesley. All rights reserved10 A-28 Quicksort –A divide-and-conquer algorithm –Strategy Partition an array into items that are less than the pivot and those that are greater than or equal to the pivot Sort the left section Sort the right section Figure A partition about a pivot

© 2006 Pearson Addison-Wesley. All rights reserved10 A-29 Quicksort Using an invariant to develop a partition algorithm –Invariant for the partition algorithm The items in region S 1 are all less than the pivot, and those in S 2 are all greater than or equal to the pivot Figure Invariant for the partition algorithm

© 2006 Pearson Addison-Wesley. All rights reserved10 A-30 Quicksort Analysis –Worst case quicksort is O(n 2 ) when the array is already sorted and the smallest item is chosen as the pivot Figure A worst-case partitioning with quicksort

© 2006 Pearson Addison-Wesley. All rights reserved10 A-31 Quicksort Analysis –Average case quicksort is O(n * log 2 n) when S 1 and S 2 contain the same – or nearly the same – number of items arranged at random Figure A average-case partitioning with quicksort

© 2006 Pearson Addison-Wesley. All rights reserved10 A-32 Quicksort Analysis –quicksort is usually extremely fast in practice –Even if the worst case occurs, quicksort ’s performance is acceptable for moderately large arrays

© 2006 Pearson Addison-Wesley. All rights reserved10 A-33 Radix Sort Radix sort –Treats each data element as a character string –Strategy Repeatedly organize the data into groups according to the i th character in each element Analysis –Radix sort is O(n)

© 2006 Pearson Addison-Wesley. All rights reserved10 A-34 Radix Sort Figure A radix sort of eight integers

© 2006 Pearson Addison-Wesley. All rights reserved10 A-35 A Comparison of Sorting Algorithms Figure Approximate growth rates of time required for eight sorting algorithms

© 2006 Pearson Addison-Wesley. All rights reserved10 A-36 Summary Worst-case and average-case analyses –Worst-case analysis considers the maximum amount of work an algorithm requires on a problem of a given size –Average-case analysis considers the expected amount of work an algorithm requires on a problem of a given size Order-of-magnitude (aka asymptotic) analysis can be used to choose an implementation for an abstract data type Selection sort, bubble sort, and insertion sort are all O(n 2 ) algorithms Quicksort and mergesort are two very efficient sorting algorithms (O(nlog 2 n))