Sorting, Merge Sort and the Divide-and-Conquer Technique

Similar documents
Fast Robust Hashing. ) [7] will be re-mapped (and therefore discarded), due to the load-balancing property of hashing.

Budgeting Loans from the Social Fund

Discounted Cash Flow Analysis (aka Engineering Economy)

Teaching fractions in elementary school: A manual for teachers

Secure Network Coding with a Cost Criterion

NCH Software Warp Speed PC Tune-up Software

Market Design & Analysis for a P2P Backup System

Teamwork. Abstract. 2.1 Overview

An Idiot s guide to Support vector machines (SVMs)

3.5 Pendulum period :40:05 UTC / rev 4d4a39156f1e. g = 4π2 l T 2. g = 4π2 x1 m 4 s 2 = π 2 m s Pendulum period 68

Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally

Multi-Robot Task Scheduling

Pricing Internet Services With Multiple Providers

Early access to FAS payments for members in poor health

1B11 Operating Systems. Input/Output and Devices

Pay-on-delivery investing

Betting on the Real Line

Breakeven analysis and short-term decision making

Australian Bureau of Statistics Management of Business Providers

SAT Math Must-Know Facts & Formulas

LADDER SAFETY Table of Contents

ELECTRONIC FUND TRANSFERS YOUR RIGHTS AND RESPONSIBILITIES

SAT Math Facts & Formulas

Chapter 1 Structural Mechanics

Avaya Remote Feature Activation (RFA) User Guide

Simultaneous Routing and Power Allocation in CDMA Wireless Data Networks

NCH Software Bolt PDF Printer

Capacity of Multi-service Cellular Networks with Transmission-Rate Control: A Queueing Analysis

DEGREES OF ORDERS ON TORSION-FREE ABELIAN GROUPS

Pricing and Revenue Sharing Strategies for Internet Service Providers

Betting Strategies, Market Selection, and the Wisdom of Crowds

7. Dry Lab III: Molecular Symmetry

Income Protection Solutions. Policy Wording

Energy Density / Energy Flux / Total Energy in 3D

Life Contingencies Study Note for CAS Exam S. Tom Struppeck

25 Indirect taxes, subsidies, and price controls

FRAME BASED TEXTURE CLASSIFICATION BY CONSIDERING VARIOUS SPATIAL NEIGHBORHOODS. Karl Skretting and John Håkon Husøy

SNMP Reference Guide for Avaya Communication Manager

5. Introduction to Robot Geometry and Kinematics

The guaranteed selection. For certainty in uncertain times

Distribution of Income Sources of Recent Retirees: Findings From the New Beneficiary Survey

The Use of Cooling-Factor Curves for Coordinating Fuses and Reclosers

Advanced ColdFusion 4.0 Application Development Server Clustering Using Bright Tiger

Income Protection Options

TERM INSURANCE CALCULATION ILLUSTRATED. This is the U.S. Social Security Life Table, based on year 2007.

Chapter 3: JavaScript in Action Page 1 of 10. How to practice reading and writing JavaScript on a Web page

Normalization of Database Tables. Functional Dependency. Examples of Functional Dependencies: So Now what is Normalization? Transitive Dependencies

ELECTRONIC FUND TRANSFERS YOUR RIGHTS AND RESPONSIBILITIES. l l l

Oligopoly in Insurance Markets

PENALTY TAXES ON CORPORATE ACCUMULATIONS

A Supplier Evaluation System for Automotive Industry According To Iso/Ts Requirements

In some states, however, the couple must live apart for a period of months or years before they can obtain a no fault divorce.

A Description of the California Partnership for Long-Term Care Prepared by the California Department of Health Care Services

IEEE JOURNAL ON SELECTED AREAS IN COMMUNICATIONS, VOL. 31, NO. 12, DECEMBER

A New Statistical Approach to Network Anomaly Detection

1 Basic concepts in geometry

Lecture 7 Datalink Ethernet, Home. Datalink Layer Architectures

CI/SfB Ro8. (Aq) September The new advanced toughened glass. Pilkington Pyroclear Fire-resistant Glass

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Energy Performance Certificate

