Heap and Merge Sorts

Similar documents
The Addition Rule. Lecture 44 Section 9.3. Robb T. Koether. Hampden-Sydney College. Mon, Apr 14, 2014

The Plurality-with-Elimination Method

The Plurality-with-Elimination Method

The Pigeonhole Principle

Hamilton s and Jefferson s Methods

Tests of Homogeneity and Independence

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

Basic Algorithms Overview

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

TÜ Information Retrieval

Logicola Truth Evaluation Exercises

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

Different types of braces for adults by Nearest Orthodontist

Zion Lutheran Church Transition Team Report June 2018 A. BEGINNING

6 Don t Walk Away. Hebrews 6:1-8

THE CIRCLE OF LIFE JOURNEY

my changes 1. LEADER PREPARATION

Outline of today s lecture

Pulling Rabbits from Hats (Conditional Probability), Part I

INTRODUCTION TO LOGIC 1 Sets, Relations, and Arguments

1.2. What is said: propositions

Criteria of Identity

The Representation of Logical Form: A Dilemma

Inference in Cyc. Copyright 2002 Cycorp

DPaxos: Managing Data Closer to Users for Low-Latency and Mobile Applications

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

Smith Waterman Algorithm - Performance Analysis

1 Introduction. Cambridge University Press Epistemic Game Theory: Reasoning and Choice Andrés Perea Excerpt More information

YEAR: UNIT-SPECIFIC GOALS (italicized) Assessable Student Outcome

In-House Retreat. SAMPLE SESSION by Sarah Briggs

Whatever happened to cman?

HISTORY OF SOCIAL THEORY I: Community & Religion

Thank you Coaches and Volunteers for serving!

Modern America Cooke. Reconstruction Essay

Can We Avoid the Repugnant Conclusion?

God s Enemies. Why Aren t Other Religions OK?

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

GOSPEL STORY CURRICULUM (NT) LOWER ELEMENTARY LESSON 60. Believe & Confess ROMANS 10:1 17 BIBLE TRUTH SALVATION IN CHRIST IS FREE FOR ALL WHO BELIEVE

Lesson Procedures. Lesson Preparation Print packets for students including: background essay, document set, evidence organizer, assessment and rubric.

AFFIRMATIVE POSITION: Debate AICE: GP/Pavich

Digital Logic Lecture 5 Boolean Algebra and Logic Gates Part I

LIVING ON PURPOSE Week 2: Connecting With Others 1. LEADER PREPARATION

TRANSformED FOLLOWING JESUS: BECOMING A DISCIPLE WHO MAKES DISCIPLES DISCIPLE-MAKER LEADER GUDE

Argument and persuasion essay outline. The end or Act 3 is the and and argument or essay of the outline..

Agnostic KWIK learning and efficient approximate reinforcement learning

The Old Hancock Protestant Cemetery, Circa MS-933

Houghton Mifflin English 2004 Houghton Mifflin Company Level Four correlated to Tennessee Learning Expectations and Draft Performance Indicators

WELS. Other. Second Edition. John F. Brug. Northwestern Publishing House Milwaukee, Wisconsin

CHRIST FELLOWSHIP LIFEGROUP LEADER ORIENTATION FACILITATOR OUTLINE

Teacher BIBLE STUDY. Younger Kids Bible Study Leader Guide Unit 6 Session LifeWay Christian Resources

When you study or studied there. What you remember about it

Number of transcript pages: 13 Interviewer s comments: The interviewer Lucy, is a casual worker at Unicorn Grocery.

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

Solomon Built the Temple

Page 1 of 6 FALL 09 PASSWORDS FOR ONLINE COURSE READERS ENGL 155 (3761) & ENGL098 (3225) LEARNING LOGS FOR MAJOR ESSAY #1

The Shirt: Current Amount Sold: 2

Three Steps to Good Structure

6.080 / Great Ideas in Theoretical Computer Science Spring 2008

Discovering Obedience - Passages Designed to Introduce People to the Commands of Christ Updated 03/04/2015,

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

