QuickSort 4 February 2003. 2 QuickSort(S) Fast divide and conquer algorithm first discovered by C. A. R. Hoare in 1962. 1.If the number of elements in.

Slides:



Advertisements
Similar presentations
Chapter 9 continued: Quicksort
Advertisements

Introduction to Algorithms Quicksort
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Practice Quiz Question
Sorting Chapter 8 CSCI 3333 Data Structures.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
CS4413 Divide-and-Conquer
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search.
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
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.
Quicksort COMP171 Fall Sorting II/ Slide 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N.
Chapter 7: Sorting Algorithms
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
CSC 2300 Data Structures & Algorithms March 23, 2007 Chapter 7. Sorting.
CMPS1371 Introduction to Computing for Engineers SORTING.
Updated QuickSort Problem From a given set of n integers, find the missing integer from 0 to n using O(n) queries of type: “what is bit[j]
Data Structures and Algorithms PLSD210 Sorting. Card players all know how to sort … First card is already sorted With all the rest, ¶Scan back from the.
Quicksort Divide-and-Conquer. Quicksort Algorithm Given an array S of n elements (e.g., integers): If array only contains one element, return it. Else.
Fundamentals of Algorithms MCS - 2 Lecture # 16. Quick Sort.
CS 206 Introduction to Computer Science II 12 / 09 / 2009 Instructor: Michael Eckmann.
Sorting21 Recursive sorting algorithms Oh no, not again!
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
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.
Quicksort. 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N 2 ) n But, the worst case seldom.
Quicksort.
Quicksort.
Quicksort
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
CS 206 Introduction to Computer Science II 12 / 08 / 2008 Instructor: Michael Eckmann.
Sorting II/ Slide 1 Lecture 24 May 15, 2011 l merge-sorting l quick-sorting.
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
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.
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.
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
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.
1 Sorting Algorithms Sections 7.1 to Comparison-Based Sorting Input – 2,3,1,15,11,23,1 Output – 1,1,2,3,11,15,23 Class ‘Animals’ – Sort Objects.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Sorting: Implementation Fundamental Data Structures and Algorithms Klaus Sutner February 24, 2004.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
1 Algorithms CSCI 235, Fall 2015 Lecture 19 Order Statistics II.
Chapter 9 Sorting 1. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step.
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.
HeapSort 25 March HeapSort Heaps or priority queues provide a means of sorting: 1.Construct a heap, 2.Add each item to it (maintaining the heap.
Quicksort Quicksort is a well-known sorting algorithm that, in the worst case, it makes Θ(n 2 ) comparisons. Typically, quicksort is significantly faster.
Quicksort This is probably the most popular sorting algorithm. It was invented by the English Scientist C.A.R. Hoare It is popular because it works well.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Quicksort
Chapter 7 Sorting Spring 14
Data Structures and Algorithms
Quicksort 1.
Quicksort and Mergesort
Chapter 4: Divide and Conquer
Quick Sort (11.2) CSE 2011 Winter November 2018.
Unit-2 Divide and Conquer
Chapter 4.
EE 312 Software Design and Implementation I
Quicksort.
CS 1114: Sorting and selection (part two)
Quicksort.
Design and Analysis of Algorithms
Algorithm Efficiency and Sorting
Quicksort.
Presentation transcript:

QuickSort 4 February 2003

2 QuickSort(S) Fast divide and conquer algorithm first discovered by C. A. R. Hoare in If the number of elements in S is 0 or 1, then return 2.Pick any element v  S. Call v the pivot. 3.Partition S – {v} (the remaining elements of S) into two disjoint groups: L = {x  S - {v} | x  v} and R = {x  S - {v} | x>v} 4.Return the result of QuickSort(L) followed by, v, followed by QuickSort(R).

3 How QuickSort Works There are two major phases 1. the sort phase 2. partition phase 3. The sort phase simply sorts the two smaller problems that are generated in the partition phase. QuickSort is a good example of the divide and conquer strategy for solving problems. (binary search) 4. In QuickSort, we divide the array of items to be sorted into two partitions and then call the QuickSort procedure recursively to sort the two partitions, i.e. we divide the problem into two smaller ones and conquer by solving the smaller ones.

4 Conquer Thus the conquer part of the QuickSort routine looks like this:  pivot > pivot pivotlowhigh  pivot > pivot pivotlowhigh pivot > pivot high LR L R R

5 Pivot Element For the strategy to be effective, the partition phase must ensure that all the items in one part (the lower part) and less than all those in the other (upper) part. We choose a pivot element and arrange that all the items in the lower part are less than (or equal to) the pivot and all those in the upper part greater than it. In the most general case, we don't know anything about the items to be sorted, so that any choice of the pivot element will do - the first element is a convenient one. In the final step, the pivot is dropped into the remaining slot between those smaller and those larger.

6 QuickSort in Place Most implementations of QuickSort make use of the fact that you can partition in place by keeping two pointers – One moves from the right and a second moves from the left. – They move towards the center until the right pointer finds an element less than the pivot and the left one finds an element greater than the pivot. – These two elements are then swapped. – The pointers are then moved inward again until they “cross over”. – The pivot is then swapped into the slot to which the right pointer points when the partition is complete.

7 Example 17, 5, 34, 2, 6, 12, 28, 3, 7, 10, 13, 20 17, 5, 13, 2, 6, 12, 28, 3, 7, 10, 34, 20 17, 5, 13, 2, 6, 12, 10, 3, 7, 28, 34, 20 7, 5, 13, 2, 6, 12, 10, 3, 17, 28, 34, 20 Stack longer, sort shorter

8 Example 7, 5, 13, 2, 6, 12, 10, 3 7, 5, 3, 2, 6, 12, 10, 13 6, 5, 3, 2, 7, 12, 10, 13 12, 10, 13 10, 12, 13 6, 5, 3, 2, 7 2, 5, 3, 6, 7 2, 5, 3 2, 3, 5

9 Analysis The partition routine examines every item in the array at most once, so it is clearly O(n). Usually, the partition routine will divide the problem into two roughly equal sized partitions. We know that we can divide n items in half log(n) times. This makes QuickSort an O(n log(n)) algorithm if things break just right.

10 Quick Sort - The Facts! QuickSort has a serious limitation, which must be understood before using it in certain applications. What happens if we apply QuickSort to an already sorted array? This is certainly a case where we expect the performance to be good! However, the first attempt to partition the problem into two problems will return an empty lower partition - the first element is the smallest. Thus, the first partition call simply chops off one element and calls QuickSort for a partition with n-1 items! This happens n-2 more times! Each partition call still requires O(n) operations - and we have generated O(n) such calls.

11 Worst Case In the worst case, QuickSort is an O(n 2 ) algorithm!

12 Animation 210/qsort.html 210/qsort.html trier.de/~naeher/Professur/deut/download.html trier.de/~naeher/Professur/deut/download.html

13 Choosing a Pivot A number of variations to the simple QuickSort will generally produce better results: rather than choose the first item as the pivot, some other strategies work better.

14 Median-of-3 Pivot Median-of-3 pivot approach selects three candidate pivots and uses the median one. If the three pivots are chosen from the first, middle and last positions, then it is easy to see that for the already sorted array, this will produce an optimum result: each partition will be exactly half (±one element) of the problem and we will need exactly  (logn)  recursive calls. However, whatever strategy we use for choosing the pivot, it is possible to propose a pathological case in which the problem is not divided equally at any partition stage.

15 Random pivot Random pivot simply uses a randomly chosen pivot. This also works fine for sorted arrays - on average the pivot will produce two equal sized partitions and there will be O(logn) of them.

16 Be Careful Whatever strategy we use for choosing the pivot, it is possible to propose a pathological case in which the problem is not divided equally at any partition stage. Thus QuickSort must always be treated as potentially O(n 2 ). Thus QuickSort must always be treated as potentially O(n 2 ).

17 QuickSort is Method of Choice Thus, when an occasional “blowout” to O(n 2 ) is tolerable, we can expect that, on average, QuickSort provides considerably better performance - especially if one of the modified pivot choice procedures is used. Most commercial applications would use QuickSort for its better average performance: they can tolerate an occasional long run in return for shorter runs most of the time.

18 Nothing is Guaranteed QuickSort should never be used in applications which require a guarantee of response time, unless it is treated as an O(n 2 ) algorithm in calculating the worst-case response time. If you have to assume O(n 2 ) time, then - if n is small, you're better off using insertion sort - which has simpler code and therefore smaller constant factors.

19 Speeding up QuickSort The recursive calls in QuickSort are generally expensive on most architectures - the overhead of any procedure call is significant and reasonable improvements can be obtained with equivalent iterative algorithms. Two things can be done to “eke” a little more performance out of your processor when sorting. (See next slide)

20 Fine Tuning 1.QuickSort- in it’s usual recursive form - has a reasonably high constant factor relative to a simpler sort such as insertion sort. Thus, when the partitions become small (n < ~10), a switch to insertion sort for the small partition will usually show a measurable speed-up. (The point at which it becomes effective to switch to the insertion sort is extremely sensitive to architectural features and needs to be determined for any target processor: although a value of ~10 is a reasonable guess!) 2.Write the whole algorithm in an iterative form.