0% found this document useful (0 votes)
6 views

Sorting Algorithms

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Sorting Algorithms

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Sorting and Selection

Merge-Sort
Divide-and-Conquer

The first two algorithms we describe in this chapter, merge-sort and quick-sort, use recursion in an algorithmic design
pattern called divide-and-conquer.

The divide-and-conquer pattern consists of the following three steps:


1. Divide: If the input size is smaller than a certain threshold (say, one or two elements), solve the problem directly
using a straightforward method and return the solution so obtained. Otherwise, divide the input data into two or
more disjoint subsets.
2. Conquer: Recursively solve the sub problems associated with the subsets.
3. Combine: Take the solutions to the sub problems and merge them into a solution to the original problem.
Using Divide-and-Conquer for Sorting

To sort a sequence S with n elements using the three divide-and-conquer steps, the merge-sort algorithm proceeds as
follows:
1. Divide: If S has zero or one element, return S immediately; it is already sorted. Otherwise (S has at least two elements),
remove all the elements from S and put them into two sequences, S1 and S2, each containing about half of the elements of
S; that is, S1 contains the first n/2 elements of S, and S2 contains the remaining n/2 elements.
2. Conquer: Recursively sort sequences S1 and S2.
3. Combine: Put back the elements into S by merging the sorted sequences S1 and S2 into a sorted sequence.
Merge-Sort
• We can visualize an execution of the merge-sort algorithm bymeans of a binary tree T, called the merge-sort tree.
• Each node of T represents a recursive invocation (or call) of the merge-sort algorithm.
• We associate with each node v of T the sequence S that is processed by the invocation associated with v.
• The children of node v are associated with the recursive calls that process the subsequences S1 and S2 of S.
• The external nodes of T are associated with individual elements of S, corresponding to instances of the algorithm that
make no recursive calls.

• Figure 12.1 summarizes an execution of the merge-sort algorithm by showing the input and output sequences processed at
each node of the merge-sort tree.
• The step-by-step evolution of the merge-sort tree is shown in Figures 12.2 through 12.4.
• This algorithm visualization in terms of the merge-sort tree helps us analyze the running time of the merge-sort algorithm.
• In particular, since the size of the input sequence roughly halves at each recursive call of merge-sort, the height of the
merge-sort tree is about logn (recall that the base of log is 2 if omitted).
Figure 12.1: Merge-sort tree T for an execution of the merge-sort algorithm on
a sequence with 8 elements: (a) input sequences processed at each node of T;
(b) output sequences generated at each node of T.
Figure 12.2: Visualization of an execution of merge-sort. Each node of the tree represents a recursive call of merge-sort.
The nodes drawn with dashed lines represent calls that have not been made yet. The node drawn with thick lines represents
the current call. The empty nodes drawn with thin lines represent completed calls. The remaining nodes (drawn with thin
lines and not empty) represent calls that are waiting for a child invocation to return. (Continues in Figure 12.3.)
Array-Based Implementation of Merge-Sort
• We begin by focusing on the case when a sequence of items is represented as an (array-based) Python list.
• The merge function (Code Fragment 12.1) is responsible for the subtask of merging two previously sorted sequences,
S1 and S2, with the output copied into S.
• We copy one element during each pass of the while loop, conditionally determining whether the next element should be
taken from S1 or S2.
• The divide-and-conquer merge-sort algorithm is given in Code Fragment 12.2.
• We illustrate a step of the merge process in Figure 12.5.
• During the process, index i represents the number of elements of S1 that have been copied to S, while index j represents
the number of elements of S2 that have been copied to S.
• Assuming S1 and S2 both have at least one uncopied element, we copy the smaller of the two elements being
considered.
• Since i+ j objects have been previously copied, the next element is placed in S[i+ j].
• (For example, when i+ j is 0, the next element is copied to S[0]).
• If we reach the end of one of the sequences, we must copy the next element from the other.

You might also like