Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Methods In this approach, the original problem is broken up into two smaller in - stances of the same problem and the solutions of the

Methods
In this approach, the original problem is broken up into two smaller in-
stances of the same problem and the solutions of the two smaller instances
were used to construct the solution to the original problem. As with the pre-
vious method, this approach can also use several iterations until the smaller
instances can be trivially solved and then reconstruct the solution to the
original problem recursively. In this chapter, we study a few algorithms
using this approach and analyze their complexities.
5.1. Merge Sort
Merge sort is a sorting algorithm. It sorts a given array A[0dotsn-1] of
numbers by dividing it into two halves A[0..(n2]-1) and ,
sorting each of them recursively, and then merging the two smaller sorted
lists into a single sorted list of the original array. We describe this process
using two algorithms. The main algorithm MergeSort will divide the array
into two halves and recursively call itself on the smaller arrays first and then
call the algorithm Merge to merge the two sorted lists. Sex Figure 5.1 for the
pseudocodes of the two algorithms. Also see Figure 5.2 for an illustration
of the algorithm on the list 8,3,2,9,7,1,5,4.
Assuming that n is a power of 2, the number of comparisons C(n) in
merge sort is given by,C(n)=2C(n2)+Cmerge(n) for n>1 and C(1)=0.
In the worst case, Cmarge(n)=n-1; to see this, imagine the pointer l
pointing to the element in the sorted left list of n2 elements to be compared
with an element in the right list and the pointer r pointing to the element
in the sorted right list of n2 elements to be compared with an element in
ALGORITHM Mergesort(A[0,n-1])
??? Sorts array A[0,.n-1] by recursive mergesort
//Input: An artay A[0..n-1] of orderable elements
//Output: Array A[0.,n-1] sorted in nondecreasing order
if n>1
copy[0.|n2|-1] to H|0|n2|-1.ALGORITHM Mergesort(A[0,n-1])
//Sorts array A[0,n-1] by recursive mergesort
//Input: An artay A[0..n-1] of orderable elements
//Output: Atray A[0,.n-1] sorted in nondecreasing order
if n1
copy A[0..|??n2??|-1] to B[0.|??|??n2??|-1]
copy A||??n2??|..n-1| to C|0..[n2~|-1]
Mergesort (B[0..|??n2??|-1])
Mergesort (C|0..|~2~|-1]
MergeB,C,A?? sce bulow
ALGORITHM Merge(B[0.p-1],C[0dotsq-1],A[0dotsp+q-1])
//Merges two sorted arrays into one soried nrray
//Input: Arrays B[0dotsp-1] and C[0.ag-1] both sorted
//Output: Sorted array A[0.,p+q-1] of the elements of B and C
ilarr0;jlarr0;klarr0
while B[i]C[j]A[k]larrB[i];ilarri+1A[k]larrC[j];jlarrj+1kpropk+1i=pC[j.q-1]A[k..p+q-1]B[idotspp-1]A[k.p+q-1]n2-1n-1Cworst(n)Cworst(n)=2Cworkt(n2)+n-1 for n>1 and Cworst(1)=0.Cworst(n)=nlogn-n+1in(nlogn)jdo
ifB[i]C[j]
A[k]larrB[i];ilarri+1
else A[k]larrC[j];jlarrj+1
kpropk+1
ifi=p
CpyC[j.q-1]toA[k..p+q-1]
else copy B[idotspp-1]toA[k.p+q-1]
Figure 5.1. Merge: Sort
the left list. At the beginning of the merge, both these pointers are pointing
to the first elements in their two lists. After each comparison, exactly one
of the pointers moves by one position to the right. The process ends once
one of the pointers has no more moves to the right after the comparison.
Thus in the worst case, each pointer may haven2-1 moves before one
final comparison is made, meaning at most n-1 comparisons are made. It
follows that Cworst(n) observes the following recursion.
Cworst(n)=2Cworkt(n2)+n-1 for n>1 and Cworst(1)=0.
Solving the recursion, we get Cworst(n)=nlogn-n+1in(nlogn).i and jdo
ifB[i]C[j]
A[k]larrB[i];ilarri+1
else A[k]larrC[j];jlarrj+1
kpropk+1
ifi=p
CpyC[j.q-1]toA[k..p+q-1]
else copy B[idotspp-1]toA[k.p+q-1]
Figure 5.1. Merge: Sort
the left list. At the beginning of the merge, both these pointers are pointing
to the first elements in their two lists. After each comparison, exactly one
of the pointers moves by one position to the right. The process ends once
one of the pointers has no more moves to the right after the comparison.
Thus in the worst case, each pointer may haven2-1 moves before one
final comparison is made, meaning at most n-1 comparisons are made. It
follows that Cworst(n) observes the following recursion.
Cworst(n)=2Cworkt(n2)+n-1 for n>1 and Cworst(1)=0.
Solving the recursion, we get Cworst(n)=nlogn-n+1in(nlogn).

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

to encourage a drive for change by developing new ideas;

Answered: 1 week ago

Question

4 What are the alternatives to the competences approach?

Answered: 1 week ago