CSE 326: Data Structures Sorting

Slides:



Advertisements
Similar presentations
Garfield AP Computer Science
Advertisements

CSE332: Data Abstractions Lecture 14: Beyond Comparison Sorting Dan Grossman Spring 2010.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
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.
CSE332: Data Abstractions Lecture 13: Comparison Sorting Tyler Robison Summer
CSE332: Data Abstractions Lecture 13: Comparison Sorting
CSE332: Data Abstractions Lecture 12: Introduction to Sorting Tyler Robison Summer
Sorting Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Lecture 25 Selection sort, reviewed Insertion sort, reviewed Merge sort Running time of merge sort, 2 ways to look at it Quicksort Course evaluations.
Sorting Algorithms Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Sorting. Introduction Assumptions –Sorting an array of integers –Entire sort can be done in main memory Straightforward algorithms are O(N 2 ) More complex.
Divide and Conquer Sorting
CSE 326: Data Structures Sorting Ben Lerner Summer 2007.
CSC 2300 Data Structures & Algorithms March 20, 2007 Chapter 7. Sorting.
1 7.5 Heapsort Average number of comparison used to heapsort a random permutation of N items is 2N logN - O (N log log N).
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
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.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting Aaron Bauer Winter 2014.
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 326: Data Structures A Sort of Detour Henry Kautz Winter Quarter 2002.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
Sorting 1 Devon M. Simmonds University of North Carolina, Wilmington TIME: Tuesday/Thursday 11:11:50am in 1012 & Thursday 3:30-5:10pm in Office hours:
CSE332: Data Abstractions Lecture 12: Introduction to Sorting Dan Grossman Spring 2010.
Divide and Conquer Sorting Algorithms COMP s1 Sedgewick Chapters 7 and 8.
CSE 326: Data Structures Lecture 23 Spring Quarter 2001 Sorting, Part 1 David Kaplan
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
CSE332: Data Abstractions Lecture 13: Comparison Sorting Dan Grossman Spring 2012.
Chapter 9: Sorting1 Sorting & Searching Ch. # 9. Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of.
CS 367 Introduction to Data Structures Lecture 11.
CSE373: Data Structure & Algorithms Lecture 19: Comparison Sorting Lauren Milne Summer 2015.
1 CSE 326: Data Structures Sorting by Comparison Zasha Weinberg in lieu of Steve Wolfman Winter Quarter 2000.
Prof. U V THETE Dept. of Computer Science YMA
CSE373: Data Structures & Algorithms
Sorting.
Fundamental Data Structures and Algorithms
May 17th – Comparison Sorts
CSE 373: Data Structure & Algorithms Comparison Sorting
CSE373: Data Structures & Algorithms
CSE373: Data Structures & Algorithms
CSE332: Data Abstractions Lecture 12: Introduction to Sorting
Teach A level Computing: Algorithms and Data Structures
Instructor: Lilian de Greef Quarter: Summer 2017
Instructor: Lilian de Greef Quarter: Summer 2017
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Chapter 4: Divide and Conquer
Quick Sort (11.2) CSE 2011 Winter November 2018.
CSE373: Data Structure & Algorithms Lecture 21: Comparison Sorting
CSE373: Data Structure & Algorithms Lecture 22: More Sorting
8/04/2009 Many thanks to David Sun for some of the included slides!
QuickSort Previous slides based on ones by Ethan Apter & Marty Stepp
CSE373: Data Structure & Algorithms Lecture 21: Comparison Sorting
Sorting CSE 373 Data Structures.
CSE 332: Data Abstractions Sorting I
CSE 373: Data Structures and Algorithms
CS 1114: Sorting and selection (part two)
CSE 373 Data Structures and Algorithms
CSE 332: Sorting II Spring 2016.
Presentation transcript:

CSE 326: Data Structures Sorting Ben Lerner Summer 2007

Sorting: The Big Picture Given n comparable elements in an array, sort them in an increasing (or decreasing) order. Simple algorithms: O(n2) Fancier algorithms: O(n log n) Comparison lower bound: (n log n) Specialized algorithms: O(n) Handling huge data sets Insertion sort Selection sort Bubble sort Shell sort … Heap sort Merge sort Quick sort … Bucket sort Radix sort External sorting

O(n2) : move k/2 in kth step Insertion Sort: Idea At the kth step, put the kth input element in the correct place among the first k elements Result: After the kth step, the first k elements are sorted. Runtime: worst case : best case : average case : O(n2) : reverse input O(n) : sorted input O(n2) : move k/2 in kth step

Example

Example

Try it out: Insertion sort

Selection Sort: idea Find the smallest element, put it 1st Find the next smallest element, put it 2nd Find the next smallest, put it 3rd And so on …

