Course Title: Fundamentals of Algorithms (CS502)

 

Lecture No. 8: Analysis of Merge Sort

 

 

Introduction

Merge Algorithm

Analysis of Merge Sort

 

 

     Introduction

 

Merging: All that is left is to describe the procedure that merges two sorted lists. Merge (A, p, q, r) assumes that the left sub-array, A [p : q], and the right sub-array, A [q + 1 : r], have already been sorted. We merge these two sub-arrays by copying the elements to a temporary working array called B. For convenience, we will assume that the array B has the same index range A, that is, B [p : r]. (One nice thing about pseudo-code, is that we can make these assumptions, and leave them up to the programmer to figure out how to implement it.) We have to indices i and j, that point to the current elements of each sub-array. We move the smaller element into the next position of B (indicated by index k) and then increment the corresponding index (either i or j). When we run out of elements in one array, then we just copy the rest of the other array into B. Finally, we copy the entire contents of B back into A. (The use of the temporary array is a bit unpleasant, but this is impossible to overcome entirely. It is one of the shortcomings of Merge Sort, compared to some of the other efficient sorting algorithms.)

 

 

 

NEXT