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 4: February 2, 2009 CS 430 Introduction to Algorithms Spring Semester, 2009 1 Quicksort Quicksort is likely the most commonly used algorithm for sorting internally. Its analysis is also of particular interest because its performance on average is asymptotically faster than its worstcase performance. Although there are many variants, the basic form of the algorithm selects the first element of the array and partitions the array around the value of that element: that is, it divides the array into two sections, one consisting of all elements smaller than the first element and the second consisting of all elements larger than the first element. It then sorts each partition recursively. 1 To analyze the quicksort algorithm, we must ask how it behaves in the best case, in the average case, and in the worst case. The best case occurs when all of the partitions are perfectly balanced—that is, when the array is partitioned evenly in each recursive call. The worst case occurs when the input array is already sorted, as in that case the partitions are all unbalanced. What are we counting in our analyses? In most sorting algorithms, including quicksort, the running time is proportional to the number of comparisons of pairs of elements. To simplify matters, then, we only count comparisons of pairs of elements. In general, what is the form of the recurrence we will be using? In an array of size n , partitioning can be accomplished in n 1 comparisons. If partitioning yields one subarray of size k and one subarray of size n 1 k , the general recurrence is of the form c ( n ) = n 1 + c ( k ) + c ( n 1 k ) 1.1 Bestcase analysis In the best case, the partitions are balanced at all levels of the recursion. Thus, in the general form above, k = § n 1 2 ¨ . This yields the recurrence c (...
View
Full Document
 Spring '08
 KAPOOR
 Algorithms, Sort, Analysis of algorithms, Computational complexity theory, Best, worst and average case, best case

Click to edit the document details