Operation Guide

NCH Software MoneyLine

Art of Java Web Development By Neal Ford 624 pages US$44.95 Manning Publications, 2004 ISBN:

PROPAGATION OF SURGE WAVES ON NON-HOMOGENEOUS TRANSMISSION LINES INDUCED BY LIGHTNING STROKE

Chapter 3: Investing: Your Options, Your Risks, Your Rewards

Annual Notice of Changes for 2016

Uncertain Bequest Needs and Long-Term Insurance Contracts 1

Key Features of Life Insurance

Distributed Strategic Interleaving with Load Balancing

APPENDIX 10.1: SUBSTANTIVE AUDIT PROGRAMME FOR PRODUCTION WAGES: TROSTON PLC

NCH Software PlayPad Media Player

ASYMPTOTIC DIRECTION FOR RANDOM WALKS IN RANDOM ENVIRONMENTS arxiv:math/ v2 [math.pr] 11 Dec 2007

DEGREES OF ORDERS ON TORSION-FREE ABELIAN GROUPS

500 IEEE JOURNAL ON SELECTED AREAS IN COMMUNICATIONS, VOL. 31, NO. 3, MARCH 2013

AA Fixed Rate ISA Savings

Finance 360 Problem Set #6 Solutions

The Computation of American Option Price Sensitivities using a Monotone Multigrid Method for Higher Order B Spline Discretizations

Technical Support Guide for online instrumental lessons

Hybrid Process Algebra

Mean-field Dynamics of Load-Balancing Networks with General Service Distributions

Risk Margin for a Non-Life Insurance Run-Off

Design and Analysis of a Hidden Peer-to-peer Backup Market

GREEN: An Active Queue Management Algorithm for a Self Managed Internet

Risk Margin for a Non-Life Insurance Run-Off

Angles formed by 2 Lines being cut by a Transversal

Scheduling in Multi-Channel Wireless Networks

WINMAG Graphics Management System

SPOTLIGHT. A year of transformation

Caibration of a Lib rate and the SABR-LMM

Traffic classification-based spam filter

Take me to your leader! Online Optimization of Distributed Storage Configurations

FLAC Legal Divorce v2 band_layout 1 26/06/ :01 Page 1 July 2014 divorce

Power-Saving Mode for Mobile Computing in Wi-Fi hotspots: Limitations, Enhancements and Open Issues

NCH Software FlexiServer

WEBSITE ACCOUNT USER GUIDE SECURITY, PASSWORD & CONTACTS

NCH Software WavePad Sound Editor

CLOUD service providers manage an enterprise-class

Figure 1. A Simple Centrifugal Speed Governor.

Chapter 2 Traditional Software Development

Flow Pattern Map for In Tube Evaporation and Condensation

Transcription:

Inf2B gorithms and Data Structures Note 7 Sorting, Merge Sort and the Divide-and-Conquer Technique This and a subsequent next ecture wi mainy be concerned with sorting agorithms. Sorting is an extremey important agorithmic probem that often appears as part of a more compicated tas. Quite frequenty, huge amounts of data have to be sorted. It is therefore worthwhie to put some effort into designing efficient sorting agorithms. In this ecture, we wi mainy consider the mergesort agorithm. mergesort is based on the principe of divide-and-conquer. Therefore in this note we wi aso discuss divide-and-conquer agorithms and their anaysis in genera. 7.1 The Sorting Probem The probem we are considering here is that of sorting a coection of items by their eys, which are assumed to be comparabe. We assume that the items are stored in an array that we wi aways denote by. The number of items to be sorted is aways denoted by n. Of course it is aso conceivabe that the items are stored in a ined ist or some other data structure, but we focus on arrays. Most of our agorithms can be adapted for sorting ined ists, athough this may go aong with a oss in efficiency. By assuming we wor with arrays, we assume we may eep a the items in memory at the same time. This is not the case for arge-scae sorting, as we wi see in a ater ecture. We aready saw the insertionsort sorting agorithm in Lecture 2 of this thread. It is dispayed again here as gorithm 1. Reca that the asymptotic worst-case gorithm insertionsort) 1. for j 1 to.ength 1 do 2. a [j] 3. i j 1 4. whie i 0 and [i].ey > a.ey do 5. [i +1] [i] 6. i i 1 7. [i +1] a gorithm 1 running time of insertionsort is n 2 ). For a sorting agorithm, this is quite poor. We wi see a coupe of agorithms that do much better. 1 7.2 Important characteristics of Sorting gorithms The main characteristic we wi be interested in for the various sorting agorithms we study is worst-case running-time we wi aso refer to best-case and averagecase from time to time). However, we wi aso be interested in whether our sorting agorithm is in-pace and whether it is stabe. Definition 2 n sorting agorithm is said to be an in-pace agorithm if it can be simpy) impemented on the input array, with ony O1) extra space extra variabes). Ceary it is usefu to have an in-pace agorithm if we want to minimize the amount of space used by our agorithm. The issue of minimizing space ony reay becomes important when we have a arge amount of data, for appications such as web-indexing. Definition 3 sorting agorithm is said to be stabe if for every i, j such that i<j and [i].ey = [j].ey, we are guaranteed that [i] comes before [j] in the output array. Stabiity is a desirabe property for a sorting agorithm. We won t reay see why it is usefu in Inf2B if you tae the 3rd-year gorithms and Data Structures course, you see that it can ead to a powerfu reduction in running-time for some cases of sorting 1 ). If we refer bac to insertionsort, we can see that it is ceary an in-pace sorting agorithm, as it operates directy on the input array. so, notice that the test [i].ey > a.ey in ine 4. is a strict inequaity and so that when [i] is inserted into the aready sorted) array [1...i 1], it wi be inserted after any eement with the same ey within [1...i 1]. Hence insertionsort is stabe. 7.3 Merge Sort The idea of merge sort is very simpe: spit the array to be sorted into haves, sort both haves recursivey, and then merge the two sorted subarrays together to one sorted array. gorithm 4 impements this idea in a straightforward manner. To mae the recursive impementation possibe, we introduce two additiona parameters i and j maring the boundaries of the subarray to be sorted; merge- Sort, i, j) sorts the subarray [i... j] =h[i], [i +1],..., [j]i. To sort the whoe array, we obviousy have to ca mergesort, 0, n 1). The ey to MergeSort is the merge) method, caed as merge, i, mid, j) in gorithm 4. ssuming that i appe mid < j and that the subarrays [i... mid] and [mid+1...j] are both sorted, merge, i, mid, j) sorts the whoe subarray [i...j]. The merging is achieved by first defining a new array B to hod the sorted data. Then we initiaise an index for the [i... mid] subarray to i, and index ` for the [mid +1...j] subarray to mid +1. We wa these indices up the array, at each step storing the minimum of [] and [`] in B, and then incrementing that index and the current index of B. 1 This is the Radix-sort agorithm, if you want to oo this up. 2

