CSC2100B Quick Sort and Merge Sort Xin 1. Quick Sort Efficient sorting algorithm Example of Divide and Conquer algorithm Two phases ◦ Partition phase.

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.
© 2004 Goodrich, Tamassia Merge Sort1 7 2  9 4   2  2 79  4   72  29  94  4.
CSC 213 – Large Scale Programming or. Today’s Goals  Begin by discussing basic approach of quick sort  Divide-and-conquer used, but how does this help?
Data Structures Lecture 9 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
1 Merge Sort Review of Sorting Merge Sort. 2 Sorting Algorithms Selection Sort uses a priority queue P implemented with an unsorted sequence: –Phase 1:
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
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.
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
Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
Sorting Algorithms and Average Case Time Complexity
© 2004 Goodrich, Tamassia QuickSort1 Quick-Sort     29  9.
Data Structures and Algorithms PLSD210 Sorting. Card players all know how to sort … First card is already sorted With all the rest, ¶Scan back from the.
Quick-Sort     29  9.
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Zhigang Zhu Department.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
© 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.
MergeSort Source: Gibbs & Tamassia. 2 MergeSort MergeSort is a divide and conquer method of sorting.
Sorting II/ Slide 1 Lecture 24 May 15, 2011 l merge-sorting l quick-sorting.
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
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.
Lecture10: Sorting II Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
CSS106 Introduction to Elementary Algorithms M.Sc Askar Satabaldiyev Lecture 05: MergeSort & QuickSort.
Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2.Solve smaller instances.
Sorting: Advanced Techniques Smt Genap
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
© 2004 Goodrich, Tamassia Merge Sort1 7 2  9 4   2  2 79  4   72  29  94  4.
1 Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
Sorting Algorithms Merge Sort Quick Sort Hairong Zhao New Jersey Institute of Technology.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
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. Yet another sorting algorithm! Usually faster than other algorithms on average, although worst-case is O(n 2 ) Divide-and-conquer: –Divide:
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.
1 Overview Divide and Conquer Merge Sort Quick Sort.
CSE 250 – Data Structures. Today’s Goals  First review the easy, simple sorting algorithms  Compare while inserting value into place in the vector 
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.
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
Data Structures and Algorithms
MergeSort Source: Gibbs & Tamassia.
Objectives Introduce different known sorting algorithms
Chapter 4: Divide and Conquer
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
Algorithms Dr. Youn-Hee Han April-May 2013
Copyright © Aiman Hanna All rights reserved
Copyright © Aiman Hanna All rights reserved
Quick-Sort 4/8/ :20 AM Quick-Sort     2 9  9
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
Divide & Conquer Sorting
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:

CSC2100B Quick Sort and Merge Sort Xin 1

Quick Sort Efficient sorting algorithm Example of Divide and Conquer algorithm Two phases ◦ Partition phase  Divides the work into half ◦ Sort phase  Conquers the halves! 2

Quicksort Partition ◦ Choose a pivot ◦ Find the position for the pivot so that  all elements to the left are less / equal  all elements to the right are equal / greater 3 < =pivot=> pivotpivot

Quicksort Conquer ◦ Apply the same algorithm to each half 4 < pivot> pivot pivot<= p’ p’=> p’< =p”p”=> p”

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

Quicksort Implementation 22 quicksort( void *a, int low, int high ) { int pivot; /* Termination condition! */ if ( high > low ) { pivot = partition( a, low, high ); quicksort( a, low, pivot-1 ); quicksort( a, pivot+1, high ); } Divide Conquer

Quicksort - Partition 23 int partition( int *a, int low, int high ) { int left, right; int pivot_item; pivot_item = a[low]; pivot = left = low; right = high; while ( left <= right ) { /* Move left while item < pivot */ while( a[left] <= pivot_item ) left++; /* Move right while item > pivot */ while( a[right] > pivot_item ) right--; if ( left < right ) { SWAP(a,left,right); left++; right--; } /* right is final position for the pivot */ a[low] = a[right]; a[right] = pivot_item; return right; }

Quicksort - Analysis Partition ◦ Check every item once O(n) Conquer ◦ Divide data in half O(log 2 n) Total ◦ Product O(n log n) Same as Heapsort ◦ quicksort is generally faster  Fewer comparisons 24

Merge Sort - Definition Definition: A sort algorithm that splits the items to be sorted into two groups, recursively sorts each group, and merge them into a final sorted sequence. Run time is O (n log n). 25

Merge Sort – Divide-and-Conquer Divide-and-conquer is a general algorithm design paradigm: ◦ Divide: divide the input data S in two disjoint subsets S1 and S2 ◦ Conquer: solve the sub-problems associated with S1 and S2 recursively ◦ Combine: combine the solutions for S1 and S2 into a solution for S The base case for the recursion are sub- problems of size 0 or 1 26

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  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 27

Merge Sort - example | Partition

Merge Sort - example | | 9 4 Recursive call, partition

Merge Sort - example | | | 2 Recursive call, partition

Merge Sort - example | | | 2 7777 Recursive call, base case

Merge Sort - example | | | 2 77772222 Recursive call, base case

Merge Sort - example | | 9 4 7|2  2 7 77772222 Merge

Merge Sort - example | | 9 4 7|2  2 7 9|4  4 9 77772222 Recursive call, base case, …, base case, merge 99994444

Merge Sort - example | |94  |2  2 7 9|4  4 9 7777222299994444 Merge

Merge Sort - example  | |94   |2  2 7 9|4  4 9 7777222299994444 3 8  3 8 3333888866661111 Recursive call, …, merge, merge

Merge Sort - example  |3861  |94   |2  2 7 9|4  4 9 7777222299994444 3 8  3 8 3333888866661111 Merge

Merge Sort - analysis The height h of the merge-sort tree is log n + 1 = O(log n) ◦ at each recursive call we divide in half the sequence The overall amount or work done at the nodes of depth i is O(n) ◦ we partition and merge 2 i sequences of size n/2 i ◦ we make 2 i+1 recursive calls Thus, the total running time of merge-sort is O(n log n) 38

Merge Sort – sample code void mergesort(int low, int high) { if (low<high) { int middle=(low+high)/2; mergesort(low, middle); mergesort(middle+1, high); merge(low, middle, high); } 39

Merge Sort – sample code (cont.) void merge(int low, int middle, int high) { int i, j, k; // copy both halves of a to auxiliary array b for (i=low; i<=high; i++) b[i]=a[i]; i=low; j=middle+1; k=low; // copy back next-greatest element at each time while (i<=middle && j<=high) if (b[i]<=b[j]) a[k++]=b[i++]; else a[k++]=b[j++]; // copy back remaining elements of first half (if any) while (i<=middle) a[k++]=b[i++]; } 40

End 41