CMPT 225 Sorting Algorithms Algorithm Analysis: Big O Notation.

Slides:



Advertisements
Similar presentations
Growth-rate Functions
Advertisements

MATH 224 – Discrete Mathematics
Analysis of Algorithms CS Data Structures Section 2.6.
Lecture3: Algorithm Analysis Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Practice Quiz Question
HST 952 Computing for Biomedical Scientists Lecture 10.
Fundamentals of Python: From First Programs Through Data Structures
Chapter 19: Searching and Sorting Algorithms
the fourth iteration of this loop is shown here
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.
Simple Sorting Algorithms
Complexity Analysis (Part I)
Complexity Analysis (Part I)
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
Cmpt-225 Algorithm Efficiency.
Cmpt-225 Sorting. Fundamental problem in computing science  putting a collection of items in order Often used as part of another algorithm  e.g. sort.
Lecture 3 Aug 31, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis discussion of lab – permutation generation.
Algorithm Efficiency and Sorting
Algorithm Analysis: Big O Notation
Elementary Data Structures and Algorithms
Copyright © Cengage Learning. All rights reserved. CHAPTER 11 ANALYSIS OF ALGORITHM EFFICIENCY ANALYSIS OF ALGORITHM EFFICIENCY.
Lecture 3 Feb 7, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis Image representation Image processing.
Cmpt-225 Simulation. Application: Simulation Simulation  A technique for modeling the behavior of both natural and human-made systems  Goal Generate.
Simple Sorting Algorithms. 2 Bubble sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them.
Sorting Example Insertion Sort. Insertion Sort Sorting problem: n Given an array of N integers, rearrange them so that they are in increasing order. Insertion.
Simple Sorting Algorithms. 2 Outline We are going to look at three simple sorting techniques: Bubble Sort, Selection Sort, and Insertion Sort We are going.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Abstract Data Types (ADTs) Data Structures The Java Collections API
Algorithm Analysis & Complexity We saw that a linear search used n comparisons in the worst case (for an array of size n) and binary search had logn comparisons.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
{ 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)
Data Structures and Algorithms Lecture 5 and 6 Instructor: Quratulain Date: 15 th and 18 th September, 2009 Faculty of Computer Science, IBA.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Analysis of Algorithms
Chapter 19: Searching and Sorting Algorithms
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
L. Grewe.  An array ◦ stores several elements of the same type ◦ can be thought of as a list of elements: int a[8]
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data.
Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion.
Data Structure Introduction.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
3 – SIMPLE SORTING ALGORITHMS
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Chapter 9 Efficiency of Algorithms. 9.3 Efficiency of Algorithms.
CISC 235: Topic 1 Complexity of Iterative Algorithms.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 1. Complexity Bounds.
27-Jan-16 Analysis of Algorithms. 2 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based.
Selection Sort main( ) { int a[ ] = { 17, 6, 13,12, 2 } ; int i, j, t ; for ( i = 0 ; i
Searching Topics Sequential Search Binary Search.
Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.
SORTING Sorting is storage of data in some order, it can be in ascending or descending order. The term Sorting comes along-with the term Searching. There.
Chapter 3 Chapter Summary  Algorithms o Example Algorithms searching for an element in a list sorting a list so its elements are in some prescribed.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Complexity Analysis (Part I)
Insertion sort Loop invariants Dynamic memory
Analysis of Algorithms
Algorithm Analysis: Big O Notation
Introduction to Search Algorithms
Introduction to Algorithms
Algorithm Analysis CSE 2011 Winter September 2018.
Quicksort analysis Bubble sort
Sorting … and Insertion Sort.
Introduction to Algorithm and its Complexity Lecture 1: 18 slides
Complexity Analysis (Part I)
the fourth iteration of this loop is shown here
Complexity Analysis (Part I)
Presentation transcript:

CMPT 225 Sorting Algorithms Algorithm Analysis: Big O Notation

Objectives Determine the running time of simple algorithms in the: Best case Average case Worst case Sorting algorithms Understand the mathematical basis of O notation Use O notation to measure the running time of algorithms

Algorithm Analysis It is important to be able to describe the efficiency of algorithms Time efficiency Space efficiency Choosing an appropriate algorithm can make an enormous difference in the usability of a system e.g. Government and corporate databases with many millions of records, which are accessed frequently Online search engines Real time systems (from air traffic control systems to computer games) where near instantaneous response is required

Measuring Efficiency of Algorithms It is possible to time algorithms System.currentTimeMillis() returns the current time so can easily be used to measure the running time of an algorithm More sophisticated timer classes exist It is possible to count the number of operations that an algorithm performs Either by a careful visual walkthrough of the algorithm or by Printing the number of times that each line executes (profiling)

Timing Algorithms It can be very useful to time how long an algorithm takes to run In some cases it may be essential to know how long a particular algorithm takes on a particular system However, it is not a good general method for comparing algorithms Running time is affected by numerous factors CPU speed, memory, specialized hardware (e.g. graphics card) Operating system, system configuration (e.g. virtual memory), programming language, algorithm implementation Other tasks (i.e. what other programs are running), timing of system tasks (e.g. memory management) Particular input used.

Cost Functions Because of the sorts of reasons just discussed for general comparative purposes we will count, rather than time, the number of operations that an algorithm performs Note that this does not mean that actual running time should be ignored! If algorithm (on some particular input) performs t operations, we will say that it runs in time t. Usually running time t depends on the data size (the input length). We express the time t as a cost function of the data size n We denote the cost function of an algorithm A as t A (), where t A (n) is the time required to process the data with algorithm A on input of size n

Best, Average and Worst Case The amount of work performed by an algorithm may vary based on its input (not only on its size) This is frequently the case (but not always) Algorithm efficiency is often calculated for three, general, cases of input Best case Average (or “usual”) case Worst case

Cost Functions of Sorting Algorithms

Simple Sorting As an example of algorithm analysis let's look at two simple sorting algorithms Selection Sort and Insertion Sort We'll calculate an approximate cost function for these sorting algorithms by analyzing exactly how many operations are performed by each algorithm Note that this will include an analysis of how many times the algorithms perform loops

Comparing sorting algorithms Measures used: The total number of operations (usually are not important) The number of comparisons (most common; in Java comparisons are expansive operation) The number of times an element is moved (in Java moving elements is cheap as references are always used, but in C++ can be expansive).

Selection Sort while some elements unsorted: Find the smallest element in the unsorted section; this requires all of the unsorted items to be compared to find the smallest Swap the smallest element with the first (left-most) element in the unsorted section the fourth iteration of this loop is shown here smallest so far:453622

Selection Sort Algorithm public void selectionSort(Comparable[] arr) { for (int i = 0; i < arr.length-1; ++i) { int smallest = i; // Find the index of the smallest element for (int j = i + 1; j < arr.length; ++j) { if (arr[j].compareTo(arr[smallest])<0) { smallest = j; } // Swap the smallest with the current item Comparable temp = arr[i]; arr[i] = arr[smallest]; arr[smallest] = temp; }

A selection sort of an array of five integers (using the algorithm in the textbook, in which the first part of array is unsorted and the second (bold) is sorted. Instead of the smallest element we have to look for the biggest element.

Selection Sort: Number of Comparisons Elements in unsorted Comparisons to find min nn-1 n-2 …… n(n-1)/2

Selection Sort Analysis public void selectionSort(Comparable[] arr) { for (int i = 0; i < arr.length-1; ++i) { // outer for loop is evaluated n-1 times int smallest = i; //n-1 times again for (int j = i + 1; j < arr.length; ++j) { // evaluated n(n-1)/2 times if (arr[j].compareTo(arr[smallest])<0) { // n(n-1)/2 comparisons smallest = j; //how many times? (*) } } Comparable temp = arr[i]; //n-1 times arr[i] = arr[smallest]; //n-1 times arr[smallest] = temp; //n-1 times } }

Selection Sort: Cost Function There is 1 operation needed to initializing the outer loop The outer loop is evaluated n-1 times 7 instructions (these include the outer loop comparison and increment, and the initialization of the inner loop) Cost is 7(n-1) The inner loop is evaluated n(n-1)/2 times There are 4 instructions in the inner loop, but one (*) is only evaluated sometimes Worst case cost upper bound: 4(n(n-1)/2) Total cost: 1 + 7(n-1) + 4(n(n-1)/2) [worst case] Assumption: that all instructions have the same cost

Selection Sort: Summary Number of comparisons: n(n-1)/2 The best case time cost: 1 + 7(n-1) + 3(n(n-1)/2) (array was sorted) The worst case time cost (an upper bound): ·1 + 7(n-1) + 4(n(n-1)/2) (the real worst case time cost: 1+7(n-1)+3n(n-1)/2+n 2 /4) ) The number of swaps: n-1 [number of moves: 3(n-1)]