Inf2B gorithms and Data Structures Note 7 gorithm mergesort, i, j) 1. if i<jthen 2. mid b i+j 2 c 3. mergesort, i, mid) 4. mergesort, mid +1, j) 5. merge, i, mid, j) gorithm 4 gorithm 5 shows an impementation of merge). Note that at the end of the initia merge oop on ine 3) there might be some entries eft in the ower haf of or the upper haf but not both; so at most one of the oops on ines 11 and 15 woud be executed these just append any eft over entries to the end of B). Figure 6 shows the first few steps of merge) on an exampe array. Let us anayse the running time of merge. Let n = j i+1, i.e., n is the number of entries in the subarray [i...j]. There are no nested oops, and ceary each of the oops of merge is iterated at most n times. Thus the running time of merge is On). Since the ast oop is certainy executed n times, the running time is aso n) and thus n). ctuay, the running time of merge is n) no matter what the input array is i.e., even in the best case). Now et us oo at mergesort. gain we et n = j i +1. We get the foowing expression for the running time T mergesort n): T mergesort 1) = 1), T mergesort n) = 1) + T mergesort dn/2e)+t mergesort bn/2c)+t merge n). gorithm merge, i, mid, j) 1. initiaise new array B of ength j i +1 2. i; ` mid +1; m 0 3. whie appe mid and ` appe j do merge haves of into B) 4. if [].ey appe [`].ey then 5. B[m] [] 6. +1 7. ese 8. B[m] [`] 9. ` ` +1 10. m m +1 11. whie appe mid do copy anything eft in ower haf of to B) 12. B[m] [] 13. +1 14. m m +1 15. whie ` appe j do copy anything eft in upper haf of to B) 16. B[m] [`] 17. ` ` +1 18. m m +1 19. for m =0to j i do copy B bac to ) 20. [m + i] B[m] Since we aready now that the running time of merge is n), we can simpify this to T mergesort n) =T mergesort dn/2e)+t mergesort bn/2c)+ n). gorithm 5 We wi see in 7.5 that soving this recurrence yieds 8 11 12 4 9 21 4 B T mergesort n) = n gn)). Our anaysis wi actuay show that the running time of mergesort is n gn)) no matter what the input is, i.e., even in the best case. The runtime of n gn)) is not surprising. The recurrence shows that for an input of size n the cost is that of the two recursive cas pus a cost proportiona to n. Now each recursive ca is to inputs of size essentiay n/2. Each of these maes two recursive cas for size n/4 and we aso charge a cost proportiona to n/2. So the cost of the next eve is a one off cost proportiona to n taing account of the two n/2 one off costs) and 4 cas to size n/4. So we see that at each recursive ca we pay a one off cost that is proportiona to n and then mae 2 r cas to inputs of size n/2 r. The base case is reached when n/2 r =1, i.e., when r =gn). So we have gn) eves each of which has a cost proportiona to n. 3 =i =mid+1 8 11 12 4 9 21 4 8 B 8 11 12 4 9 21 4 8 9 B 8 11 12 4 9 21 Figure 6. The first few steps of merge) on two component subarrays. 4 m

