Sorting: Merge Sort. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

Similar documents
Basic Algorithms Overview

Heap and Merge Sorts

Artificial Intelligence Prof. Deepak Khemani Department of Computer Science and Engineering Indian Institute of Technology, Madras

Recursive Mergesort. CSE 589 Applied Algorithms Spring Merging Pattern of Recursive Mergesort. Mergesort Call Tree. Reorder the Merging Steps

COS 226 Algorithms and Data Structures Fall Midterm

I thought I should expand this population approach somewhat: P t = P0e is the equation which describes population growth.

Deep Neural Networks [GBC] Chap. 6, 7, 8. CS 486/686 University of Waterloo Lecture 18: June 28, 2017

Kripke s skeptical paradox

McDougal Littell High School Math Program. correlated to. Oregon Mathematics Grade-Level Standards

SUMMARY COMPARISON of 6 th grade Math texts approved for 2007 local Texas adoption

Torah Code Cluster Probabilities

Model: 2+2 Scenario 1: Cluster SMK, SKD, and SM; cluster CCBT and SJW

HP-35s Calculator Program Compute Horizontal Curve Values given only 2 Parameters

Lazy Functional Programming for a survey

Surveying Prof. Bharat Lohani Department of Civil Engineering Indian Institute of Technology, Kanpur. Module - 7 Lecture - 3 Levelling and Contouring

ON SOPHIE GERMAIN PRIMES

NPTEL NPTEL ONLINE COURSES REINFORCEMENT LEARNING. UCB1 Explanation (UCB1)

Artificial Intelligence: Valid Arguments and Proof Systems. Prof. Deepak Khemani. Department of Computer Science and Engineering

Focusing the It s Time Urban Mission Initiative

Artificial Intelligence Prof. Deepak Khemani Department of Computer Science and Engineering Indian Institute of Technology, Madras

Smith Waterman Algorithm - Performance Analysis

Ch. 5 Lessons 1-6 Preview #1

Module 02 Lecture - 10 Inferential Statistics Single Sample Tests

Quorums. Christian Plattner, Gustavo Alonso Exercises for Verteilte Systeme WS05/06 Swiss Federal Institute of Technology (ETH), Zürich

Lesson 07 Notes. Machine Learning. Quiz: Computational Learning Theory

Final Exam (PRACTICE-2) #2

Logicola Truth Evaluation Exercises

A Scientific Model Explains Spirituality and Nonduality

Creation Answers. In this issue... Who does this newsletter?

Lead Student Lesson Plan L14: Alma 17-29

Math Matters: Why Do I Need To Know This? 1 Logic Understanding the English language

Different types of braces for adults by Nearest Orthodontist

The Decline of the Traditional Church Choir: The Impact on the Church and Society. Dr Arthur Saunders

Lead Student Lesson Plan L04: 1 Nephi 15-22

Midterm Review Part 1 #4

Midterm Review. Intermediate Algebra / MAT135 S2014 test (Mr. Porter)

2.1 Review. 2.2 Inference and justifications

Predictive Coding. CSE 390 Introduction to Data Compression Fall Entropy. Bad and Good Prediction. Which Context to Use? PPM

CMSC 341 Snow Day Chat March 14, 2017

Discussion Notes for Bayesian Reasoning

(Refer Slide Time 03:00)

Agnostic KWIK learning and efficient approximate reinforcement learning

Strategic Planning Update for the Diocese of Evansville

Computational Learning Theory: Agnostic Learning

Lead Student Lesson Plan L11: 4 Nephi Mormon 9

Jethro Helped Moses. Bible Passage: Exodus 18. Story Point: Moses needed help to lead God s people. Key Passage:

ECE 5984: Introduction to Machine Learning

Math 11 Final Exam Review Part 3 #1

Hello everyone. This is Trang. Let s give it a couple of more minutes for people to dial in, so we ll get started in a couple of minutes. Thank you.

Experiencing God Notes for Home Family Study

NPTEL NPTEL ONINE CERTIFICATION COURSE. Introduction to Machine Learning. Lecture-59 Ensemble Methods- Bagging,Committee Machines and Stacking

Probability Foundations for Electrical Engineers Prof. Krishna Jagannathan Department of Electrical Engineering Indian Institute of Technology, Madras

