Lecture 19: Sorting Algorithms

Slides:



Advertisements
Similar presentations
Garfield AP Computer Science
Advertisements

Sorting Comparison-based algorithm review –You should know most of the algorithms –We will concentrate on their analyses –Special emphasis: Heapsort Lower.
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
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
Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search.
CSE 373: Data Structures and Algorithms
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]
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.
Sorting Chapter 10.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Sorting Chapter 10. Chapter 10: Sorting2 Chapter Objectives To learn how to use the standard sorting methods in the Java API To learn how to implement.
Overview of Sorting by Ron Peterson. Variety of Algorithms There are a lot of different approaches to sorting, not just different programs that use similar.
HKOI 2006 Intermediate Training Searching and Sorting 1/4/2006.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Sorting Chapter 10. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following sorting.
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.
CSE 373: Data Structures and Algorithms Lecture 6: Sorting 1.
Lecture10: Sorting II Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
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.
Comparison of Optimization Algorithms By Jonathan Lutu.
1 Sorting (Bubble Sort, Insertion Sort, Selection Sort)
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.
Sorting Fundamental Data Structures and Algorithms Aleks Nanevski February 17, 2004.
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.
Intro. to Data Structures Chapter 7 Sorting Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Chapter 7 Sorting Sort is.
Searching and Sorting Searching algorithms with simple arrays
Advanced Sorting.
School of Computing Clemson University Fall, 2012
Sorting and Searching Algorithms
Sorting.
CSCI 104 Sorting Algorithms
May 17th – Comparison Sorts
CSE 373: Data Structure & Algorithms Comparison Sorting
Divide and Conquer Strategy
CSE373: Data Structures & Algorithms
Quick-Sort 9/13/2018 1:15 AM Quick-Sort     2
Week 12 - Wednesday CS221.
Fast Sorting "The bubble sort seems to have nothing to recommend it, except a catchy name and the fact that it leads to some interesting theoretical problems."
CSE 143 Lecture 23: quick sort.
Description Given a linear collection of items x1, x2, x3,….,xn
Sorting Chapter 13 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Quicksort and Mergesort
Data Structures and Algorithms
Data Structures and Algorithms
CSC215 Lecture Algorithms.
Data Structures and Algorithms
8/04/2009 Many thanks to David Sun for some of the included slides!
Data Structures and Algorithms
Sorting Chapter 8 CS 225.
Sub-Quadratic Sorting Algorithms
slides adapted from Marty Stepp
CSE373: Data Structure & Algorithms Lecture 21: Comparison Sorting
Topic 17 Faster Sorting "The bubble sort seems to have nothing to recommend it, except a catchy name and the fact that it leads to some interesting theoretical.
Chapter 4.
CSE 326: Data Structures Sorting
EE 312 Software Design and Implementation I
CSE 332: Data Abstractions Sorting I
CSE 373: Data Structures and Algorithms
Sorting.
CSE 373 Data Structures and Algorithms
Sorting Chapter 10.
CSE 373 Sorting 1: Bogo Sort, Stooge Sort, Bubble Sort
Sorting: part 1 Barak Obama on sorting, bubble sort, insertion sort,
The Selection Problem.
Lecture 15: Sorting Algorithms
Design and Analysis of Algorithms
CSE 332: Sorting II Spring 2016.
Presentation transcript:

Lecture 19: Sorting Algorithms CSE 373: Data Structures and Algorithms CSE 373 19 sp - Kasey Champion

Administrivia HW 5 Part 1 Due Thursday HW 5 Part 2 Out Wednesday CSE 373 19 SP - Kasey Champion

Sorting CSE 373 19 wi - Kasey Champion

Types of Sorts Niche Sorts aka “linear sorts” Comparison Sorts Compare two elements at a time General sort, works for most types of elements Element must form a “consistent, total ordering” For every element a, b and c in the list the following must be true: If a <= b and b <= a then a = b If a <= b and b <= c then a <= c Either a <= b is true or <= a What does this mean? compareTo() works for your elements Comparison sorts run at fastest O(nlog(n)) time Niche Sorts aka “linear sorts” Leverages specific properties about the items in the list to achieve faster runtimes niche sorts typically run O(n) time In this class we’ll focus on comparison sorts CSE 373 SP 18 - Kasey Champion