Junior Soldiers. Talking to God! Consider & Prepare. Unit 3 : Lesson 7

Circle of Influence Strategy (For YFC Staff)

CET Syllabus of Record

Mid-Atlantic Community Examen Evaluation

The Lord s Prayer. Shepherd Guides. The Lord s Prayer 151

GOD CREATED THE WORLD

Allreduce for Parallel Learning. John Langford, Microsoft Resarch, NYC

Making the Most of Your Vestry Retreat

Lead Student Lesson Plan L04: 1 Nephi 15-22

Tusculum Hills Baptist Church Paul Gunn, Pastor

Computational Learning Theory: Agnostic Learning

P R A Y L E A D E R S G U I D E

Lead Student Lesson Plan L11: 4 Nephi Mormon 9

Cambridge International Advanced Subsidiary Level 8053 Islamic Studies November 2013 Principal Examiner Report for Teachers

occasions (2) occasions (5.5) occasions (10) occasions (15.5) occasions (22) occasions (28)

Outline. Uninformed Search. Problem-solving by searching. Requirements for searching. Problem-solving by searching Uninformed search techniques

Distributed Hash Tables

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

A romp through the foothills of logic Session 3

THE TOWARDS AN IDEAL BOTANICAL CURRICULUM. PART III.' ADVANCED UNIVRKSITY TEACHING.

The Upside-Down Kingdom

The Giving Tree by Shel Silverstein. Caring, Friendship, Reciprocity

The King Ascends Lesson Aim: To honor Jesus final words and to look forward to His return.

Particle Sizes and Clumps from Stellar Occultations

Distributed Systems. 11. Consensus: Paxos. Paul Krzyzanowski. Rutgers University. Fall 2015

Trends of Urbanization in Nanded District of Maharashtra State

Sociology 475: Classical Sociological Theory Spring 2012

More Good than Harm. When you see a priest hugging a little boy, what is the first thing that crosses your mind? Are these

Ananias & Sapphira GOSPEL STORY CURRICULUM (NT) LOWER ELEMENTARY THE SPIRIT OF GOD IS POURED OUT IN GRACE, DISCIPLINE, AND POWER LESSON 48

John s Vision of Jesus

Lesson plan: Darkness Dwellers

BBC LEARNING ENGLISH 6 Minute English Have you got too much stuff?

Qur'an And Bible Side By Side: A Non-partial Anthology By Marlies ter Borg READ ONLINE

STUDENTS WILL BE TAUGHT, BECAUSE I AM A PEACEMAKER:

1914 Ridgeview Dr., Allen, TX phone: fax:

The Episcopal Church of Bangor in Caernarvon

St. Catharine St. Margaret Parish Town Hall Meeting St. Catharine School Auditorium February 1, 2017 MEETING SUMMARY

and also defend the topic.it is nature of mankind the discussion and debate make stronger faith. On these circumstances we have selected

The SAT Essay: An Argument-Centered Strategy

Transcription:

Heap and Merge Sorts Lecture 32 Robb T. Koether Hampden-Sydney College Mon, Apr 20, 2015 Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 1 / 22

1 Sorting 2 Comparison of Run Times 3 The Merge Sort 4 The Heap Sort Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 2 / 22

Outline 1 Sorting 2 Comparison of Run Times 3 The Merge Sort 4 The Heap Sort Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 3 / 22

Total Order Relations Definition (Total Order Relation) A total order relation on a set A is a relation that has the following properties. Reflexive: For all a A, a a. Anti-symmetric: For all a, b A, if a b and b a, then a = b. Transitive: For all a, b, c A, if a b and b c, then a c. Comparable: For all a, b A, a b, or b a. Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 4 / 22

Sorting The order is determined by an order operator: <, >, <=, or >=. These operators, together with == and!=, must define a total order on the class. Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 5 / 22

Inefficient Sorting Algorithms Most elementary sorting algorithms are inefficient for long lists. Examples Bubble Sort. Selection Sort. Insertion Sort. These algorithms have run times of order O(n 2 ). They are fine for short lists (length < 10, 000) and ok for length < 100, 000. Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 6 / 22

