Sorting Suppose you wanted to write a computer game like Doom 4: The Caverns of Calvin… How do you render those nice (lurid) pictures of Calvin College.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Theory of Computing Lecture 3 MAS 714 Hartmut Klauck.
Sorting Comparison-based algorithm review –You should know most of the algorithms –We will concentrate on their analyses –Special emphasis: Heapsort Lower.
Introduction to Algorithms Jiafen Liu Sept
Divide And Conquer Distinguish between small and large instances. Small instances solved differently from large ones.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Quicksort Quicksort     29  9.
Quicksort CSE 331 Section 2 James Daly. Review: Merge Sort Basic idea: split the list into two parts, sort both parts, then merge the two lists
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
Chapter 7: Sorting Algorithms
Internal Sorting A brief review and some new ideas CS 400/600 – Data Structures.
Introduction to Algorithms Chapter 7: Quick Sort.
Lecture 7COMPSCI.220.FS.T Algorithm MergeSort John von Neumann ( 1945 ! ): a recursive divide- and-conquer approach Three basic steps: –If the.
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.
Quick Sort. Quicksort Quicksort is a well-known sorting algorithm developed by C. A. R. Hoare. The quick sort is an in-place, divide- and-conquer, massively.
CS 171: Introduction to Computer Science II Quicksort.
1 TCSS 342, Winter 2005 Lecture Notes Sorting Weiss Ch. 8, pp
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.
TTIT33 Algorithms and Optimization – Dalg Lecture 2 HT TTIT33 Algorithms and optimization Lecture 2 Algorithms Sorting [GT] 3.1.2, 11 [LD] ,
Quicksort
Analysis of Algorithms CS 477/677
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
Sorting Suppose you wanted to write a computer game like Doom 4: The Caverns of Calvin… How do you render those nice (lurid) pictures of Calvin College.
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.
Computer Algorithms Lecture 10 Quicksort Ch. 7 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Divide-And-Conquer Sorting Small instance.  n
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.
Sorting and Lower bounds Fundamental Data Structures and Algorithms Ananda Guna January 27, 2005.
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.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
Analysis of Algorithms CS 477/677
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.
Sorting: Implementation Fundamental Data Structures and Algorithms Klaus Sutner February 24, 2004.
Quicksort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Divide And Conquer A large instance is solved as follows:  Divide the large instance into smaller instances.  Solve the smaller instances somehow. 
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.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Sorting Fundamental Data Structures and Algorithms Aleks Nanevski February 17, 2004.
Divide and Conquer Sorting Algorithms COMP s1 Sedgewick Chapters 7 and 8.
IS 2610: Data Structures Discuss HW 2 problems Binary Tree (continued) Introduction to Sorting Feb 9, 2004.
CSE 326: Data Structures Lecture 23 Spring Quarter 2001 Sorting, Part 1 David Kaplan
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Sorting.
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.
Sorting Lower Bounds n Beating Them. Recap Divide and Conquer –Know how to break a problem into smaller problems, such that –Given a solution to the smaller.
Sorting: Implementation Fundamental Data Structures and Algorithms Margaret Reid-Miller 24 February 2004.
SORTING AND ASYMPTOTIC COMPLEXITY Lecture 13 CS2110 – Fall 2009.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
CS6045: Advanced Algorithms Sorting Algorithms. Sorting So Far Insertion sort: –Easy to code –Fast on small inputs (less than ~50 elements) –Fast on nearly-sorted.
David Luebke 1 6/26/2016 CS 332: Algorithms Linear-Time Sorting Continued Medians and Order Statistics.
David Luebke 1 7/2/2016 CS 332: Algorithms Linear-Time Sorting: Review + Bucket Sort Medians and Order Statistics.
Chapter 7 Sorting Spring 14
Description Given a linear collection of items x1, x2, x3,….,xn
Chapter 4: Divide and Conquer
Quick Sort (11.2) CSE 2011 Winter November 2018.
CO 303 Algorithm Analysis And Design Quicksort
Sub-Quadratic Sorting Algorithms
slides adapted from Marty Stepp
Quicksort.
CSE 373 Data Structures and Algorithms
Data Structures & Algorithms
Algorithms CSCI 235, Spring 2019 Lecture 17 Quick Sort II
Quicksort.
Presentation transcript:

Sorting Suppose you wanted to write a computer game like Doom 4: The Caverns of Calvin… How do you render those nice (lurid) pictures of Calvin College torture chambers, with hidden surfaces removed? Given a collection of polygons (points, tests, values), how do you sort them? My favorite sort: What are your favorite sorts? Read , omit rest of chapter 6.

Simple (and slow) algorithms Bubble sort: Selection Sort: Insertion Sort: Which is best? important factors: comparisons, data movement

Sorting out Sorting A collection or file of items with keys Sorting may be on items or pointers Sorting may be internal or external Sorting may or may not be stable Simple algorithms: easy to implement slow (on big sets of data) show the basic approaches, concepts May be used to improve fancier algorithms