Sort Approaches [(8, “fox”), (9, “dog”), (4, “wolf”), (8, “cow”)] In Place sort A sorting algorithm is in-place if it requires only O(1) extra space to sort the array Typically modifies the input collection Useful to minimize memory usage Stable sort A sorting algorithm is stable if any equal items remain in the same relative order before and after the sort Why do we care? Sometimes we want to sort based on some, but not all attributes of an item Items that “compareTo()” the same might not be exact duplicates Enables us to sort on one attribute first then another etc… [(8, “fox”), (9, “dog”), (4, “wolf”), (8, “cow”)] [(4, “wolf”), (8, “fox”), (8, “cow”), (9, “dog”)] Stable [(4, “wolf”), (8, “cow”), (8, “fox”), (9, “dog”)] Unstable CSE 373 SP 18 - Kasey Champion

SO MANY SORTS Quicksort, Merge sort, in-place merge sort, heap sort, insertion sort, intro sort, selection sort, timsort, cubesort, shell sort, bubble sort, binary tree sort, cycle sort, library sort, patience sorting, smoothsort, strand sort, tournament sort, cocktail sort, comb sort, gnome sort, block sort, stackoverflow sort, odd-even sort, pigeonhole sort, bucket sort, counting sort, radix sort, spreadsort, burstsort, flashsort, postman sort, bead sort, simple pancake sort, spaghetti sort, sorting network, bitonic sort, bogosort, stooge sort, insertion sort, slow sort, rainbow sort… CSE 373 SP 18 - Kasey Champion

Insertion Sort https://www.youtube.com/watch?v=ROalU379l3U 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 10 Sorted Items Unsorted Items Current Item 1 2 3 4 5 6 7 8 9 10 Sorted Items Unsorted Items Current Item 1 2 3 4 5 6 7 8 9 10 Sorted Items Unsorted Items Current Item