Lead Student Lesson Plan L02: 1 Nephi 1-5

Inference in Cyc. Copyright 2002 Cycorp

Draft 11/20/2017 APPENDIX C: TRANSPORTATION PLAN FORECASTS

Lead Student Lesson Plan L06: 2 Nephi 9-16

Content Area Variations of Academic Language

Segment 2 Exam Review #1

The Healthy Small Church

ABSTRACT. Religion and Economic Growth: An Analysis at the City Level. Ran Duan, M.S.Eco. Mentor: Lourenço S. Paz, Ph.D.

The Hypercube 3.1. Section 3.1 The Hypercube

Dr. John Hamre President and Chief Executive Officer Center for Strategic and International Studies Washington, D.C.

THE BELIEF IN GOD AND IMMORTALITY A Psychological, Anthropological and Statistical Study

MISSOURI S FRAMEWORK FOR CURRICULAR DEVELOPMENT IN MATH TOPIC I: PROBLEM SOLVING

Lead Student Lesson Plan L12: Mosiah 27 Alma 7

Write three supporting reasons that would convince the reader to agree with your position (in order of importance).

Project 1: Grameen Foundation USA, Philippine Microfinance Initiative

4.3: Adjusting Sprint Content

The branch settlement system is basically an accounting or a bookkeeping system that the bank uses to transfer money between one branch office of the

Prioritizing Issues in Islamic Economics and Finance

Faith Communities Today

HP-35s Calculator Program Triangle Solution (Program Based on Sine & Cosine Rules)

defines problem 2. Search for Exhaustive Limited, sequential Demand generation

Overview of the ATLAS Fast Tracker (FTK) (daughter of the very successful CDF SVT) July 24, 2008 M. Shochet 1

Model: 2+2 Scenario 2: Cluster SMK and CCBT; cluster SKD, SM, and SJW

DOES17 LONDON FROM CODE COMMIT TO PRODUCTION WITHIN A DAY TRANSCRIPT

ECE 5424: Introduction to Machine Learning

Grade 6 Math Connects Suggested Course Outline for Schooling at Home

THE TREES, ROCKS, MOUNTAINS AND HILLS SING

AUTOMATION. Presents DALI

Gesture recognition with Kinect. Joakim Larsson

Lead Student Lesson Plan L11: Mosiah 4-26

An Efficient Indexing Approach to Find Quranic Symbols in Large Texts

Research (universe energy from human energy) Written by Sarab Abdulwahed Alturky

PROCEEDINGS OF THE TECHNICAL ADVISORY COMMITTEE Wednesday, August 9 th, 2017 East Grand Forks City Hall Training Conference Room

Apologies: Julie Hedlund. ICANN Staff: Mary Wong Michelle DeSmyter

TRANSCRIPT. Contact Repository Implementation Working Group Meeting Durban 14 July 2013

Foundations of World Civilization: Notes 2 A Framework for World History Copyright Bruce Owen 2009 Why study history? Arnold Toynbee 1948 This

Why the Hardest Logic Puzzle Ever Cannot Be Solved in Less than Three Questions

Transcription ICANN Los Angeles Translation and Transliteration Contact Information PDP WG Update to the Council meeting Saturday 11 October 2014

Our Story with MCM. Shanghai Jiao Tong University. March, 2014

Lead Student Lesson Plan L03: 1 Nephi 6-14

COMMUNITY FORUM CONVERSATIONS. Facilitation Guide

Forms of Justification when Reading Scientific Arguments

Prentice Hall The American Nation: Beginnings Through 1877 '2002 Correlated to: Chandler USD Social Studies Textbook Evaluation Instrument (Grade 8)

The Trolley Problem. 1. The Trolley Problem: Consider the following pair of cases:

United Methodist History: DENOM-600X, Fall Garrett-Evangelical Theological Seminary. Jonathan LeMaster-Smith, PhD - Instructor

Can the Angel fly into infinity, or does the Devil eat its squares?

First Things First or Choosing the Most Important Things by Bill Scheidler

The Complete Guide to Godly Play

Transcription:

Sorting: Merge Sort College of Computing & Information Technology King Abdulaziz University CPCS-204 Data Structures I

