Ch 18 – Big-O Notation: Sorting & Searching Efficiencies Our interest in the efficiency of an algorithm is based on solving problems of large size. If.

Slides:



Advertisements
Similar presentations
Growth-rate Functions
Advertisements

CSE Lecture 3 – Algorithms I
Algorithm Analysis.
Chapter 9: Searching, Sorting, and Algorithm Analysis
CompSci Searching & Sorting. CompSci Searching & Sorting The Plan  Searching  Sorting  Java Context.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
Algorithmic Complexity Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Lecture 25 Selection sort, reviewed Insertion sort, reviewed Merge sort Running time of merge sort, 2 ways to look at it Quicksort Course evaluations.
Wednesday, 11/25/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/25/02  QUESTIONS??  Today:  More on sorting. Advanced sorting algorithms.  Complexity:
Analysis of Algorithms. Time and space To analyze an algorithm means: –developing a formula for predicting how fast an algorithm is, based on the size.
Cmpt-225 Algorithm Efficiency.
1 Algorithm Efficiency, Big O Notation, and Role of Data Structures/ADTs Algorithm Efficiency Big O Notation Role of Data Structures Abstract Data Types.
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.
Data Structure Algorithm Analysis TA: Abbas Sarraf
Analysis of Algorithm.
1 Section 2.3 Complexity of Algorithms. 2 Computational Complexity Measure of algorithm efficiency in terms of: –Time: how long it takes computer to solve.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
Abstract Data Types (ADTs) Data Structures The Java Collections API
COMP s1 Computing 2 Complexity
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Searching – Linear and Binary Searches. Comparing Algorithms Should we use Program 1 or Program 2? Is Program 1 “fast”? “Fast enough”? P1P2.
Big Oh Algorithm Analysis with
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)
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Chapter 19 Searching, Sorting and Big O
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Analysis of Algorithms
Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
CSC 205 Java Programming II Algorithm Efficiency.
Big Oh Algorithms are compared to each other by expressing their efficiency in big-oh notation Big O notation is used in Computer Science to describe the.
Week 12 - Wednesday.  What did we talk about last time?  Asymptotic notation.
Algorithm Analysis (Algorithm Complexity). Correctness is Not Enough It isn’t sufficient that our algorithms perform the required tasks. We want them.
1 Lecture 5 Generic Types and Big O. 2 Generic Data Types A generic data type is a type for which the operations are defined but the types of the items.
CSC 211 Data Structures Lecture 13
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.
Computer Science and Software Engineering University of Wisconsin - Platteville 8. Comparison of Algorithms Yan Shi CS/SE 2630 Lecture Notes Part of this.
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
3.3 Complexity of Algorithms
Algorithmic Analysis Charl du Plessis and Robert Ketteringham.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Searching Topics Sequential Search Binary Search.
CISC220 Spring 2010 James Atlas Lecture 07: Big O Notation.
Search Algorithms Written by J.J. Shepherd. Sequential Search Examines each element one at a time until the item searched for is found or not found Simplest.
Concepts of Algorithms CSC-244 Unit 3 and 4 Algorithm Growth Rates Shahid Iqbal Lone Computer College Qassim University K.S.A.
WHICH SEARCH OR SORT IS BETTER?. COMPARING ALGORITHMS Time efficiency refers to how long it takes an algorithm to run Space efficiency refers to the amount.
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
Section 1.7 Comparing Algorithms: Big-O Analysis.
Chapter 16: Searching, Sorting, and the vector Type.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
1 Algorithms Searching and Sorting Algorithm Efficiency.
Algorithm Complexity Analysis (Chapter 10.4) Dr. Yingwu Zhu.
Searching – Linear and Binary Searches
Introduction to Algorithms
Program Efficiency Interested in “order of magnitude”
Lecture 6 Efficiency of Algorithms (2) (S&G, ch.3)
Algorithm Analysis Bina Ramamurthy CSE116A,B.
Presentation transcript:

Ch 18 – Big-O Notation: Sorting & Searching Efficiencies Our interest in the efficiency of an algorithm is based on solving problems of large size. If the array to be searched/sorted is relatively small, it doesn’t much matter whether or not the algorithm is efficient because the number of comparisons is reasonably small. But as the size of the array grows, the number of comparisons grows even faster. Our interest in the efficiency of an algorithm is based on solving problems of large size. If the array to be searched/sorted is relatively small, it doesn’t much matter whether or not the algorithm is efficient because the number of comparisons is reasonably small. But as the size of the array grows, the number of comparisons grows even faster. So the amount of work an algorithm grows proportionally with the size of the list. We can approximate the amount of work an algorithm does by a mathematical notation called the Order of Magnitude or Big-O notation. So the amount of work an algorithm grows proportionally with the size of the list. We can approximate the amount of work an algorithm does by a mathematical notation called the Order of Magnitude or Big-O notation. The order of magnitude of a function is the same as the degree of the polynomial. That is, it is the term in the function that dominates the computation for large amounts of data. Consider the following: The order of magnitude of a function is the same as the degree of the polynomial. That is, it is the term in the function that dominates the computation for large amounts of data. Consider the following: f(N) = N N N + 50 The order of f(N) is N 4. Written O(N 4 ) in Big-O notation. That is, N 4 will dominate the function for large N. N 4 will dominate f(N) and is so much more than 50, 10N, or even 100N 2 that we ignore the other terms. That doesn’t mean that the other terms do not contribute to the computation time; it only means that they are NOT SIGNIFICANT in our approximation. The order of f(N) is N 4. Written O(N 4 ) in Big-O notation. That is, N 4 will dominate the function for large N. N 4 will dominate f(N) and is so much more than 50, 10N, or even 100N 2 that we ignore the other terms. That doesn’t mean that the other terms do not contribute to the computation time; it only means that they are NOT SIGNIFICANT in our approximation. Consider the following table representing a comparison of rates of growth. The table shows how the size (N) of the list effects the amount of work needed to complete different types of algorithms. Consider the following table representing a comparison of rates of growth. The table shows how the size (N) of the list effects the amount of work needed to complete different types of algorithms. Nlog 2 NNlog 2 NN 2 N 3 2 N , ,147,483, ,1445 yrs on Super Computer , times age of universe ,53616,777,216Don’t Ask! Common orders of magnitude:Logarithmic O(log 2 N), Linear O(N), Quadratic O(N 2 ), Cubic O(N 3 ) Common orders of magnitude:Logarithmic O(log 2 N), Linear O(N), Quadratic O(N 2 ), Cubic O(N 3 )

SEARCHING Efficiencies using Big-O : Searching a phone book of 50,000 people. SEARCHING Efficiencies using Big-O : Searching a phone book of 50,000 people.  Linear Search – A 50,000 element array would require 50,000 comparisons in a worst case scenario. The amount of work, comparisons, is in a direct correlation with the size of the list.  Binary Search – Is based on repeated divisions of 2. Consider the following: Divisions of 2N=50,000Fraction of ListMathematically Stated 125,0001/2 212,5001/4The greatest number of comparisons will be K, 36,2501/8where K is the first value such that: 43,1251/16 51,5631/322 K > N or 2 K > 50, / / = 32,768 and 2 16 = 65, / /512Written as a logarithm ( exp = log base answer) 10491/ / = 65,536 or 16 = log 2 65, / /8192Specifically for N = 50, /16,384log 2 50,000 = /32, /65,536  Therefore, the amount of work for a Binary Search is logarithmic in nature. The maximum number of comparisons of a 50,000 element array using the Binary Search would be 16! The only stipulation is that the list must be sorted first. Summarizing our searching algorithms: Summarizing our searching algorithms:  Sequential Search is an O(N) algorithm.  Binary Search is an O(log 2 N) algorithm. Sometimes written as O(log N).

