Lecture10: Sorting II Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)

Slides:



Advertisements
Similar presentations
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
© 2004 Goodrich, Tamassia Merge Sort1 7 2  9 4   2  2 79  4   72  29  94  4.
Goodrich, Tamassia Sorting, Sets and Selection1 Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
© 2004 Goodrich, Tamassia Merge Sort1 7 2  9 4   2  2 79  4   72  29  94  4.
Data Structures Lecture 9 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Lecture16: Heap Sort Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
Merge Sort 4/15/2017 4:30 PM Merge Sort 7 2   7  2  2 7
Quicksort Quicksort     29  9.
Merge Sort1 Part-G1 Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
Merge Sort1 7 2  9 4   2  2 79  4   72  29  94  4.
Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.
CSC2100B Quick Sort and Merge Sort Xin 1. Quick Sort Efficient sorting algorithm Example of Divide and Conquer algorithm Two phases ◦ Partition phase.
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
© 2004 Goodrich, Tamassia QuickSort1 Quick-Sort     29  9.
Quick-Sort     29  9.
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
Comparison Sorts Chapter 9.4, 12.1, Sorting  We have seen the advantage of sorted data representations for a number of applications  Sparse vectors.
© 2004 Goodrich, Tamassia Merge Sort1 Quick-Sort     29  9.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4 Modified by: Daniel Gomez-Prado, University of Massachusetts Amherst.
© 2004 Goodrich, Tamassia Merge Sort1 Chapter 8 Sorting Topics Basics Merge sort ( 合併排序 ) Quick sort Bucket sort ( 分籃排序 ) Radix sort ( 基數排序 ) Summary.
MergeSort Source: Gibbs & Tamassia. 2 MergeSort MergeSort is a divide and conquer method of sorting.
Sorting.
CSC 213 Lecture 12: Quick Sort & Radix/Bucket Sort.
CSCE 3110 Data Structures & Algorithm Analysis Sorting (I) Reading: Chap.7, Weiss.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Sorting Fun1 Chapter 4: Sorting     29  9.
1 CSE 373 Sorting 3: Merge Sort, Quick Sort reading: Weiss Ch. 7 slides created by Marty Stepp
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Lecture No. 04,05 Sorting.  A process that organizes a collection of data into either ascending or descending order.  Can be used as a first step for.
Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
1 Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
Towers of Hanoi Move n (4) disks from pole A to pole B such that a larger disk is never put on a smaller disk A BC ABC.
Quicksort Quicksort is a well-known sorting algorithm that, in the worst case, it makes Θ(n 2 ) comparisons. Typically, quicksort is significantly faster.
QuickSort by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 8b. Sorting(2): (n log n) Algorithms.
Computer Sciences Department1. Sorting algorithm 4 Computer Sciences Department3.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Merge Sort 1/12/2018 5:48 AM Merge Sort 7 2   7  2  2 7
Advanced Sorting 7 2  9 4   2   4   7
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Bohyung Han CSE, POSTECH
Merge Sort 7 2  9 4   2   4   7 2  2 9  9 4  4.
Merge Sort 1/12/2018 9:39 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Merge Sort 1/12/2018 9:44 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Quick Sort and Merge Sort
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
Quick-Sort To understand quick-sort, let’s look at a high-level description of the algorithm 1) Divide : If the sequence S has 2 or more elements, select.
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.
Radish-Sort 11/11/ :01 AM Quick-Sort     2 9  9
Objectives Introduce different known sorting algorithms
Merge Sort 12/4/ :32 AM Merge Sort 7 2   7  2   4  4 9
1 Lecture 13 CS2013.
Sorting, Sets and Selection
Merge Sort 2/23/ :15 PM Merge Sort 7 2   7  2   4  4 9
Merge Sort 2/24/2019 6:07 PM Merge Sort 7 2   7  2  2 7
Quick-Sort 2/25/2019 2:22 AM Quick-Sort     2
Copyright © Aiman Hanna All rights reserved
Copyright © Aiman Hanna All rights reserved
Quick-Sort 4/8/ :20 AM Quick-Sort     2 9  9
CSE 373 Data Structures and Algorithms
Merge Sort 4/10/ :25 AM Merge Sort 7 2   7  2   4  4 9
Quick-Sort 4/25/2019 8:10 AM Quick-Sort     2
Quick-Sort 5/25/2019 6:16 PM Selection Selection.
Merge Sort 5/30/2019 7:52 AM Merge Sort 7 2   7  2  2 7
CS203 Lecture 15.
Divide-and-Conquer 7 2  9 4   2   4   7
Presentation transcript:

Lecture10: Sorting II Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Sorting Important operation when organizing data  Ordering of elements  Finding duplicate elements  Ranking elements (i.e., nth largest)  etc. An example There are many different strategies and we need to care about  Correctness  Efficiency (in terms of time complexity) Input: Output:

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Sorting Algorithms Bubble sort Selection sort Insertion sort Merge sort Quick sort 3

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Comparisons of Sorting Algorithms 4 Size of array (n) Sorting time in milliseconds How about the performance of merge sort and quicksort? Bubble sort Selection sort Insertion sort

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Divide‐and‐Conquer A strategy to solve problems  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? 5

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Merge Sort A sorting algorithm based on the divide‐and‐conquer paradigm 6 Divide Conquer

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Merge Sort 7 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

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Merge Sort 8 Algorithm merge(A, B) Input sequences A and B with n/2 elements each Output sorted sequence of A U 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

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Merge Sort Tree 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  9 4   2  2 79  4   77  72  22  29  99  94  44  4

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Execution Example

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Merge Sort Algorithm 11 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];

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Merge Sort Algorithm 12 // 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; }

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Analysis of Merge Sort 13 depth#seqssize ………

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Quicksort A (randomized) sorting algorithm based on the divide-and-conquer paradigm 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 smaller 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. 14

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Quicksort 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  Sort L and G Conquer  Join L, E and G 15 x x L G E x

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Quicksort Pseudocode of quicksort 16 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) return

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Quicksort 17 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 G.addLast(y) return L, E, G

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Quicksort Tree An execution of quick-sort 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     29  9

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Execution Example

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 In‐Place Quicksort Drawback of (naïve) quicksort  It requires additional memory to maintain three lists. In-place quicksort  Quick-sort 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 20 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)

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 In‐Place Quick‐Sort (pivot = 6)

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 In‐Place Quicksort 22 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); }

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 In‐Place Quicksort 23 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, pivot); // swap left and pivot return left; // return index of pivot }

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Analysis of Quicksort 24

CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Summary of Sorting Algorithms 25 AlgorithmTimeNotes 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) Quicksort  in-place, randomized  fastest (good for large inputs) Merge sort  sequential data access  fast (good for huge inputs)

26