Answered step by step
Verified Expert Solution
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.
Merge Sort
Merge sort is a sorting algorithm. It sorts a given array dotsn of
numbers by dividing it into two halves 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 for the
pseudocodes of the two algorithms. Also see Figure for an illustration
of the algorithm on the list
Assuming that is a power of the number of comparisons in
merge sort is given by for and
In the worst case, ; to see this, imagine the pointer
pointing to the element in the sorted left list of elements to be compared
with an element in the right list and the pointer pointing to the element
in the sorted right list of elements to be compared with an element in
ALGORITHM MergesortAn
Sorts array by recursive mergesort
Input: An artay of orderable elements
Output: Array sorted in nondecreasing order
if
copy to ALGORITHM MergesortAn
Sorts array by recursive mergesort
Input: An artay of orderable elements
Output: Atray sorted in nondecreasing order
if
copy to
copy to ~
Mergesort
Mergesort ~~
Merge sce bulow
ALGORITHM Mergedotsqdotsp
Merges two sorted arrays into one soried nrray
Input: Arrays dotsp and both sorted
Output: Sorted array of the elements of and
ilarr;jlarr;klarr
while ;ilarri;jlarrjkpropk for and
;ilarri
else ;jlarrj
kpropk
else copy
Figure Merge: Sort
the left list. the beginning the merge, both these pointers are pointing
the first elements their two lists. After each comparison, exactly one
the pointers moves one position the right. The process ends once
one the pointers has more moves the right after the comparison.
Thus the worst case, each pointer may have moves before one
final comparison made, meaning most comparisons are made.
follows that observes the following recursion.
for and
Solving the recursion, get i and
;ilarri
else ;jlarrj
kpropk
else copy
Figure Merge: Sort
the left list. the beginning the merge, both these pointers are pointing
the first elements their two lists. After each comparison, exactly one
the pointers moves one position the right. The process ends once
one the pointers has more moves the right after the comparison.
Thus the worst case, each pointer may have moves before one
final comparison made, meaning most comparisons are made.
follows that observes the following recursion.
for and
Solving the recursion, get
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started