Sorting: Merge Sort Problem with Bubble/Insertion/Selection Sorts: All of these sorts make a large number of comparisons and swaps between elements As mentioned last class (while covering n 2 sorts): Any algorithm that swaps adjacent elements can only run so fast So one might ask is there a more clever way to sort numbers A way that does not require looking at all these pairs Indeed, there are several ways to do this And one of them is Merge Sort Sorting: Merge Sort page 2

Sorting: Merge Sort Merge Sort Conceptually, Merge Sort works as follows: If the list is of length 0 or 1, then it is already sorted! Otherwise: 1. Divide the unsorted list into two sub-lists of about half the size So if your list has n elements, you will divide that list into two sub-lists, each having approximately n/2 elements: 2. Recursively sort each sub-list by calling recursively calling Merge Sort on the two smaller lists 3. Merge the two sub-lists back into one sorted list This Merge is a function that we study on its own In a bit Sorting: Merge Sort page 3

Sorting: Merge Sort Merge Sort Basically, given a list: You will split this list into two lists of about half the size Then you recursively call Merge Sort on each list What does that do? Each of these new lists will, individually, be split into two lists of about half the size. So now we have four lists, each about ¼ the size of the original list This keeps happening the lists keep getting split into smaller and smaller lists Until you get to a list of size 1 or size 0 which is sorted! Then we Merge them into a larger, sorted list Sorting: Merge Sort page 4

Sorting: Merge Sort Merge Sort Incorporates two main ideas to improve its runtime: 1) A small list will take fewer steps to sort than a large list 2) Fewer steps are required to construct a sorted list from two sorted lists than two unsorted lists For example: You only have to traverse each list once if they re already sorted Sorting: Merge Sort page 5

Sorting: Merge Sort Merge function The key to Merge Sort: the Merge function Given two sorted lists, Merge them into one sorted list Problem: You are given two arrays, each of which is already sorted Your job is to efficiently combine the two arrays into one larger array The larger array should contain all the values of the two smaller arrays Finally, the larger array should be in sorted order Sorting: Merge Sort page 6

Sorting: Merge Sort Merge function The key to Merge Sort: the Merge function Given two sorted lists, Merge them into one sorted list If you have two lists: X (x 1 <x 2 < <x m ) and Y (y 1 <y 2 < <y n ) Merge these into one list: Z (z 1 <z 2 < <z m+n ) Example: List 1 = {3, 8, 9} and List 2 = {1, 5, 7} Merge(List 1, List 2) = {1, 3, 5, 7, 8, 9} Sorting: Merge Sort page 7

Sorting: Merge Sort Merge function Solution: Keep track of the smallest value in each array that hasn t been placed, in order, in the larger array yet Compare these two smallest values from each array One of these MUST be the smallest of all the values in both arrays that are left Place the smallest of the two values in the next location in the larger array Adjust the smallest value for the appropriate array Continue this process until all values have been placed in the large array Sorting: Merge Sort page 8

Sorting: Merge Sort Example of Merge function: X: Y: 3 10 23 54 1 5 25 75 Result: Sorting: Merge Sort page 9

Sorting: Merge Sort Example of Merge function: 3 10 23 54 5 25 75 X: Y: Result: 1 Sorting: Merge Sort page 10

Sorting: Merge Sort Example of Merge function: 10 23 54 5 25 75 X: Y: Result: 1 3 Sorting: Merge Sort page 11

Sorting: Merge Sort Example of Merge function: 10 23 54 25 75 X: Y: Result: 1 3 5 Sorting: Merge Sort page 12

Sorting: Merge Sort Example of Merge function: X: Y: 23 54 25 75 Result: 1 3 5 10 Sorting: Merge Sort page 13

Sorting: Merge Sort Example of Merge function: X: Y: 54 25 75 Result: 1 3 5 10 23 Sorting: Merge Sort page 14

Sorting: Merge Sort Example of Merge function: X: Y: 54 75 Result: 1 3 5 10 23 25 Sorting: Merge Sort page 15

Sorting: Merge Sort Example of Merge function: X: Y: 75 Result: 1 3 5 10 23 25 54 Sorting: Merge Sort page 16

Sorting: Merge Sort Example of Merge function: X: Y: Result: 1 3 5 10 23 25 54 75 Sorting: Merge Sort page 17