Sorting Utilities We’d like our sorting algorithms to work with all data types… template void exch(Item &A, Item &B) {Item t=A; A=B; B=t; } template void compexch(Item &A, Item &B) {if (B<A) exch(A, B); }

Bubble Sort The first sort most students learn And the worst… template void bubble(Item a[], int l, int r) { for (int i=l; i<r; i++) for (int j=r; j>i; j--) compexch(a[j-1], a[j]); } comparisons?something like n 2 /2 date movements?something like n 2 /2

Selection Sort Find smallest element Exchange with first Recursively sort rest template void selection(Item a[], int l, int r) { for (int i=1; i<r; i++) { int min=i; for (int j=i+1; j<=r; j++) if (a[j]<a[min]) min=j; exch(a[i], a[min]); } comparisons?n 2 /2 swaps?n

Insertion Sort Like sorting cards Put next one in place template void insertion(Item a[], int l, int r) { int i; for (i=r; i>l; i--) compexch(a[i-1],a[i]); for (i=l+2; i<=r; i++) { int j=i; Item v=a[i]; while (v<a[j-1]) { a[j] = a[j-1]; j--; } a[j] = v; } comparisons?n 2 /4 data moves? n 2 /4

Which one to use? Selection: few data movements Insertion: few comparisons Bubble: blows But all of these are  (n 2 ), which, as you know, is TERRIBLE for large n Can we do better than  (n 2 )?

Merge Sort The quintessential divide-and-conquer algorithm Divide the list in half Sort each half recursively Merge the results. Base case: left as an exercise to the reader

Merge Sort Analysis Recall runtime recurrence: T(1)=0; T(n) = 2T(n/2) + cn  (n log n) runtime in the worst case Much better than the simple sorts on big data files – and easy to implement! Can implement in-place and bottom-up to avoid some data movement and recursion overhead Still, empirically, it’s slower than Quicksort, which we’ll study next.

Quicksort Pick a pivot; pivot list; sort halves recursively. The most widely used algorithm A heavily studied algorithm with many variations and improvements (“it seems to invite tinkering”) A carefully tuned quicksort is usually fastest (e.g. unix’s qsort standard library function) but not stable, and in some situations slooow…

Quicksort template void qsort(Item a[], int l, int r) { if (r<=l) return; int i=partition(a, l, r); qsort(a, l, i-1); qsort(a, i+1, r); } partition: pick an item as pivot, p (last item?) rearrange list into items smaller, equal, and greater than p

Partitioning template int partition(Item a[], int l, int r) { int i=l-1, j=r; Item v=a[r]; for (;;) { while (a[++i] < v) ; while (v<a[--j]) if (j==l) break; if (i >= j) break; exch(a[i], a[j]); } exch(a[i], a[r]); return i; }

Quicksort Analysis What is the runtime for Quicksort? Recurrence relation? Worst case:  (n 2 ) Best, Average case:  (n log n) When does the worst case arise?  when the list is (nearly) sorted! oops… Recursive algorithms also have lots of overhead. How to reduce the recursion overhead?

Quick Hacks: Cutoff How to improve the recursion overhead? Don’t sort lists of size <= 10 (e.g.) At the end, run a pass of insertion sort. In practice, this speeds up the algorithm

Quick Hacks: Picking a Pivot How to prevent that nasty worst-case behavior? Be smarter about picking a pivot E.g. pick three random elements and take their median Again, this yields an improvement in empirical performance: the worst case is much more rare (what would have to happen to get the worst case?)

Median, Order Statistics Quicksort improvement idea: use the median as pivot Give me an algorithm to: find the smallest element of a list find the 4 th smallest element find the k th smallest element Algorithm idea: sort, then pick the middle element.   (n log n) worst, average case.  This won’t help for quicksort!  Can we do better?

Quicksort-based selection Pick a pivot; partition list. Let i be location of pivot. If i>k search left part; if i<k search right part template void select(Item a[], int l, int r, int k) { if (r <= l) return a[r]; int i = partition(a, l, r); if (i > k) return select(a, l, i-1, k); if (i < k) return select(a, i+1, r, k); } Worst-case runtime? O(n 2 ) Expected runtime? O(n)

Lower Bound on Sorting Do you think that there will always be improvements in sorting algorithms? better than  (n)? better than  (n log n)? how to prove that no comparison sort is better than  (n log n) in the worst case?  consider all algorithms!? Few non-trivial lower bounds are known. Hard! But, we can say that the runtime for any comparison sort is  (n log n).

Comparison sort lower bound How many comparisons are needed to sort? decision tree: each leaf a permutation; each node a comparison: a < b? A sort of a particular list: a path from root to leaf. How many leaves? n! Shortest possible decision tree?  (log n!) Stirling’s formula (p. 43): lg n! is about n lg n – n lg e + lg(sqrt(2 pi n))  (n log n)! There is no comparison sort better than  (n log n) (but are there other approaches to sorting?)