Sorting. Lecture10: Sorting II. Sorting Algorithms. Performance of Sorting Algorithms

Similar documents
Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015

Sorting. Data structures and Algorithms

Merge Sort

Lecture 19 Sorting Goodrich, Tamassia

Quick-Sort. Quick-Sort 1

Quick-Sort fi fi fi 7 9. Quick-Sort Goodrich, Tamassia

Sorting Goodrich, Tamassia Sorting 1

Quick-Sort. Quick-sort is a randomized sorting algorithm based on the divide-and-conquer paradigm:

Recursive Sorts. Recursive Sorts. Divide-and-Conquer. Divide-and-Conquer. Divide-and-conquer paradigm:

Sorting. Divide-and-Conquer 1

Data Structures and Algorithms " Sorting!

Merge Sort fi fi fi 4 9. Merge Sort Goodrich, Tamassia

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Merge Sort & Quick Sort

Binary Trees. Quicksort. SorKng process with quicksort. University of Massachuse5s Amherst ECE 242 Data Structures and Algorithms Lecture 18

1. The Sets ADT. 1. The Sets ADT. 1. The Sets ADT 11/22/2011. Class Assignment Set in STL

Sorting. Outline. Sorting with a priority queue Selection-sort Insertion-sort Heap Sort Quick Sort

ECE 242 Fall 14 Exam II Profs. Gao and Zink

QuickSort

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

SORTING AND SELECTION

Merge Sort Goodrich, Tamassia. Merge Sort 1

Outline and Reading. Quick-Sort. Partition. Quick-Sort. Quick-Sort Tree. Execution Example

SORTING, SETS, AND SELECTION

Quicksort. Repeat the process recursively for the left- and rightsub-blocks.

Comparison Sorts. Chapter 9.4, 12.1, 12.2

Merge Sort Goodrich, Tamassia Merge Sort 1

EECS 2011M: Fundamentals of Data Structures

Unit-2 Divide and conquer 2016

ECE 242 Data Structures and Algorithms. Advanced Sorting II. Lecture 17. Prof.

CPE702 Algorithm Analysis and Design Week 7 Algorithm Design Patterns

Sorting and Selection

Algorithm for siftdown(int currentposition) while true (infinite loop) do if the currentposition has NO children then return

Sorting. Task Description. Selection Sort. Should we worry about speed?

Quick Sort. CSE Data Structures May 15, 2002

SORTING LOWER BOUND & BUCKET-SORT AND RADIX-SORT

COMP2012H Spring 2014 Dekai Wu. Sorting. (more on sorting algorithms: mergesort, quicksort, heapsort)

Sorting Algorithms. + Analysis of the Sorting Algorithms

Algorithms and Data Structures (INF1) Lecture 7/15 Hua Lu

Chapter 4: Sorting. Spring 2014 Sorting Fun 1

Sorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort

Better sorting algorithms (Weiss chapter )

DATA STRUCTURES AND ALGORITHMS

BM267 - Introduction to Data Structures

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015

CS 310 Advanced Data Structures and Algorithms

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

Sorting. Bubble Sort. Pseudo Code for Bubble Sorting: Sorting is ordering a list of elements.

// walk through array stepping by step amount, moving elements down for (i = unsorted; i >= step && item < a[i-step]; i-=step) { a[i] = a[i-step];

Algorithmic Analysis and Sorting, Part Two

ITEC2620 Introduction to Data Structures

Plan of the lecture. Quick-Sort. Partition of lists (or using extra workspace) Quick-Sort ( 10.2) Quick-Sort Tree. Partitioning arrays

Mergesort again. 1. Split the list into two equal parts

SORTING. Comparison of Quadratic Sorts

Quicksort (Weiss chapter 8.6)

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CSE 2123 Sorting. Jeremy Morris

IS 709/809: Computational Methods in IS Research. Algorithm Analysis (Sorting)

Lecture Notes 14 More sorting CSS Data Structures and Object-Oriented Programming Professor Clark F. Olson

MERGESORT & QUICKSORT cs2420 Introduction to Algorithms and Data Structures Spring 2015

Priority Queues. Lecture15: Heaps. Priority Queue ADT. Sequence based Priority Queue

Mergesort again. 1. Split the list into two equal parts

Divide and Conquer Sorting Algorithms and Noncomparison-based

Lecture 8: Mergesort / Quicksort Steven Skiena

L14 Quicksort and Performance Optimization

We can use a max-heap to sort data.

Cpt S 122 Data Structures. Sorting

CSE 143 Lecture 22. Sorting. reading: 13.1, slides adapted from Marty Stepp and Hélène Martin

Searching and Sorting

CSE 143 Lecture 16 (B)