Sorting: Merge Sort Merge function The big question: How can we use this Merge function to sort an entire, unsorted array? This function only sorts a specific scenario: You have to have two, already sorted, arrays Merge can then sort (merge) them into one larger array So can we use this Merge function to somehow sort a large, unsorted array??? This brings us back to Merge Sort Sorting: Merge Sort page 18

Sorting: Merge Sort Merge Sort Again, here is the main idea for Merge Sort: 1) Sort the first half of the array, using Merge Sort 2) Sort the second half of the array, using Merge Sort Now, we do indeed have a situation where we can use the Merge function! Each half is already sorted! 3) So simply merge the first half of the array with the second half. And this points to a recursive solution Sorting: Merge Sort page 19

Sorting: Merge Sort Merge Sort Conceptually, Merge Sort works as follows: If the list is of length 0 or 1, then it is already sorted! Otherwise: 1. Divide the unsorted list into two sub-lists of about half the size So if your list has n elements, you will divide that list into two sub-lists, each having approximately n/2 elements: 2. Recursively sort each sub-list by calling recursively calling Merge Sort on the two smaller lists 3. Merge the two sub-lists back into one sorted list Sorting: Merge Sort page 20

Sorting: Merge Sort Merge Sort Basically, given a list: You will split this list into two lists of about half the size Then you recursively call Merge Sort on each list What does that do? Each of these new lists will, individually, be split into two lists of about half the size. So now we have four lists, each about ¼ the size of the original list This keeps happening the lists keep getting split into smaller and smaller lists Until you get to a list of size 1 or size 0 Then we Merge them into a larger, sorted list Sorting: Merge Sort page 21

Sorting: Merge Sort Merge sort idea: Divide the array into two halves. Recursively sort the two halves (using merge sort). Use Merge to combine the two arrays. mergesort(0, n/2-1) mergesort(n/2, n-1) sort merge(0, n/2, n-1) sort Sorting: Merge Sort page 22

98 23 45 14 6 67 33 42 Sorting: Merge Sort page 23

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 Sorting: Merge Sort page 24

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 Sorting: Merge Sort page 25

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 Sorting: Merge Sort page 26

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 Merge Sorting: Merge Sort page 27

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 23 Merge Sorting: Merge Sort page 28

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 23 98 Merge Sorting: Merge Sort page 29

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 23 98 Sorting: Merge Sort page 30

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 23 98 Merge Sorting: Merge Sort page 31

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 23 98 14 Merge Sorting: Merge Sort page 32

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 23 98 14 45 Merge Sorting: Merge Sort page 33

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 23 98 14 45 Merge Sorting: Merge Sort page 34

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 23 98 14 45 14 Merge Sorting: Merge Sort page 35

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 23 98 14 45 14 23 Merge Sorting: Merge Sort page 36

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 23 98 14 45 14 23 45 Merge Sorting: Merge Sort page 37

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 23 98 14 45 14 23 45 98 Merge Sorting: Merge Sort page 38

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 23 98 14 45 14 23 45 98 Sorting: Merge Sort page 39

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 23 98 14 45 14 23 45 98 Sorting: Merge Sort page 40

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 23 98 14 45 14 23 45 98 Merge Sorting: Merge Sort page 41

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 23 98 14 45 6 14 23 45 98 Merge Sorting: Merge Sort page 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 23 98 14 45 6 67 14 23 45 98 Merge Sorting: Merge Sort page 43

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 14 23 45 98 Sorting: Merge Sort page 44

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 14 23 45 98 Merge Sorting: Merge Sort page 45

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 14 23 45 98 Merge Sorting: Merge Sort page 46

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 Merge Sorting: Merge Sort page 47

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 Merge Sorting: Merge Sort page 48

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 Merge Sorting: Merge Sort page 49

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 Merge Sorting: Merge Sort page 50

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 Merge Sorting: Merge Sort page 51

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 Merge Sorting: Merge Sort page 52

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 Merge Sorting: Merge Sort page 53

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 6 Merge Sorting: Merge Sort page 54

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 6 14 Merge Sorting: Merge Sort page 55

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 6 14 23 Merge Sorting: Merge Sort page 56

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 6 14 23 33 Merge Sorting: Merge Sort page 57

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 6 14 23 33 42 Merge Sorting: Merge Sort page 58

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 6 14 23 33 42 45 Merge Sorting: Merge Sort page 59

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 6 14 23 33 42 45 67 Merge Sorting: Merge Sort page 60

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 6 14 23 33 42 45 67 98 Merge Sorting: Merge Sort page 61

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 6 14 23 33 42 45 67 98 Sorting: Merge Sort page 62