Insertion Sort 1 2 3 4 5 6 7 8 9 10 Sorted Items Unsorted Items 1 2 3 4 5 6 7 8 9 10 Sorted Items Unsorted Items Current Item public void insertionSort(collection) { for (entire list) if(currentItem is smaller than largestSorted) int newIndex = findSpot(currentItem); shift(newIndex, currentItem); } public int findSpot(currentItem) { for (sorted list) if (spot found) return public void shift(newIndex, currentItem) { for (i = currentItem > newIndex) item[i+1] = item[i] item[newIndex] = currentItem Worst case runtime? Best case runtime? Average runtime? Stable? In-place? O(n2) O(n) O(n2) Yes Yes CSE 373 SP 18 - Kasey Champion

Selection Sort https://www.youtube.com/watch?v=Ns4TPTC8whw 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 18 10 14 11 15 Sorted Items Unsorted Items Current Item 1 2 3 4 5 6 7 8 9 10 14 18 11 15 Sorted Items Unsorted Items Current Item 1 2 3 4 5 6 7 8 9 10 18 14 11 15 Sorted Items Unsorted Items Current Item

Selection Sort 1 2 3 4 5 6 7 8 9 18 10 14 11 15 Sorted Items 1 2 3 4 5 6 7 8 9 18 10 14 11 15 Sorted Items Current Item Unsorted Items public void selectionSort(collection) { for (entire list) int newIndex = findNextMin(currentItem); swap(newIndex, currentItem); } public int findNextMin(currentItem) { min = currentItem for (unsorted list) if (item < min) return min public int swap(newIndex, currentItem) { temp = currentItem currentItem = newIndex newIndex = currentItem Worst case runtime? Best case runtime? Average runtime? Stable? In-place? O(n2) O(n2) O(n2) No Yes CSE 373 SP 18 - Kasey Champion

Heap Sort 1. run Floyd’s buildHeap on your data https://www.youtube.com/watch?v=Xw2D9aJRBY4 1. run Floyd’s buildHeap on your data 2. call removeMin n times Worst case runtime? Best case runtime? Average runtime? Stable? In-place? O(nlogn) public void heapSort(collection) { E[] heap = buildHeap(collection) E[] output = new E[n] for (n) output[i] = removeMin(heap) } O(nlogn) O(nlogn) No No CSE 373 SP 18 - Kasey Champion

In Place Heap Sort 1 2 3 4 5 6 7 8 9 14 15 18 16 17 20 22 Heap Sorted Items Current Item 1 2 3 4 5 6 7 8 9 22 14 15 18 16 17 20 percolateDown(22) Heap Sorted Items Current Item 1 2 3 4 5 6 7 8 9 16 14 15 18 22 17 20 Heap Sorted Items Current Item CSE 373 SP 18 - Kasey Champion

In Place Heap Sort 1 2 3 4 5 6 7 8 9 15 17 16 18 20 22 14 Heap Sorted Items Current Item public void inPlaceHeapSort(collection) { E[] heap = buildHeap(collection) for (n) output[n – i - 1] = removeMin(heap) } Worst case runtime? Best case runtime? Average runtime? Stable? In-place? O(nlogn) O(nlogn) O(nlogn) Complication: final array is reversed! Run reverse afterwards (O(n)) Use a max heap Reverse compare function to emulate max heap No Yes CSE 373 SP 18 - Kasey Champion

Divide and Conquer Technique 1. Divide your work into smaller pieces recursively Pieces should be smaller versions of the larger problem 2. Conquer the individual pieces Base case! 3. Combine the results back up recursively divideAndConquer(input) { if (small enough to solve) conquer, solve, return results else divide input into a smaller pieces recurse on smaller piece combine results and return } CSE 373 SP 18 - Kasey Champion

Merge Sort https://www.youtube.com/watch?v=XaqR3G_NVoo Divide 1 2 3 4 1 2 3 4 5 6 7 8 9 91 22 57 10 1 2 3 4 8 91 22 57 5 6 7 8 9 1 10 4 8 Conquer 8 Combine 1 2 3 4 8 22 57 91 5 6 7 8 9 1 4 10 1 2 3 4 5 6 7 8 9 10 22 57 91 CSE 373 SP 18 - Kasey Champion

Merge Sort 1 2 3 4 8 57 91 22 1 8 2 1 2 57 91 22 mergeSort(input) { if (input.length == 1) return else smallerHalf = mergeSort(new [0, ..., mid]) largerHalf = mergeSort(new [mid + 1, ...]) return merge(smallerHalf, largerHalf) } 8 2 57 1 91 22 91 22 1 22 91 1 if n<= 1 2T(n/2) + n otherwise Worst case runtime? Best case runtime? Average runtime? Stable? In-place? T(n) = = O(nlog(n)) Same as above 1 2 8 1 2 22 57 91 Same as above Yes 1 2 3 4 8 22 57 91 No CSE 373 SP 18 - Kasey Champion

Quick Sort v1 Divide Conquer Combine https://www.youtube.com/watch?v=ywWBy6J5gz8 Divide Conquer Combine 1 2 3 4 5 6 7 8 9 91 22 57 10 1 2 3 4 6 7 8 1 2 3 91 22 57 10 6 6 2 3 4 22 57 91 8 5 6 7 8 9 1 4 10 1 2 3 4 5 6 7 8 9 10 22 57 91 CSE 373 SP 18 - Kasey Champion

1 2 3 4 5 6 20 50 70 10 60 40 30 Quick Sort v1 10 1 2 3 4 50 70 60 40 30 quickSort(input) { if (input.length == 1) return else pivot = getPivot(input) smallerHalf = quickSort(getSmaller(pivot, input)) largerHalf = quickSort(getBigger(pivot, input)) return smallerHalf + pivot + largerHalf } 1 40 30 1 70 60 30 60 1 30 40 1 60 70 1 if n<= 1 n + T(n - 1) otherwise T(n) = = O(n^2) Worst case runtime? Best case runtime? Average runtime? Stable? In-place? 1 2 3 4 30 40 50 60 70 1 if n<= 1 n + 2T(n/2) otherwise T(n) = = O(nlog(n)) https://secweb.cs.odu.edu/~zeil/cs361/web/website/Lectures/quick/pages/ar01s05.html Same as best case 1 2 3 4 5 6 10 20 30 40 50 60 70 No No CSE 373 SP 18 - Kasey Champion

Can we do better? Pick a better pivot Pick a random number Pick the median of the first, middle and last element Sort elements by swapping around pivot in place CSE 373 SP 18 - Kasey Champion

Quick Sort v2 (in-place) 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 Low X < 6 High X >= 6 1 2 3 4 5 6 7 8 9 Low X < 6 High X >= 6 CSE 373 SP 18 - Kasey Champion

Quick Sort v2 (in-place) 1 2 3 4 5 6 7 8 9 quickSort(input) { if (input.length == 1) return else pivot = getPivot(input) smallerHalf = quickSort(getSmaller(pivot, input)) largerHalf = quickSort(getBigger(pivot, input)) return smallerHalf + pivot + largerHalf } 1 if n<= 1 n + T(n - 1) otherwise T(n) = Worst case runtime? Best case runtime? Average runtime? Stable? In-place? = O(n^2) 1 if n<= 1 n + 2T(n/2) otherwise T(n) = = O(nlogn) = O(nlogn) https://secweb.cs.odu.edu/~zeil/cs361/web/website/Lectures/quick/pages/ar01s05.html No Yes CSE 373 SP 18 - Kasey Champion

CSE 373 SP 18 - Kasey Champion