Quicksort. The divide-and-conquer strategy is used in quicksort. Below the recursion step is described:

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

Key question: how do we pick a good pivot (and what makes a good pivot in the first place)?

Algorithmic Analysis and Sorting, Part Two. CS106B Winter

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

having any value between and. For array element, the plot will have a dot at the intersection of and, subject to scaling constraints.

Lecture 2: Divide&Conquer Paradigm, Merge sort and Quicksort

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Outline. Quadratic-Time Sorting. Linearithmic-Time Sorting. Conclusion. Bubble/Shaker Sort Insertion Sort Odd-Even Sort

Table ADT and Sorting. Algorithm topics continuing (or reviewing?) CS 24 curriculum

CS Sorting Terms & Definitions. Comparing Sorting Algorithms. Bubble Sort. Bubble Sort: Graphical Trace

CSCI 136 Data Structures & Advanced Programming. Lecture 14 Spring 2018 Profs Bill & Jon

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

Divide and Conquer. Algorithm D-and-C(n: input size)

Sorting. CSE 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation. CSE143 Au

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

Lecture Notes on Quicksort

Divide and Conquer Algorithms: Advanced Sorting. Prichard Ch. 10.2: Advanced Sorting Algorithms

Sorting. Bringing Order to the World

Unit Outline. Comparing Sorting Algorithms Heapsort Mergesort Quicksort More Comparisons Complexity of Sorting 2 / 33

CSE 373: Data Structures and Algorithms

Data structures. More sorting. Dr. Alex Gerdes DIT961 - VT 2018

Merge- and Quick Sort

Assignment 4: Question 1b omitted. Assignment 5: Question 1b clarification

Overview of Sorting Algorithms

COMP Data Structures

Programming II (CS300)

Sorting is a problem for which we can prove a non-trivial lower bound.

Lecture 5: Sorting Part A

Lecture Notes on Quicksort

Transcription:

Sorting (2013F) Lecture10: Sorting II Bohyung Han CSE, POSTECH bhhan@postech.ac.kr Important operation when organizing data Ordering of elements Finding duplicate elements Ranking elements (i.e., n th largest) etc. An example input 3 6 1 9 4 2 output 1 2 3 4 6 9 There are many different strategies and we need to care about Correctness Efficiency (in terms of time complexity) 2 Sorting Algorithms Performance of Sorting Algorithms Bubble sort Selection sort Insertion sort How about the performance of merge sort and quicksort? Bubble sort Selection sort Insertion sort Merge sort Quick sort 3 4

A strategy to solve problems Divide and Conquer Break problem into smaller problems Solve the smaller problems Combine results Strategy can be applied recursively to smaller problem Continue dividing problem until solution is trivial. Divide and conquer can be implemented by recursion effectively. Recursive sorting Apply divide and conquer to sorting problem How can we solve sorting recursively? A sorting algorithm based on the divide and conquer paradigm Divide Conquer 5 6 Divide Partition S with elements into two sequences S 1 and S 2 of about /2 elements each. Recursion Recursively sort S 1 and S 2 Performed implicitly by calling itself Conquer Merge S 1 and S 2 into a unique sorted sequence Algorithm mergesort(s, C) Input sequence S with n elements, comparator C Output sequence S sorted according to C if S.size() > 1 (S 1, S 2 ) partition(s, n/2) mergesort(s 1, C) mergesort(s 2, C) S merge(s 1, S 2 ) return Merge function Conquer step of merge sort Actual sorting is performed inside this function. Time complexity Can be implemented with doubly linked list efficiently Algorithm merge(a, B) Input sequences A and B with n/2 elements each Output sorted sequence of A B S empty sequence while A.isEmpty() B.isEmpty() if A.first().element() < B.first().element() S.addLast(A.remove(A.first())) else S.addLast(B.remove(B.first())) while A.isEmpty() S.addLast(A.remove(A.first())) while B.isEmpty() S.addLast(B.remove(B.first())) return S 7 8

Tree An Execution Example 7 2 9 4 2 4 7 9 1 2 3 4 6 7 8 9 7 2 2 7 9 4 4 9 7 7 2 2 9 9 4 4 7 2 9 4 2 4 7 9 3 8 6 1 1 3 6 8 An execution of merge sort is depicted by a binary tree. Each node represents a recursive call of merge sort and stores unsorted sequence before the execution and its partition sorted sequence at the end of the execution The root is the initial call The leaves are calls on subsequences of size 0 or 1 7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6 1 2 3 4 6 7 8 9 9 10 Algorithm Algorithm public void merge_sort(int[] values) // check for termination condition if (values == null values.length <= 1) return; // divide array into two arrays of half size int middle = values.length/2; int[] left = new int[middle]; int[] right = new int[values.length middle]; for (int i=0; i<middle; i++) left[i] = values[i]; for (int i=0; i<values.length middle; i++) right[i] = values[middle+i]; // recursively call sorting function on each smaller array merge_sort(left); merge_sort(right); // combine sorted arrays int l=0; int r=0; for (int i=0; i<values.length; i++) if (r>=right.length (l<left.length && left[l]<right[r])) values[i] = left[l++]; else values[i] = right[r++]; return; 11 12