98 23 45 14 6 67 33 42 6 14 23 33 42 45 67 98 Sorting: Merge Sort page 63

Sorting: Merge Sort Merge Sort Conceptual Understanding To make the explanation easy, we said that the first step is to divide the array into two smaller arrays of about equal size Then we recursively call Merge Sort on the first half of the array Then we recursively call Merge Sort on the second half of the array. Finally, we Merge. However, this is not what happens in code! Sorting: Merge Sort page 64

Sorting: Merge Sort Merge Sort Reality of the Code However, in reality, we do NOT divide the array into two new arrays. This is unnecessary and would take a lot of memory What really happens? There is only ONE array. There are three steps: 1. Recursively Merge Sort the left half of the array. 2. Recursively Merge Sort the right half of the array. 3. Merge the two sorted halves together. Sorting: Merge Sort page 65

Sorting: Merge Sort Merge Sort Psuedocode Here s the idea of what happens in the code: method MergeSort(array) { 1. MergeSort(left half of array) 2. MergeSort(right half of Array) 3. Merge } Sorting: Merge Sort page 66

Sorting: Merge Sort (real example) 13 6 21 18 9 4 8 20 0 7 13 6 21 18 0 3 9 4 8 20 4 7 13 6 21 18 9 4 8 20 0 1 2 3 4 5 6 7 13 6 21 18 9 4 8 20 0 1 2 3 4 5 6 7 6 13 18 21 4 9 8 20 0 1 2 3 4 5 6 7 6 13 18 21 4 8 9 20 0 3 4 7 4 6 8 9 13 18 20 21 0 7 Sorting: Merge Sort page 67

Brief Interlude: FAIL Picture Sorting: Merge Sort page 68

Daily Bike Fail Sorting: Merge Sort page 69

Weekly Bike Fail Sorting: Merge Sort page 70

Sorting: Merge Sort Merge Sort Code public void MergeSort(int values[], int start, int end) { int mid; // Check if our sorting range is more than one element. if (start < end) { mid = (start+end)/2; // Sort the first half of the values. MergeSort(values, start, mid); // Sort the last half of the values. MergeSort(values, mid+1, end); } } // Put it all together. Merge(values, start, mid+1, end); Sorting: Merge Sort page 71

Sorting: Merge Sort Merge Code This code is longer And a bit convoluted But all it does it Merge the values from two arrays into one larger array Of course, keeping the items in order Just like the example shown earlier in the slides You can find Merge code in the book and online Or you can code it up yourself from the example shown earlier in the slides Sorting: Merge Sort page 72

Sorting: Merge Sort Merge Sort Analysis Again, here are the steps of Merge Sort: 1) Merge Sort the first half of the list 2) Merge Sort the second half of the list 3) Merge both halves together Let T(n) be the running time of Merge Sort on an input size n Then we have: T(n) = (Time in step 1) + (Time in step 2) + (Time in step 3) Sorting: Merge Sort page 73

Sorting: Merge Sort Merge Sort Analysis T(n): running time of Merge Sort on input size n Therefore, we have: T(n) = (Time in step 1) + (Time in step 2) + (Time in step 3) Notice that Step 1 and Step 2 are sorting problems also But they are of size n/2 we are halving the input And the Merge function runs in O(n) time Thus, we get the following equation for T(n) T(n) = T(n/2) + T(n/2) + O(n) T(n) = 2T(n/2) + O(n) Sorting: Merge Sort page 74

Sorting: Merge Sort Merge Sort Analysis T(n) = 2T(n/2) + O(n) For the time being, let s simplify O(n) to just n T(n) = 2T(n/2) + n and we know that T(1) = 1 So we now have a Recurrence Relation Is it solved? NO! Why? Damn T s! Sorting: Merge Sort page 75

