1 CSE 326: Data Structures Sorting by Comparison Zasha Weinberg in lieu of Steve Wolfman Winter Quarter 2000.

Slides:



Advertisements
Similar presentations
CPSC 411 Design and Analysis of Algorithms Set 2: Sorting Lower Bound Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 2 1.
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Theory of Computing Lecture 3 MAS 714 Hartmut Klauck.
Using Divide and Conquer for Sorting
Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search.
Spring 2015 Lecture 5: QuickSort & Selection
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
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.
Quick-Sort     29  9.
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
CSC 2300 Data Structures & Algorithms March 27, 2007 Chapter 7. Sorting.
CS 206 Introduction to Computer Science II 12 / 05 / 2008 Instructor: Michael Eckmann.
General Computer Science for Engineers CISC 106 James Atlas Computer and Information Sciences 10/23/2009.
2 -1 Analysis of algorithms Best case: easiest Worst case Average case: hardest.
CPSC 411, Fall 2008: Set 2 1 CPSC 411 Design and Analysis of Algorithms Set 2: Sorting Lower Bound Prof. Jennifer Welch Fall 2008.
CS 206 Introduction to Computer Science II 12 / 03 / 2008 Instructor: Michael Eckmann.
Sorting. Introduction Assumptions –Sorting an array of integers –Entire sort can be done in main memory Straightforward algorithms are O(N 2 ) More complex.
Sorting Lower Bound Andreas Klappenecker based on slides by Prof. Welch 1.
CSC 2300 Data Structures & Algorithms March 20, 2007 Chapter 7. Sorting.
Sorting Lower Bound1. 2 Comparison-Based Sorting (§ 4.4) Many sorting algorithms are comparison based. They sort by making comparisons between pairs of.
CSE 373 Data Structures Lecture 19
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
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.
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.
CSE373: Data Structure & Algorithms Lecture 22: More Sorting Linda Shapiro Winter 2015.
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.
1 CSE 373 Sorting 3: Merge Sort, Quick Sort reading: Weiss Ch. 7 slides created by Marty Stepp
CSE 326: Data Structures Lecture #16 Sorting Things Out Bart Niswonger Summer Quarter 2001.
1 CSE 326: Data Structures A Sort of Detour Henry Kautz Winter Quarter 2002.
Sorting 1. Insertion Sort
CSE 326: Data Structures Lecture 23 Spring Quarter 2001 Sorting, Part 1 David Kaplan
Chapter 4, Part II Sorting Algorithms. 2 Heap Details A heap is a tree structure where for each subtree the value stored at the root is larger than all.
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Sorting.
CPSC 411 Design and Analysis of Algorithms
Quick-Sort 9/12/2018 3:26 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Quick-Sort 9/13/2018 1:15 AM Quick-Sort     2
Chapter 7 Sorting Spring 14
Quicksort "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
Divide and Conquer.
CSE 143 Lecture 23: quick sort.
Quick Sort (11.2) CSE 2011 Winter November 2018.
Lecture 3 / 4 Algorithm Analysis
Medians and Order Statistics
8/04/2009 Many thanks to David Sun for some of the included slides!
ITEC 2620M Introduction to Data Structures
Yan Shi CS/SE 2630 Lecture Notes
Sub-Quadratic Sorting Algorithms
slides adapted from Marty Stepp
CSE373: Data Structure & Algorithms Lecture 21: Comparison Sorting
CSE 326: Data Structures Sorting
CSE 332: Data Abstractions Sorting I
Topic 5: Heap data structure heap sort Priority queue
CS 1114: Sorting and selection (part two)
CSE 326: Data Structures: Sorting
CSE 373 Data Structures and Algorithms
Quick-Sort 4/25/2019 8:10 AM Quick-Sort     2
Divide & Conquer Sorting
CSE 326: Data Structures: Sorting
The Selection Problem.
Quicksort and Randomized Algs
CSE 332: Sorting II Spring 2016.
Presentation transcript:

1 CSE 326: Data Structures Sorting by Comparison Zasha Weinberg in lieu of Steve Wolfman Winter Quarter 2000

2 Sorting by Comparison algorithms Simple: Selection Sort –(some say Insertion Sort, or Bubble Sort) Quick: QuickSort Good worst case: MergeSort, HeapSort Can we do better?

3 Selection Sort idea Find the smallest element, put it first Find the next smallest element, put it second Find the next smallest, put it next etc.

4 Selection Sort procedure SelectionSort (Array[1..N] For i=1 to N-1 Find the smallest entry in Array[i..N] Let j be the index of that entry Swap(Array[i],Array[j]) End For While other people are coding QuickSort/MergeSort Twiddle thumbs End While

5 HeapSort: sorting with a priority queue ADT (heap) Shove everything into a queue, take them out smallest to largest.

6 QuickSort Pick a “pivot”. Divide into less-than & greater-than pivot. Sort each side recursively. Picture from PhotoDisc.com

7 QuickSort Partition Pick pivot: Partition with cursors <> <> 2 goes to less-than

8 QuickSort Partition (cont’d) <> 6, 8 swap less/greater-than ,5 less-than 9 greater-than Partition done. Recursively sort each side.

9 QuickSort Worst case

10 Dealing with Slow QuickSorts Randomly permute input –Bad cases more common than simple probability would suggest. So, make it truly random. Pick pivot cleverly –“Median-of-3” rule takes Median(first, middle, last element elements). Use MergeSort or HeapSort.

11 QuickSort Average Case Model expected # of operations Manipulate the recurrences Get O(n log n) (Weiss, p )

12 MergeSort Photo from Merging Cars by key [Aggressiveness of driver]. Most aggressive goes first. MergeSort (Table [1..n]) Split Table in half Recursively sort each half Merge two halves together Merge (T1[1..n],T2[1..n]) i1=1, i2=1 While i1<n, i2<n If T1[i1] < T2[i2] Next is T1[i1] i1++ Else Next is T2[i2] i2++ End If End While

13 MergeSort Running Time

14 Could we do better?* For any possible correct Sorting by Comparison alg –What is lowest best case time? –What is lowest worst case time? * (no. sorry.)

15 Best case time

16 Worst case time How many comparisons does it take before we can be sure of the order? This is the minimum # of comparisons that any algorithm could do.

17 Decision tree to sort list A,B,C

18 Max depth of the decision tree How many leaves does the tree have? What’s the shallowest tree with a given number of leaves?

19 Stirling’s approximation (textbook has alternate way of getting the bound, p. 288)

20 Eine kleine Nachmath