O(n2) : must select in linear time Selection Sort: Code void SelectionSort (Array a[0..n-1]) { for (i=0, i<n; ++i) { j = Find index of smallest entry in a[i..n-1] Swap(a[i],a[j]) } Runtime: worst case : best case : average case : O(n2) : must select in linear time Ask About Heap sort ?

Try it out: Selection sort Insert 31, 16, 54, 4, 2, 17, 6

HeapSort: Using Priority Queue ADT (heap) Buildheap = N Insert = log N (avg O(1)) Deletemin = log N Shove all elements into a priority queue, take them out smallest to largest. Runtime: Worst, avg: O(n log n)

Merge Sort MergeSort (Array [1..n]) Merge (a1[1..n],a2[1..n]) 1. Split Array in half 2. Recursively sort each half 3. Merge two halves together Merge (a1[1..n],a2[1..n]) i1=1, i2=1 While (i1<n, i2<n) { if (a1[i1] < a2[i2]) { Next is a1[i1] i1++ } else { Next is a2[i2] i2++ } Now throw in the dregs… “The 2-pointer method”

Mergesort example 8 2 9 4 5 3 1 6 Divide 8 2 9 4 5 3 1 6 1-element 8 2 9 4 5 3 1 6 _ Merge 2 8 4 9 3 5 1 6 Merge 2 4 8 9 1 3 5 6 Final: 1 2 3 4 5 6 8 9

Try it out: Merge sort Insert 31, 16, 54, 4, 2, 17, 6

Merge Sort: Complexity Want: n/2k = 1 n = 2k log n = k Base case: T(1) = c T(n) = 2 T(n/2) + n … T(n) = O(n log n) (best, worst) Merge Sort: Complexity Base case: T(1) = c T(n) = 2 T(n/2) + n = 2 (2T(n/4) +n/2) + n =4T(n/4)+n+n =4T(n/4)+2n =4 (2T(n/8) +n/4) + 2n =8T(n/8)+n+2n =8T(n/8)+3n =2k T(n/2k) + kn = nT(1) +n logn = n+ n log n

The steps of QuickSort S S1 S2 S1 S2 S Details: how to pick a good pivot? S select pivot value 81 31 57 43 13 75 92 65 26 S1 S2 partition S 31 75 43 13 65 81 92 26 57 QuickSort(S1) and QuickSort(S2) S1 S2 13 26 31 43 57 65 75 81 92 S 13 26 31 43 57 65 75 81 92 Presto! S is sorted [Weiss]

Show how to select a pivot Show how to do the partition QuickSort Example 8 1 4 9 3 5 2 7 6 i j Choose the pivot as the median of three. Place the pivot and the largest at the right and the smallest at the left

QuickSort Example Move i to the right to be larger than pivot. 1 4 9 7 3 5 2 6 8 i j Move i to the right to be larger than pivot. Move j to the left to be smaller than pivot. Swap

QuickSort Example S1 < pivot pivot S2 > pivot 1 4 2 5 3 7 9 6 8 1 4 2 5 3 7 9 6 8 i j S1 < pivot pivot S2 > pivot

Recursive Quicksort Don’t use quicksort for small arrays. Quicksort(A[]: integer array, left,right : integer): { pivotindex : integer; if left + CUTOFF  right then pivot := median3(A,left,right); pivotindex := Partition(A,left,right-1,pivot); Quicksort(A, left, pivotindex – 1); Quicksort(A, pivotindex + 1, right); else Insertionsort(A,left,right); } Don’t use quicksort for small arrays. CUTOFF = 10 is reasonable.

Try it out: Recursive quicksort Insert 31, 16, 54, 4, 2, 17, 6

QuickSort: Best case complexity T(n) = 2T(n/2) + n … T(n) = O(n log n) Same as Mergesort What is best case? Always chooses a pivot that splits array in half at each step

QuickSort: Worst case complexity T(n) = n + T(n-1) … T(n) = O(n2) T(1) = c T(n) = n + T(n-1) T(n) = n + (n-1) + T(n-2) T(n) = n + (n-1) + (n-2)+ T(n-3) T(n) = 1 + 2+ 3+…+N … T(n) = O(n2) Always chooses WORST pivot – so that one array is empty at each step

QuickSort: Average case complexity T(n) = n + T(n-1) … T(n) = O(n2) Turns out to be O(n log n) See Section 7.7.5 for an idea of the proof. Don’t need to know proof details for this course. In-place (but uses extra storage due to recursion – no iterative w/o a stack) O(n log n ) average but O(n2) worst case Ends up being very fast in practice, for large arrays – a good choice

Features of Sorting Algorithms In-place Sorted items occupy the same space as the original items. (No copying required, only O(1) extra space if any.) Stable Items in input with the same value end up in the same order as when they began.

Sort Properties Are the following: stable? in-place? Your Turn Sort Properties Are the following: stable? in-place? Insertion Sort? No Yes Can Be No Yes Selection Sort? No Yes Can Be No Yes MergeSort? No Yes Can Be No Yes QuickSort? No Yes Can Be No Yes