Docsity

Worksheet 12: Merge Sort - A Fast Recursive Sorting Algorithms | CS 261, Assignments of Data Structures and Algorithms

Toggle sidebar
bg1
worksheet 12: Merge Sort: A Fast Recursive Algorithm Name:
An Active Learning Approach to Data Structures using C
1
Worksheet 12: Merge Sort โ€“ A Fast Recursive
Sorting Algorithm
In Preparation: Read the description of Merge Sort in Chapter 4.
In earlier lessons you were introduced to two classic sorting algorithms, selection sort and
insertion sort. While easy to explain, both are O(n2) worst case. Many sorting algorithms
can do better. In this lesson we will explore one of these.
As with many algorithms, the intuition for merge sort is best understood if you start in the
middle, and only after having seen the general situation do you examine the start and
finish as special cases. For merge sort the key insight is that two already sorted arrays can
be very rapidly merged together to form a new collection. All that is necessary is to walk
down each of the original lists in order, selecting the smallest element in turn:
When you reach the end of one of the arrays (you cannot, in general, predict which list
will end first), you must copy the remainder of the elements from the other.
Chapter 4 described how a sorting algorithm, Merge sort, could be constructed based on
these ideas. Merge sort can be written as follows:
void mergeSort (double data [ ], int n) {
double * temp = (double *) malloc (n * sizeof(double));
assert (temp != 0); /* make sure allocation worked */
mergeSortInternal (data, 0, n-1, temp);
pf2

Partial preview of the text

Download Worksheet 12: Merge Sort - A Fast Recursive Sorting Algorithms | CS 261 and more Assignments Data Structures and Algorithms in PDF only on Docsity!

worksheet 12: Merge Sort: A Fast Recursive Algorithm Name:

An Active Learning Approach to Data Structures using C 1

Worksheet 12: Merge Sort โ€“ A Fast Recursive

Sorting Algorithm

In Preparation : Read the description of Merge Sort in Chapter 4. In earlier lessons you were introduced to two classic sorting algorithms, selection sort and insertion sort. While easy to explain, both are O(n^2 ) worst case. Many sorting algorithms can do better. In this lesson we will explore one of these. As with many algorithms, the intuition for merge sort is best understood if you start in the middle, and only after having seen the general situation do you examine the start and finish as special cases. For merge sort the key insight is that two already sorted arrays can be very rapidly merged together to form a new collection. All that is necessary is to walk down each of the original lists in order, selecting the smallest element in turn: When you reach the end of one of the arrays (you cannot, in general, predict which list will end first), you must copy the remainder of the elements from the other. Chapter 4 described how a sorting algorithm, Merge sort, could be constructed based on these ideas. Merge sort can be written as follows: void mergeSort (double data [ ], int n) { double * temp = (double ) malloc (n * sizeof(double)); assert (temp != 0); / make sure allocation worked */ mergeSortInternal (data, 0, n-1, temp);

worksheet 12: Merge Sort: A Fast Recursive Algorithm Name:

An Active Learning Approach to Data Structures using C 2

} void mergeSortInternal (double data [ ], int low, int high, double temp [ ]) { int i, mid; if (low >= high) return; /* base case / mid = (low + high) / 2; mergeSortInternal(data, low, mid, temp); / first recursive call / mergeSortInternal(data, mid+1, high, temp); / second recursive call / merge(data, low, mid, high, temp); / merge into temp / for (i = low; i <= high; i++) / copy merged values back */ data[i] = temp[i]; } The only part left unfinished is to write the routine merge. Complete this routine in the space below: void merge (double [ ] data, int low, int mid, int high, double [ ] temp) { }