Inf2B gorithms and Data Structures Note 7 Thus the tota cost is proportiona to n gn). We wi prove the correctness of this setch argument beow. Since n gn) grows much sower than n 2, mergesort is much more efficient than insertionsort. s simpe experiments show, this can aso be observed in practice. So usuay mergesort is preferabe to insertionsort. However, there are situations where this is not so cear. Suppose we want to sort an array that is amost sorted, with ony a few items that are sighty out of pace. For such amost sorted arrays, insertionsort wors in amost inear time, because very few items have to be moved. mergesort, on the other hand, uses time n gn)) no matter what the input array oos ie. insertionsort aso tends to be faster on very sma input arrays, because there is a certain overhead caused by the recursive cas in mergesort. Maybe most importanty, mergesort wastes a ot of space, because merge copies the whoe array to an intermediate array. Ceary mergesort is not an in-pace agorithm in the sense of Definition 2. s to the question of stabiity for mergesort, ine 4 of merge achieves stabiity for mergesort ony because it has a non-strict inequaity for testing when we shoud put the item [] from the eft subarray) down before the item [`] from the right subarray). In some paces you may see non-stabe versions of mergesort, where a strict inequaity is used in ine 4. 7.4 Divide-and-Conquer gorithms The divide-and-conquer technique invoves soving a probem in the foowing way: 1) Divide the input instance into severa instances of the same probem of smaer size. 2) Recursivey sove the probem on these smaer instances. 3) Combine the soutions for the smaer instances to a soution for the origina instance so after the recursive cas, we do some extra wor to find the soution for our origina instance). Obviousy, mergesort is an exampe of a divide-and-conquer agorithm. Suppose now that we want to anayse the running time of a divide-andconquer agorithm. Denote the size of the input by n. Furthermore, suppose that the input instance is spit into a instances of sizes n 1,...,n a, for some constant a 1. Then we get the foowing recurrence for a running time T n): T n) =T n 1 )+...+ T n a )+fn), where fn) is the time required by steps 1) for setting up the recursions) and 3) for the extra wor ). Typicay, n 1,...,n are a of the form bn/bc or dn/be for some b>1. Disregarding foors and ceiings for a moment, we get a recurrence of the form: T n) =atn/b)+fn). 5 If we want to tae foors and ceiings into account, we have to repace this by T n) =a 1 T bn/bc)+a 2 T dn/be)+fn). It is usuay the case that the asymptotic growth rate of the soution to the recurrence does not depend on foors and ceiings. Throughout this ecture note we wi usuay disregard the foors and ceiings this can be justified formay). To specify the recurrence fuy, we need aso to say what happens for sma instances that can no onger be divided, and we must specify the function f. We can aways assume that for input instances of size beow some constant n 0 typicay, n 0 =2), the agorithm requires 1) steps. typica function f occurring in the anaysis of divide-and-conquer agorithms has an asymptotic growth rate of n ), for some constant. To concude: The anaysis of divide-and-conquer agorithms usuay yieds recurrences of the form 1) if n<n 0, T n) = 7.1) atn/b)+ n ) if n n 0, where n 0,a2 N, 2 N 0, and b 2 R with b>1 are constants. We normay assume that n is a power of b so that n/b is an integer as the recurrence unwinds. Without this assumption we must use foor or ceiing functions as appropriate. The fact is that the asymptotic growth rate for the type of recurrence shown; see CLRS for a discussion.) 7.5 Soving Recurrences Exampe 7. Reca that for the running time of merge sort we got the foowing recurrence: 1) if n<2, T mergesort n) = T mergesort dn/2e)+t mergesort bn/2c)+ n) if n 2. We first assume that n is a power of 2, say, n =2`; in this case we can omit foors and ceiings. Then T n) = 2T n/2) + n) = 2 2T n/4) + n/2) + n) = 4T n/4) + 2 n/2) + n) = 4 2T n/8) + n/4) +2 n/2) + n) = 8T n/8) + 4 n/4) + 2 n/2) + n). X 1 = 2 T n/2 )+ 2 i n/2 i ). X` 1 = 2`T n/2`)+ 2 i n/2 i ) 6

Inf2B gorithms and Data Structures Note 7 X` 1 = nt 1) + 2 i n/2 i ) gn) 1 X = n 1) + 2 i n/2 i ) = n)+ n gn)) = n gn)). Stricty speaing the argument just given, usuay referred to as unwinding the recurrence, is incompete those dots in the midde). We have taen it on trust that the ast ines are the correct outcome. We can easiy fi in this gap using induction e.g. by proving that the caimed soution is indeed correct). However as ong as the unwinding process is carried out carefuy the induction step is a matter of routine and is usuay omitted. You shoud however be abe to suppy it if ased. though we have ony treated the specia case when n is a power of 2, we can now use a tric to prove that we have T n) = n gn) for a n. We mae the reasonabe assumption that T n) appe T n +1) this can be proved from the recurrence for T n) by induction). Now notice that for every n, there is some exponent such that n appe 2 < 2n. This is the fact we need. We observe that T n) appe T 2 )=O2 g2 )) = O2n g2n)) = On g n). Liewise for every n 1 there is some 0 such that n/2 < 2 0 appe n. Hence T n) = 2 0 )g2 0 )) = n/2)gn/2)) = ngn). Putting these two together we have T n) = ng n). n aternative approach is to use the fu recurrence directy though this is rather tedious. Note though that once we have a good guess at what the soution is we can prove its correctness by using induction and the fu recurrence, we do need to unwind the compicated version. Before moving on it is worth expaining one possibe error that is sometimes made when unwinding recurrences. We have T n) = 2T n/2) + n) = 2 2T n/4) + n/2) + n) = 4T n/4) + 2 n/2) + n) It is very tempting to simpify the ast ine to 4T n/4) + n) on the basis that 2 n/2) + n) = n) and do iewise at each stage. It is certainy true that 2 n/2) + n) = n). However such repacements are ony justified if we have a constant number of ) terms. In our case we unwind the recurrence to obtain gn) terms and this is not a constant. You are strongy encouraged to unwind the recurrence with the n) repaced by cn where c>0 is some constant. Observe that at each stage we obtain a mutipe of cn as an additive term cn, 2cn, 3cn etc.). Of course after any fixed number of stages we just have a constant mutipe of n. But after ogn) stages this is not the case. By further expoiting the idea used in this exampe, one can prove the foowing genera theorem that gives soutions to recurrences of the form 7.1). Reca that og b a) denotes the ogarithm of a with base b, i.e., we have c =og b a) ) b c = a. 7 For exampe, og 2 8) = 3, og 3 9) = 2, og 4 8) = 1.5. The foowing theorem is caed the Master Theorem for soving recurrences). It maes ife easy by aowing us to read-off the ) expression of a recurrence without going through a proof as we did for mergesort: Theorem 8. Let n 0 2 N, 2 N 0 and a, b 2 R with a>0 and b>1, and et T : N! N satisfy the foowing recurrence: 1), if n<n 0 ; T n) = at n/b)+ n ), if n n 0. Let e =og b a); we ca e the critica exponent. Then 8 < n e ), if <e I); T n) = n e gn)), if = e II); : n ), if >e III). The theorem remains true if we repace at n/b) in the recurrence by a 1 T bn/bc) + a 2 T dn/be) for a 1,a 2 0 with a 1 + a 2 = a. In some situations we do not have a n ) expression for the extra cost part of the recurrence but do have a On ) expression. This is fine, the Mater Theorem sti appies but gives us just an upper bound for the runtime, i.e., in the three cases for the vaue of T n) repace by O. Exampes 9. 1) Now reconsider the recurrence for the running time of mergesort: 1), T mergesort n) = T mergesort dn/2e)+t mergesort bn/2c)+ n), if n 2. In the setting of the Master Theorem, we have n 0 =2, =1, a =2, b =2. Thus e = og b a) = og 2 2) = g2) = 1. Hence T mergesort n) = n gn)) by case II). 2) Reca the recurrence for the running time of binary search: 1), T binarysearch n) = T binarysearch bn/2c)+ 1), if n 2. Here we tae n 0 =2, =0, a =1, b =2. Thus e =og b a) =og 2 1) = 0 and therefore T binarysearch n) 2 gn)) by case II) of the Master Theorem. 3) Let T be a function satisfying 1), T n) = 3T n/2) + n), if n n 0. Then e =og b a) =og 2 3) = g3. Hence T n) 2 n g3) ) by case I). 4) Let T be a function satisfying 1), T n) = 7T n/2) + n 4 ), if n n 0. Then e =og b a) =og 2 7) < 3. Hence T n) 2 n 4 ) by case III) of the Master Theorem. 8

Exercises 1. Sove the foowing recurrences using the Master Theorem: a) T n) =3T n/2) + n 5 b) T n) =4T n/3) + n c) T n) =8T n/3) + n 2 d) T n) =8T n/3) + n e) T n) =8T n/3) + ngn) Remar: The Master Theorem is not directy appicabe in this case. Find a way to use it anyway. In each case T n) is assumed to be 1) for some appropriate base case. 2. Let T n) be the function satisfying the foowing recurrence: 3, if n =1; T n) = T n 1) + 2n, if n 2. Sove the recurrence and give an expicit formua for T. Try to derive a genera formua for the asymptotic growth rate of functions satisfying recurrences of the form 1), if n appe n 0 ; T n) = T n )+fn), if n>n 0, where, n 0 2 N such that 1 appe appe n 0 and f : N! N is a function. 3. Prove that P gn) 1 2 i n/2 i )= ngn)), this was used in soving the recurrence for T mergesort n). This is much easier than it oos, remember that to say f = g) means there is an n 0 2 N and constants c 1,c 2 2 R both stricty bigger than 0 such that c 1 gn) appe fn) appe c 2 gn) for a n n 0. Reca aso the formua for summing geometric series: P s ri =1 r i+1 )/1 r). 9