Sorting: Merge Sort Merge Sort Analysis T(n) = 2T(n/2) + n and T(1) = 1 So we need to solve this, by removing the T( ) s from the right hand side Then T(n) will be in its closed form And we can state its Big-O running time We do this in steps We replace n with n/2 on both sides of the equation We plug the result back in And then we do it again till a light goes off and we see something Sorting: Merge Sort page 76

Sorting: Merge Sort Merge Sort Analysis T(n) = 2T(n/2) + n and T(1) = 1 Do you know what T(n/2) equals Does it equal 2,125 operations? We don t know! So we need to develop an equation for T(n/2) How? Take the original equation shown above Wherever you see an n, substitute with n/2 T(n/2) = 2T(n/4) + n/2 So now we have an equation for T(n/2) Sorting: Merge Sort page 77

Sorting: Merge Sort Merge Sort Analysis T(n) = 2T(n/2) + n T(n/2) = 2T(n/4) + n/2 So now we have an equation for T(n/2) We can take this equation and substitute it back into the original equation T(n) = 2T(n/2) + n = 2[2T(n/4) + n/2] + n now simplify T(n) = 4T(n/4) + 2n and T(1) = 1 Same thing here: do you know what T(n/4) equals? No we don t! So we need to develop an eqn for T(n/4) Sorting: Merge Sort page 78

Sorting: Merge Sort Merge Sort Analysis T(n) = 2T(n/2) + n T(n/2) = 2T(n/4) + n/2 T(n) = 4T(n/4) + 2n Same thing here: do you know what T(n/4) equals? No we don t! So we need to develop an eqn for T(n/4) Take the eqn above and again substitute n/2 for n T(n/4) = 2T(n/8) + n/4 and T(1) = 1 So now we have an equation for T(n/4) We can take this equation and substitute it back the equation that we currently have in terms of T(n/4) Sorting: Merge Sort page 79

Sorting: Merge Sort Merge Sort Analysis T(n) = 2T(n/2) + n T(n/2) = 2T(n/4) + n/2 T(n) = 4T(n/4) + 2n T(n/4) = 2T(n/8) + n/4 So now we have an equation for T(n/4) We can take this equation and substitute it back the equation that we currently have in terms of T(n/4) T(n) = 4T(n/4) + 2n = 4[2T(n/8) + n/4] + 2n Simplify a bit T(n) = 8T(n/8) + 3n and T(1) = 1 Sorting: Merge Sort page 80

Sorting: Merge Sort Merge Sort Analysis So now we have three equations for T(n): T(n) = 2T(n/2) + n 1 st step of recursion T(n) = 4T(n/4) + 2n 2 nd step of recursion T(n) = 8T(n/8) + 3n 3 rd step of recursion So on the kth step/stage of the recursion, we get a generalized recurrence relation: T(n) = 2 k T(n/2 k ) +kn k th step of recursion Whew! So now we re done right? Wrong! Sorting: Merge Sort page 81

Sorting: Merge Sort Merge Sort Analysis So on the kth step/stage of the recursion, we get a generalized recurrence relation: T(n) = 2 k T(n/2 k ) +kn We need to get rid of the T( ) s on the right side Remember, we know T(1) = 1 So we make a substitution: Let n = 2 k and also solve for k k = log 2 n Plug these back in Sorting: Merge Sort page 82

Sorting: Merge Sort Merge Sort Analysis So on the kth step/stage of the recursion, we get a generalized recurrence relation: T(n) = 2 k T(n/2 k ) +kn Let n = 2 k and also solve for k k = log 2 n Plug these back in T(n) = 2 log 2 n T(n/n) +(log 2 n)n T(n) = n*t(1) + nlogn = n + n*logn So Merge Sort runs in O(n*logn) time Sorting: Merge Sort page 83

Sorting: Merge Sort Merge Sort Summary Avoids all the unnecessary swaps of n 2 sorts Uses recursion to split up a list until we get to lists of 1 or 0 elements Uses a Merge function to merge ( sort ) these smaller lists into larger lists Is MUCH faster than n 2 sorts Merge Sort runs in O(nlogn) time Sorting: Merge Sort page 84

Sorting: Merge Sort WASN T THAT THE COOLEST! Sorting: Merge Sort page 85

Daily Demotivator Sorting: Merge Sort page 86

Sorting: Merge Sort College of Computing & Information Technology King Abdulaziz University CPCS-204 Data Structures I