Efficient Sorting Algorithms The efficient sorting algorithms are more complicated. Examples Merge Sort Heap Sort Quick Sort These algorithms have run times of order O(n log n). They are fast for lists of any practical length. Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 7 / 22

Outline 1 Sorting 2 Comparison of Run Times 3 The Merge Sort 4 The Heap Sort Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 8 / 22

Comparison of Algorithms How much faster is O(n log n) than O(n 2 )? Let s compare. Let A be an algorithm of order O(n 2 ). Let B be an algorithm of order O(n log n). Suppose both algorithms require 1 µsec to process a list of size n = 100. How long will they take to process lists of sizes 10 3, 10 4, 10 5, 10 6, 10 7, 10 8, and 10 9? Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 9 / 22

Comparison of Algorithms Algorithm A has run time Algorithm B has run time n 2 100 2 = 0.0001n2. n log n = 0.005n log n. 100 log 100 Evaluate these functions when n = 10 2, 10 3, 10 4, 10 5, 10 6, 10 7, 10 8, and 10 9. Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 10 / 22

Comparison of Algorithms n Algorithm A Algorithm B 10 2 1 µs 1 µs Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 11 / 22

Comparison of Algorithms n Algorithm A Algorithm B 10 2 1 µs 1 µs 10 3 100 µs 15 µs Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 11 / 22

Comparison of Algorithms n Algorithm A Algorithm B 10 2 1 µs 1 µs 10 3 100 µs 15 µs 10 4 10 ms 200 µs Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 11 / 22

Comparison of Algorithms n Algorithm A Algorithm B 10 2 1 µs 1 µs 10 3 100 µs 15 µs 10 4 10 ms 200 µs 10 5 1 s 2.5 ms Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 11 / 22

Comparison of Algorithms n Algorithm A Algorithm B 10 2 1 µs 1 µs 10 3 100 µs 15 µs 10 4 10 ms 200 µs 10 5 1 s 2.5 ms 10 6 100 s 30 ms Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 11 / 22

Comparison of Algorithms n Algorithm A Algorithm B 10 2 1 µs 1 µs 10 3 100 µs 15 µs 10 4 10 ms 200 µs 10 5 1 s 2.5 ms 10 6 100 s 30 ms 10 7 2.8 h 350 ms Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 11 / 22

Comparison of Algorithms n Algorithm A Algorithm B 10 2 1 µs 1 µs 10 3 100 µs 15 µs 10 4 10 ms 200 µs 10 5 1 s 2.5 ms 10 6 100 s 30 ms 10 7 2.8 h 350 ms 10 8 11.6 d 4 s Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 11 / 22

Comparison of Algorithms n Algorithm A Algorithm B 10 2 1 µs 1 µs 10 3 100 µs 15 µs 10 4 10 ms 200 µs 10 5 1 s 2.5 ms 10 6 100 s 30 ms 10 7 2.8 h 350 ms 10 8 11.6 d 4 s 10 9 3.2 y 45 s Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 11 / 22

Outline 1 Sorting 2 Comparison of Run Times 3 The Merge Sort 4 The Heap Sort Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 12 / 22

The Merge Sort Merging two sorted lists of length n has run time O(n). The run time of the Merge Sort is O(n log n). Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 13 / 22

The Merge Sort Algorithm Begin by considering the list to be a collection of sublists each of length 1. 50 30 70 20 80 40 10 60 Merge adjacent sublists in pairs. Continue to merge adjacent sublists until there remains only one sublist. Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 14 / 22

The Merge Sort The Merge Sort 50 30 70 20 80 40 10 60 Begin with a list of size n = 8. Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 15 / 22

The Merge Sort The Merge Sort 30 50 70 20 80 40 10 60 Pass #1: Merge the 1st and 2nd lists. Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 15 / 22

The Merge Sort The Merge Sort 30 50 20 70 80 40 10 60 Pass #1: Merge the 3rd and 4th lists. Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 15 / 22