SIMPLE SORTING Efficiencies using Big- O SIMPLE SORTING Efficiencies using Big- O  Selection/Exchange Sort :  Will always have N(N-1)/2 comparisons because it’s looping sequence of is always the same! for(int i=0; i<list.length-1 ; i++) for(int j=0; j<list.length ; j++) for(int j=0; j<list.length ; j++)  Therefore, the Selection/Exchange Sorts order of magnitude is:  O(N 2 )  Bubble Sort - Consider the following more efficient version: int k = 1;//need at least 1 pass do{ sorted = true; sorted = true; for(int j = 0; j < list.length – k ; j++) for(int j = 0; j < list.length – k ; j++) if( list[j] > list[j+1] ){ swap(list, j); swap(list, j); sorted = false; sorted = false;} k++; k++; }while (!sorted);  Will have (2KN-K-K 2 )/2 comparisons because it depends on the makeup of the array. Since K is between 1 & N-1, we can say that K is on the same order as N.Since K is between 1 & N-1, we can say that K is on the same order as N. 2KN will then dominate the function and is proportional to N 2.2KN will then dominate the function and is proportional to N 2.  Therefore, the Bubble Sort order of magnitude is:  O(N 2 )  Which of these sorts is the most efficient on large amounts of data?  Because the Selection/Exchange Sort & Bubble Sort are both O(N 2 ) algorithms, for large values of N there is NO SIGNIFICANT difference in the amount of work they do! The Bubble sort may require somewhat fewer comparisons, but the difference on average is not significant.  Most Simple Sorts are O(N 2 )

RECURSIVE SORTING Efficiencies using Big- O RECURSIVE SORTING Efficiencies using Big- O  QUICK SORT – The Quick Sort, like the Binary Search, uses the “divide & conquer” approach. The original list is divided into two lists. Each of these lists is again divided into two lists. This division continues until each list has exactly 1 element. Assuming that the splitting value is the median of the list, each sublist has approximately one-half as many elements as the previous list. It would therefore take up to log 2 N levels of splits to complete. DataLevelOrder 10 O(N) 21 O(N) 42 O(N) 83 O(N) :: : Nlog 2 N O(N)  At level 0, each number including the splitting value is compared to the splitting value. So N comparisons are made at level 0.  At level 1, there are two lists, one with k elements, and one with N-k elements. In the first list with k elements, k comparisons are made (each of the k elements to the new splitting value). In the second list of N-k elements, N-k comparisons are made. At level 1, a total of k + (N-k) or N comparisons are made. And so on for all levels.  Therefore, the total number of comparisons made to order a list of N elements using Quick Sort is the number of splitting levels log 2 N times the number of comparisons at each level N.  Consequently, the Quick Sort order of magnitude is O(Nlog 2 N).

Big-O Summary: Big-O Summary:  Revisiting the rates of growth table from before: Nlog 2 NNlog 2 NN 2 N 3 2 N , ,147,483,643 The order of magnitude, Big-O, of a function is the degree of the function. That is, it is the term that dominates the function for large size (N). This is an approximation that gives us a mathematical way to compare relative efficiencies for specific sorts & searches. The order of magnitude, Big-O, of a function is the degree of the function. That is, it is the term that dominates the function for large size (N). This is an approximation that gives us a mathematical way to compare relative efficiencies for specific sorts & searches. Common Orders of Magnitude: Common Orders of Magnitude:  O(N) Linear:Traversing a list sequentially one element at a time.  O(log 2 N)Logarithmic:Binary Search – Repeated divisions of 2.  O(N 2 )Quadratic:Simple sorts. Traversing a 2-Dimensional array.  O(N 3 )Cubic: Traversing a 3-Dimensional array. So what’s the most efficient algorithm in terms of Big-O notation: So what’s the most efficient algorithm in terms of Big-O notation:  O(1)Constant Time:The size of the list doesn’t effect the time at all! Examples:Swapping data, accessing a component of an array through its index, inserting data into an array through its index. Hash tables arrange data so that they can be accessed directly through some ID number. In other words, their ID numbers have semantic meaning and are the indices of the array storing the data. Continued at a college near you!