Analysis of The height of the merge sort tree is At each recursive call we divide in half the sequence. The amount of work done at the nodes of depth is. We partition and merge 2 sequences of size /2. We make 2 recursive calls. The total running time of merge sort is. depth #seqs size 0 1 1 2 /2 2 /2 A divide and conquer algorithm. The steps are: Pick an element, called a pivot, from the list. Partitioning: Reorder the list so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it. Equal values can go either way. After this partitioning, the pivot is in its final position. Recursively apply the above steps to the sub list of elements with smal ler values and separately the sub list of elements with greater values. The base case of the recursion are lists of size zero or one, which never need to be sorted. 13 14 Divide Pick a random element x (called pivot) and partition S into L elements less than x E elements equal x G elements greater than x Recurrence x x Pseudo code of quicksort Algorithm quicksort(s, p) Input sequence S, position p of pivot Output sorted sequence S L, E, G partition(s, p) quicksort(l,p) quicksort(g,p) S join(l, E, G) Sort L and G Conquer Join L, E and G L E G return x 15 16

Partition an input sequence Partition Remove each element y from S. Insert y into L, E or G, depending on the result of the comparison with the pivot x. Time complexity Insertions and removals happen at the beginning or at the end of a sequence, and hence takes 1 time. The partition step of quicksort tak es time. Algorithm partition(s, p) Input sequence S, position p of pivot Output subsequences L, E, G of the elements of S less than, equal to, or greater than the pivot, resp. L, E, G empty sequences x S.remove(p) while S.isEmpty() y S.remove(S.first()) if y < x L.addLast(y) else if y = x E.addLast(y) else y > x G.addLast(y) return L, E, G Tree 7 4 9 6 2 2 4 6 7 9 4 2 2 4 7 9 7 9 2 2 9 9 An execution of quicksort is depicted by a binary tree Each node represents a recursive call of quick sort and stores Unsorted sequence before the execution and its pivot Sorted sequence at the end of the execution The root is the initial call The leaves are calls on subsequences of size 0 or 1 17 18 An Execution Example In Place 1 2 3 4 6 7 8 9 2 4 3 1 1 2 3 4 7 9 8 7 8 9 1 4 3 3 4 7 9 can be implemented to run in place. In the partition step, we use replace operations to rearrange the elements. The recursive calls consider elements with rank less than h elements with rank greater than k Algorithm inplacequicksort(s, l, r) Input sequence S, ranks l and r Output sequence S with the elements of rank between l and r rearranged in increasing order if l r return i a random integer between l and r x S.elemAtRank(i) (h, k) inplacepartition(x) inplacequicksort(s, l, h 1) inplacequicksort(s, k + 1,r) 3 1 2 3 4 6 7 8 9 19 20

In Place Quick Sort In Place Perform the partition using two indices to split S into L, E, G 3 2 5 1 0 7 3 5 9 2 7 9 8 9 7 6 9 Repeat until and cross: Scan to the right until finding an element greater than or equal to pivot or. Scan to the left until finding an element less than pivot or. Swap elements at indices and or swap pivot with when. (pivot = 6) public void quicksort(int[] values, int left, int right) if (right <= left) return; int pivotindex = partition(values, left, right); quicksort(values, left, pivotindex 1); quicksort(values, pivotindex+1, right); 3 2 5 1 0 7 3 5 9 2 7 9 8 9 7 6 9 21 22 In Place Analysis of public int partition(int[] values, int leftbound, int rightbound) int pivot = values[rightbound]; int left = leftbound 1; int right = rightbound; // rightmost is pivot while (true) while (values[++left]<pivot) ; while (right>leftbound && values[ right]>pivot) ; if (left >= right) break; else swap(values, left, right); // swap misplaced items swap(values, left, rightbound); // swap left and pivot Depends on the choice of pivot element Good choice: median value of array Bad choice: max or min value of array In practice, it is difficult to choose a good one without search. Time complexity Worst case: Average case: log Randomized algorithm: log return left; // return index of pivot 23 24

Summary of Sorting Algorithms Algorithm Time complexity Notes Bubble sort in place slow (good for small inputs) Selection sort in place slow (good for small inputs) Insertion sort in place slow (good for small inputs) log expected log in place, randomized fastest (good for large inputs) in place fast (good for large inputs) 25 26