The Merge Sort The Merge Sort 30 50 20 70 40 80 10 60 Pass #1: Merge the 5th and 6th lists. Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 15 / 22

The Merge Sort The Merge Sort 30 50 20 70 40 80 10 60 Pass #1: Merge the 7th and 8th lists. Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 15 / 22

The Merge Sort The Merge Sort 20 30 50 70 40 80 10 60 Pass #2: Merge the 1st and 2nd lists. Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 15 / 22

The Merge Sort The Merge Sort 20 30 50 70 10 40 60 80 Pass #2: Merge the 3rd and 4th lists. Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 15 / 22

The Merge Sort The Merge Sort 10 20 30 40 50 60 70 80 Pass #3: Merge the 1st and 2nd lists. Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 15 / 22

Outline 1 Sorting 2 Comparison of Run Times 3 The Merge Sort 4 The Heap Sort Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 16 / 22

The Heap Sort Definition Heap Sort A heap is a binary tree that has the following structure: At each node, the value at that node is greater than or equal to each of its children. Every level except the lowest level is full. The nodes in the lowest level are as far to the left as possible. The Heap Sort algorithm uses a heap. The nodes are indexed from 0 to size - 1 in level order. Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 17 / 22

A Heap 90 80 50 60 70 30 10 20 40 A heap Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 18 / 22

The Heap Sort algorithm is as follows. Heapify the tree (to be explained). Set end equal to size - 1. While end > 0, do the following. Swap the root value with the end value. Sift down the root value down through the tree (to be explained). Decrement end. Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 19 / 22

The siftdown() Function The siftdown() function proceeds as follows. Begin at the root node. While the node value is smaller than at least one of its active children, Swap it with the larger of its children. Make that child node the current node. Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 20 / 22

The heapify() Function The heapify() function proceeds as follows. Begin with node = end/2, i.e., the last node with a child. While node > 1, do the following Sift the node down. Decrement the node. Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 21 / 22

A Heap 40 80 50 60 70 30 10 20 90 Swap 90 and 40 Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 22 / 22

A Heap 80 40 50 60 70 30 10 20 90 Swap 80 and 40 Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 22 / 22

A Heap 80 70 50 60 40 30 10 20 90 Swap 70 and 40 Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 22 / 22

A Heap 20 70 50 60 40 30 10 80 90 Swap 80 and 20 Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 22 / 22

A Heap 70 20 50 60 40 30 10 80 90 Swap 70 and 20 Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 22 / 22

A Heap 70 60 50 20 40 30 10 80 90 Swap 60 and 20 Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 22 / 22

A Heap 10 60 50 20 40 30 70 80 90 Swap 70 and 10 Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 22 / 22

A Heap 60 10 50 20 40 30 70 80 90 Swap 60 and 10 Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 22 / 22

A Heap 60 40 50 20 10 30 70 80 90 Swap 40 and 10 Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 22 / 22

A Heap 30 40 50 20 10 60 70 80 90 Swap 60 and 30 Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 22 / 22

A Heap 50 40 30 20 10 60 70 80 90 Swap 50 and 30 Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 22 / 22

A Heap 10 40 30 20 50 60 70 80 90 Swap 50 and 10 Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 22 / 22

A Heap 40 10 30 20 50 60 70 80 90 Swap 40 and 10 Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 22 / 22

A Heap 40 20 30 10 50 60 70 80 90 Swap 20 and 10 Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 22 / 22

A Heap 10 20 30 40 50 60 70 80 90 Swap 40 and 10 Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 22 / 22

A Heap 30 20 10 40 50 60 70 80 90 Swap 30 and 10 Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 22 / 22

A Heap 10 20 30 40 50 60 70 80 90 Swap 30 and 10 Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 22 / 22

A Heap 20 10 30 40 50 60 70 80 90 Swap 20 and 10 Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 22 / 22

A Heap 10 20 30 40 50 60 70 80 90 Swap 20 and 10 Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 22 / 22

A Heap 10 20 30 40 50 60 70 80 90 Done Robb T. Koether (Hampden-Sydney College) Heap and Merge Sorts Mon, Apr 20, 2015 22 / 22