This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Illinois Institute of Technology Department of Computer Science Lecture 3: January 28, 2009 CS 430 Introduction to Algorithms Spring Semester, 2009 How to Sort A Sequence of Numbers Defining the problem Input: A sequence of n numbers a 1 , a 2 , , a n Output: A permutation (reordering) of the input sequence such that a 1 a 2 a n Insertion Sort Referring to the pseudocode for insertion sort on page 17 of CLRS, if we want to analyze the time complexity of insertion sort, we need to answer the following question: How often will line 6 be exceuted? Obviously, insertion sort will be very fast if file already is nearly sorted; if it is actually sorted, insertion sort uses ( n ) comparisons; this is the best case. Proposition: Insertion sort uses O ( n 2 ) comparisons in the worst case as well as on the average (assuming all permutations are equally likely). Proof: We need to compute be number of elements to the left of a i and larger than it. Call this d i (this is called the number of inversions relative to a i ; M = n i =1 d i is the number of inversions of the input permutation....
View Full
Document
 Spring '08
 KAPOOR
 Algorithms, Sort

Click to edit the document details