FASTER SORT using RECURSION : MERGE SORT 2015-T2 Lecture 15 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Back to Sorting – More efficient sorting algorithms.
Garfield AP Computer Science
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?
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.
Theory of Algorithms: Divide and Conquer
Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search.
1 Exceptions Exceptions oops, mistake correction oops, mistake correction Issues with Matrix and Vector Issues with Matrix and Vector Quicksort Quicksort.
QuickSort The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Sorting Algorithms and Average Case Time Complexity
CMPS1371 Introduction to Computing for Engineers SORTING.
CS 171: Introduction to Computer Science II Quicksort.
Sorting Part 2 CS221 – 3/4/09. Announcements Midterm: 3/11 – 15% of your total grade – We will review in class on 3/9 – You can bring one sheet of paper.
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
Sorting21 Recursive sorting algorithms Oh no, not again!
CS 206 Introduction to Computer Science II 12 / 05 / 2008 Instructor: Michael Eckmann.
1 Algorithm Efficiency and Sorting (Walls & Mirrors - Remainder of Chapter 9)
CS 280 Data Structures Professor John Peterson. Project Questions?
Sorting - Merge Sort Cmput Lecture 12 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
CS2420: Lecture 10 Vladimir Kulyukin Computer Science Department Utah State University.
Cmpt-225 Sorting. Fundamental problem in computing science  putting a collection of items in order Often used as part of another algorithm  e.g. sort.
CS2420: Lecture 9 Vladimir Kulyukin Computer Science Department Utah State University.
Sorting Algorithms Bubble Sort Merge Sort Quick Sort Randomized Quick Sort.
Sorting CS-212 Dick Steflik. Exchange Sorting Method : make n-1 passes across the data, on each pass compare adjacent items, swapping as necessary (n-1.
Mergesort and Quicksort Chapter 8 Kruse and Ryba.
Analysis of Algorithms Dilemma: you have two (or more) methods to solve problem, how to choose the BEST? One approach: implement each algorithm in C, test.
1 Divide and Conquer Binary Search Mergesort Recurrence Relations CSE Lecture 4 – Algorithms II.
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
ALGORITHM ANALYSIS AND DESIGN INTRODUCTION TO ALGORITHMS CS 413 Divide and Conquer Algortihms: Binary search, merge sort.
A review session 2013-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
FASTER SORTING using RECURSION : QUICKSORT COMP 103.
Sorting Sanghyun Park Fall 2002 CSE, POSTECH. Sorts To Consider Selection sort Bubble sort Insertion sort Merge sort Quick sort Why do we care about sorting?
Computer Science Searching & Sorting.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArraySet and Binary Search.
FASTER SORTING using RECURSION : QUICKSORT 2014-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.
An introduction to costs (continued), and Binary Search 2013-T2 Lecture 11 School of Engineering and Computer Science, Victoria University of Wellington.
SORTING 2014-T2 Lecture 13 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
2011-T2 Lecture 21 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, and Peter Andreae, VUW.
Merge Sort: Taught By Example CO1406: Algorithms and Data Structures Module Lecturer: Dr. Nearchos Paspallis Week 10 Lab - Practice with Merge sort and.
Searching and Sorting Recursion, Merge-sort, Divide & Conquer, Bucket sort, Radix sort Lecture 5.
More about costs: cost of “ensureCapacity”, cost of ArraySet, Binary Search 2014-T2 Lecture 12 School of Engineering and Computer Science, Victoria University.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
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.
2015-T2 Lecture 17 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,
QUICKSORT 2015-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
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.
Intro To Algorithms Searching and Sorting. Searching A common task for a computer is to find a block of data A common task for a computer is to find a.
CS1022 Computer Programming & Principles Lecture 2.2 Algorithms.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Fast Sorting COMP
CSE 143 Lecture 16 Sorting reading: 13.1, slides created by Marty Stepp
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Review Quick Sort Quick Sort Algorithm Time Complexity Examples
Sorting Ordering data. Design and Analysis of Sorting Assumptions –sorting will be internal (in memory) –sorting will be done on an array of elements.
CS 367 Introduction to Data Structures Lecture 11.
FASTER SORT using RECURSION : MERGE SORT COMP 103.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Sorting and Runtime Complexity CS255. Sorting Different ways to sort: –Bubble –Exchange –Insertion –Merge –Quick –more…
FASTER SORT using RECURSION : MERGE SORT
Lecture 4 Divide-and-Conquer
Chapter 4 Divide-and-Conquer
COMP 103 SORTING Lindsay Groves 2016-T2 Lecture 26
COMP 103 Sorting with Binary Trees: Tree sort, Heap sort Alex Potanin
CSE 332: Data Abstractions Sorting I
CSE 373 Data Structures and Algorithms
Algorithms: Design and Analysis
Presentation transcript:

FASTER SORT using RECURSION : MERGE SORT 2015-T2 Lecture 15 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean

RECAP-TODAY RECAP  Recursion TODAY  Fast Sorts: Divide and Conquer  Merge Sort (and analysis) Announcements  Assignment #5: came out last Friday, but is due Friday 11 th Sept (ie. you get a really loooong time to complete it)  Mid-term test: is this Friday 2

3 the test this Friday  20% of final grade, and takes 45 mins RoomsFROM  TO  Maclaurin 101A  J  Maclaurin 102K  M  Hunter 323N  Z Please be on time, and bring your ID card.  “Not-quite-Exam conditions”: keep a vacant seat between each put your ID card where it can be seen – at the end of the row. (no need to leave bags at the front)  What’s in? everything up to and including Recursion old tests are a guide for what to expect

4 Stresses and strains 1 st year is stressful to most people. How’s your “mental health” and how well connected are you socially? Do any of the following resonate?  freaked out, wound up  flat, exhausted  lonely, vulnerable  just all over the place  it’s normal in “doses”, but if this is you much of the time, that’s not ideal.  Talking to a trained person (student health) is great, but there’s a long wait.  If you’re struggling academically (e.g. you might not make 7 assignments, or you do badly on tomorrow’s test), then : knock on Craig’s door: CO 253 (Cotton building) 4

5 Well-being: some homework  connect  talk and listen, be there, feel connected  be active  do what you can do, enjoy what you do, move your mood  take notice  re-remembering the simple things that give you joy : good but the opposite (ruminating) : sucketh 5 And these are No-brainers for keeping a resilient brain: sleep intake under control (caffeine, booze, sugar) exercise for fun: waitbutwhy.com especially “Taming the mammoth”, and “The procrastination matrix”

6  Insertion sort, Selection Sort, Bubble Sort:  All slow (except Insertion sort on almost sorted lists)  O(n 2 )  Problem:  Insertion and Bubble only compare adjacent items only move items one step at a time  Selection compares every pair of items ignores results of previous comparisons.  Solution:  Must compare and swap items at a distance  Must not perform redundant comparisons Slow Sorts

7 Divide and Conquer Sorts To Sort:  Split  Sort each part (recursive)  Combine Where does the work happen ?  MergeSort:  split is trivial  combine does all the work  QuickSort:  split does all the work  combine is trivial Array Sorted Array Split Combine SubArray SortedSubArray Sort Split Combine SubArray Sort SortedSubArray Split Combine SubArray Sort SortedSubArray

8 MergeSort : the concept

10 MergeSort : the order when recursing...

MergeSort: the ‘merge’ method “from” array “to” array

12 Merge /** Merge from[low..mid-1] with from[mid..high-1] into to[low..high-1.*/ private static void merge(E[] from, E[] to, int low, int mid, int high, Comparator comp){ int index = low; // where we will put the item into "to“ int indxLeft = low; // index into the lower half of the "from" range int indxRight = mid; // index into the upper half of the "from" range while (indxLeft<mid && indxRight < high){ if (comp.compare(from[indxLeft], from[indxRight]) <=0) to[index++] = from[indxLeft++]; else to[index++] = from[indxRight++]; } // copy over the remainder. Note only one loop will do anything. while (indxLeft<mid) to[index++] = from[indxLeft++]; while (indxRight<high) to[index++] = from[indxRight++]; }

13 MergeSort – a wrapper method that starts it  It looks like we an extra temporary array for each “level” (how many levels are there?)  Somewhat REMARKABLY, it turns out we only need one (extra), and at each layer we can treat the other array as “storage”  We start with a wrapper makes this second array, and fills it with a copy of the original data. public static void mergeSort(E[] data, int size, Comparator comp){ E[] other = (E[])new Object[size]; for (int i=0; i<size; i++) temp[i]=data[i]; mergeSort(data, temp, 0, size, comp); }

14 MergeSort – the recursive method private static void mergeSort(E[] data, E[] other, int low, int high, Comparator comp){ // sort items from low..high-1, using the other array if (high > low+1){ int mid = (low+high)/2; // mid = low of upper 1/2, = high of lower half. mergeSort(other, data, low, mid, comp); mergeSort(other, data, mid, high, comp); merge(other, data, low, mid, high, comp); }  there are multiple calls to the recursive method in here. this will make a "tree" structure  we swap other and data at each recursive call (= each “level”)

15 MergeSort: recursion ms(d, t, 0,16) ms(t, d, 0,8)ms(t, d, 8,16)mg(t, d, 0,8,16) ms(d,t,0,4) ms(d,t,4,8) mg(d,t,0,4,8) ms(t,d,0,2) ms(t,d,2,4) mg(t,d,0,2,4) ms(d,t,0,1) ms(d,t,1,2) mg(d,t,0,1,2) ms(d,t,2,3) ms(d,t,3,4) mg(d,t,2,3,4) ms(t,d,4,6) ms(t,d,6,8) mg(t,d,4,6,8) ms(d,t,4,5) ms(d,t,5,6) mg(d,t,4,5,6) ms(d,t,6,7) ms(d,t,7,8) mg(d,t,6,7,8) ms(d,t,8,12) ms(d,t,12,16) mg(d,t,0,8,16) (...)

16 MergeSort – will the two arrays 'mess up'? data to sort other to sort 'working' 'working' merge 'working' merge to sort

17 data [p a1 r f e q2 w q1 t z2 x c v b z1 a2 ] msort(0..16) [p a1 r f e q2 w q1 t z2 x c v b z1 a2 ] msort(0..8) [p a1 r f e q2 w q1 ] msort(0..4 ) [p a1 r f ] msort(0..2 ) [p a1 ] msort(0..1 ) [p ] msort(1..2) [ a1 ] merge(0.1.2) [a1 p ] msort(2..4) [ r f ] msort(2..3) [ r ] msort(3..4) [ f ] merge(2.3.4) [ f r ] merge(0.2.4) [a1 f p r ] msort(4..8) [ e q2 w q1 ] msort(4..6) [ e q2 ] : : merge(4.5.6) [ e q2 ] msort(6..8) [ w q1 ] : : merge(6.7.8) [ q1 w ] merge(4.6.8) [ e q2 q1 w ] merge(0.4.8) [a1 e f p q2 q1 r w ] msort(8..16) [ t z2 x c v b z1 a2 ] : : merge( ) [ a2 b c t v x z1 z2 ] merge(0.8.16) [a1 a2 b c e f p q2 q1 r t v w x z1 z2 ]

18 Sorting Algorithm costs:  Insertion sort, Selection Sort:  All slow (except Insertion sort on almost-sorted lists)  O(n 2 )  Merge Sort ?  There’s no inner loop!  How do you analyse recursive algorithms?

19 MergeSort private static void mergeSort (E[ ] data, E[ ] other, int low, int high, Comparator comp) { if (high > low+1) { int mid = (low+high)/2; mergeSort(other, data, low, mid, comp); mergeSort(other, data, mid, high, comp); merge(other, data, low, mid, high, comp); } }  Cost of mergeSort:  Three steps: first recursive call second recursive call merge: has to copy over (high-low) items

20 MergeSort Cost  Level 1:2 * n/2= n  Level 2:4 * n/4= n  Level 3:8 * n/8= n  Level 4:16 * n/16= n so in general, at any level k, there are n comparisons  How many levels ? the number of times you can halve n is log n  Total cost? = O( )  n = 1,000 n = 1,000,000

21 Analysing with Recurrence Relations private static void mergeSort(E[] data, E[] other, int low, int high, Comparator comp){ if (high > low+1){ int mid = (low+high)/2; mergeSort(other, data, low, mid, comp); mergeSort(other, data, mid, high, comp); merge(other, data, low, mid, high, comp); } }  Cost of mergeSort = C(n)  C(n) = C(n/2) + C(n/2) + n = 2 C(n/2) + n  Recurrence Relation:  (we will) Solve by repeated substitution & find pattern  (we could) Solve by general method

22 Solving Recurrence Relations C(n) = 2 C(n/2) + n = 2 [ 2 C(n/4) + n/2] + n = 4 C(n/4) + 2 n = 4 [ 2 (C(n/8) + n/4] + 2 n = 8 C(n/8) + 3 n = 16 C(n/16) + 4 n : = 2 k C( n/2 k ) + k * n when n = 2 k, k = log(n) = n C (1) + log(n) * n and since C(1) = 0, C(n) = log(n) * n