1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Sorting Comparison-based algorithm review –You should know most of the algorithms –We will concentrate on their analyses –Special emphasis: Heapsort Lower.
Practice Quiz Question
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
Sorting Algorithms and Average Case Time Complexity
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
CS203 Programming with Data Structures Sorting California State University, Los Angeles.
CS 171: Introduction to Computer Science II Quicksort.
Sorting Heapsort Quick review of basic sorting methods Lower bounds for comparison-based methods Non-comparison based sorting.
Quicksort. Quicksort I To sort a[left...right] : 1. if left < right: 1.1. Partition a[left...right] such that: all a[left...p-1] are less than a[p], and.
CPSC 411, Fall 2008: Set 2 1 CPSC 411 Design and Analysis of Algorithms Set 2: Sorting Lower Bound Prof. Jennifer Welch Fall 2008.
Quicksort.
Sorting Chapter 10.
TTIT33 Algorithms and Optimization – Dalg Lecture 2 HT TTIT33 Algorithms and optimization Lecture 2 Algorithms Sorting [GT] 3.1.2, 11 [LD] ,
Divide and Conquer Sorting
CSE 326: Data Structures Sorting Ben Lerner Summer 2007.
Sorting Lower Bound Andreas Klappenecker based on slides by Prof. Welch 1.
Analysis of Algorithms CS 477/677
Sorting CS-212 Dick Steflik. Exchange Sorting Method : make n-1 passes across the data, on each pass compare adjacent items, swapping as necessary (n-1.
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
Sorting Chapter 6 Chapter 6 –Insertion Sort 6.1 –Quicksort 6.2 Chapter 5 Chapter 5 –Mergesort 5.2 –Stable Sorts Divide & Conquer.
Sorting II/ Slide 1 Lecture 24 May 15, 2011 l merge-sorting l quick-sorting.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
CSE 373 Data Structures Lecture 19
Computer Algorithms Lecture 10 Quicksort Ch. 7 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Sorting and Asymptotic Complexity
CSE 373 Data Structures Lecture 15
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input.
1 Chapter 1 Analysis Basics. 2 Chapter Outline What is analysis? What to count and consider Mathematical background Rates of growth Tournament method.
Order Statistics The ith order statistic in a set of n elements is the ith smallest element The minimum is thus the 1st order statistic The maximum is.
Chapter 12 Recursion, Complexity, and Searching and Sorting
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
Sorting Fun1 Chapter 4: Sorting     29  9.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
Analysis of Algorithms CS 477/677
Sorting CSIT 402 Data Structures II. 2 Sorting (Ascending Order) Input ›an array A of data records ›a key value in each data record ›a comparison function.
Sort Algorithms.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
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.
COSC 3101A - Design and Analysis of Algorithms 6 Lower Bounds for Sorting Counting / Radix / Bucket Sort Many of these slides are taken from Monica Nicolescu,
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
SORTING AND ASYMPTOTIC COMPLEXITY Lecture 13 CS2110 – Fall 2009.
CSE 250 – Data Structures. Today’s Goals  First review the easy, simple sorting algorithms  Compare while inserting value into place in the vector 
Sorting and Runtime Complexity CS255. Sorting Different ways to sort: –Bubble –Exchange –Insertion –Merge –Quick –more…
CS6045: Advanced Algorithms Sorting Algorithms. Sorting Input: sequence of numbers Output: a sorted sequence.
CMPT 438 Algorithms.
Sorting.
Sorting by Tammy Bailey
CSE 373 Data Structures and Algorithms
The Selection Problem.
Presentation transcript:

1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends on implementation, compiler, architecture So let's use a different measure of time e.e. number of steps/simple operations Space Amount of temporary storage required We don’t count the input

2 Time Analysis Goals: Compute the running time of an algorithm. Compare the running times of algorithms that solve the same problem Observations: Since the time it takes to execute an algorithm usually depends on the size of the input, we express the algorithm's time complexity as a function of the size of the input.

3 Time Analysis Observations: Since the time it takes to execute an algorithm usually depends on the size of the input, we express the algorithm's time complexity as a function of the size of the input. Two different data sets of the same size may result in different running times e.g. a sorting algorithm may run faster if the input array is already sorted. As the size of the input increases, one algorithm's running time may increase much faster than another's The first algorithm will be preferred for small inputs but the second will be chosen when the input is expected to be large.

4 Time Analysis Ultimately, we want to discover how fast the running time of an algorithm increases as the size of the input increases. This is called the order of growth of the algorithm Since the running time of an algorithm on an input of size n depends on the way the data is organized, we'll need to consider separate cases depending on whether the data is organized in a "favorable" way or not.

5 Time analysis Best case analysis Given the algorithm and input of size n that makes it run fastest (compared to all other possible inputs of size n), what is the running time? Worst case analysis Given the algorithm and input of size n that makes it run slowest (compared to all other possible inputs of size n), what is the running time? A bad worst-case complexity doesn't necessarily mean that the algorithm should be rejected. Average case analysis Given the algorithm and a typical, average input of size n, what is the running time?

6 Time Analysis Iterative algorithms Concentrate on the time it takes to execute the loops Recursive algorithms Come up with a recursive function expressing the time and solve it.

7 Example: Sorting Sorting algorithm: insertion sort The idea: Divide the array in two imaginary parts: sorted, unsorted The sorted part is initially empty Pick the first element from the unsorted part and insert it in its correct slot in the sorted part. Do the insertion by traversing the sorted part to find where the element should be placed. Shift the other elements over to make room for it. Repeat the process. This algorithm is very efficient for sorting small arrays

8 Example continued As we "move" items to the sorted part of the array, this imaginary wall between the parts moves towards the end of the array. When it reaches the edge, we are done!

9 Example continued Input: array A of size n Output: A, sorted in ascending order function InsertionSort(A[1..n]) begin for i:=2 to n item := A[i] j := i - 1 while (j > 0 and A[j] > item) A[j+1] := A[j] j := j - 1 end while A[j+1] := item end for end

10 Example continued times executed n n-1  t j  (t j -1) n-1 Input: array A of size n Output: A, sorted in ascending order function InsertionSort(A[1..n]) begin for i:=2 to n item := A[i] j := i - 1 while (j > 0 and A[j] > item) A[j+1] := A[j] j := j - 1 end while A[j+1] := item end for end depends on input

11 Example continued Best case for insertion sort The best case occurs when the array is already sorted. Then, the while loop is not executed and the running time of the algorithm is a linear function of n. Worst case for insertion sort The worst case occurs when the array is sorted in reverse. Then, for each value of i, the while loop is executed i-1 times. The running time of the algorithm is a quadratic function of n. Average case for insertion sort On average, the while loop is executed i/2 times for each value of i. The running time is again a quadratic function of n (albeit with a smaller coefficient than in the worst case)

12 Algorithm Analysis Observations Some terms grow faster than others as the size of the input increases ==> these determine the rate of growth Example 1: In n 2 +4n, n 2 grows much faster than n. We say that this is a quadratic function and concentrate on the term n 2. Example 2: Both n 2 +4n and n 2 are quadratic functions. They grow at approximately the same rate. Since they have the same rate of growth, we consider them "equivalent". We are interested in finding upper (and lower) bounds for the order of growth of an algorithm.

13 Sorting: Lower bounds We found that the running time of insertion sort is quadratic. Can we perhaps do better? Comparison sort = A family of algorithms that use comparisons to determine the sorted order of a collection Example: insertion sort Decision tree = a tree that represents the comparisons performed by a sorting algorithm on input of given size

14 Sorting: Lower bounds Decision tree for 3 elements: b ? c a ? b a ? c b ? c  (a, b, c) (a, c, b ) (c, a, b )(c, b a )(b, c, a ) (b, a, c )         Each internal node contains a comparison Each leaf contains a permutation Algorithm execution = a path from the root to a leaf

15 Sorting: Lower bounds Each internal node contains a comparison Each leaf contains a permutation Algorithm execution = a path from the root to a leaf Worst-case number of comparisons = height of decision tree Idea: If we find a lower bound on the height of the decision tree, we’ll have a lower bound on the running time of any comparison-based sorting algorithm A decision tree that sorts n elements has height at least nlgn This means that comparison-based sorting algorithms whose worst- case running time is at most nlgn are optimal!

16 MergeSort Main idea: break the list in two sort each half recursively base case: a single-element array is already sorted merge the two halves

17 Sorting: MergeSort void mergesort (int array[ ], int low, int high) { int mid; if (low < high) { mid=(low+high)/2; mergesort(array, low, mid); mergesort(array, mid+1, high); merge(array, low, mid, mid+1, high); } /* merge() is used to merge the two sorted subarrays, [low..mid] and [mid+1..high] into one sorted array. This is done by selecting the smallest element at the front of each subarray and placing it in the "final" array. */

18 Sorting: MergeSort Running time: Time to mergesort n items = twice the time to mergesort n/2 items + the time to merge a total of n items T(n) = 2*T(n/2) + n =...=O(nlgn)

19 Quicksort Basic idea: Pick an element (called pivot) Partition the array in two subsequences: those smaller than or equal to the pivot and those larger than or equal to the pivot Sort each subsequence recursively

20 Quicksort Partitioning an array Goal : move all elements smaller than or equal to the pivot to the left of the array and all elements larger than or equal to the pivot to the right of the array. In the end, the right part of the array will contain elements that are larger than or equal to those in the left part.

21 Quicksort Running time: depends on selection of pivot Best case (array partitioned in half every time) time to quicksort n elements = time to partition array + twice the time to quicksort n/2 elements T(n) = 2*T(n/2) + n =... = O(nlgn)

22 Quicksort Running time: depends on selection of pivot Average case Still O(nlgn) but with a slightly larger constant Worst case: Partitioning n elements results in two subsequences of lengths 1 and n-1 T(n) = T(n-1) + n =... =O(n 2 )

23 Stable sorting algorithms A sorting algorithm is stable if it does not change the relative order of items with the same value. This is important when satellite data is carried around with the element being sorted. Example: ATM transactions where the key is the account number. Initially the transactions are ordered by time, but if we sort them by account number, the relative order of the transactions must stay the same. Insertion sort and mergesort are stable. Quicksort is not stable

24 Space requirements Mergesort needs additional space (n) for the temporary array. All other algorithms that we saw sort the arrays in- place and need only constant additional space for temporary variables.

25 Comparison Insertion sort Speed: very good in the best case or on partially sorted arrays, bad in the worst case. Very little overhead Space: constant Stability: stable When to use: when the array is small or we already know that it's almost sorted. Mergesort Speed: optimal time in all cases. Overhead due to recursion. Space: requires a copy of the array (extra n). Extra stack space for recursion. Stability: stable When to use: when sorting a very large array, when time is critical, when space is not an issue. Quicksort Speed: optimal best/average case, bad worst case. Overhead due to recursion. Space: constant. Extra stack space for recursion Stability: not stable When to use: when sorting a large array and time is not critical. Fast in practice.