Data Structure & Algorithm Lecture 4 – Merge Sort & Divide and Conquer JJCAO.
-
Upload
beverly-griffith -
Category
Documents
-
view
217 -
download
3
Transcript of Data Structure & Algorithm Lecture 4 – Merge Sort & Divide and Conquer JJCAO.
Data Structure & Algorithm
Lecture 4 – Merge Sort & Divide and Conquer
JJCAO
2
Recitation - Asymptotic Notations
3
Recitation - Example
Total complexity:
4
The Sorting Problem
• Example:
• Input: A sequence of n numbers
• Output: A permutation (reordering) of the input sequence such that
5
Sorting - The Data
• In practice, we usually sort records with keys and satellite data (non-key data)
• Sometimes, if the records are large, we sort pointers to the records
• For now, we ignore satellite data assume that we are dealing only with keys only i.e. focus on sorting algorithms
6
Divide and Conquer
• Divide – the problem into several (disjoint) sub-
problems
• Conquer – the sub-problems by solving them
recursively
• Combine – the solutions to the sub-problems into a
solution for the original problem
7
Divide and Conquer
8
Merge Sort
• Divide: Split the list into 2 equal sized sub-lists
• Conquer: Recursively sort each of these sub-lists
• Combine: Merge the two sorted sub-lists to make a single sorted list
9
Mergesort(A[1, n]) Merge( MergeSort(A[1,n/2]), MergeSort(A[n/2+1,n]) )
10
11
Pseudo Code
12
Merge(A,p,q,r) p<=q < r
1. Point to the beginning of each sub-array
2. choose the smallest of the two elements
3. move it to merged array4. and advance the
appropriate pointer
Running Time: cm for some constant c > 0 & m = r-p+1
13
merge(item_type s[], int low, int middle, int high){
int i; /* counter */queue buffer1, buffer2; /* buffers to hold elements for merging */init_queue(&buffer1);init_queue(&buffer2);
for (i=low; i<=middle; i++) enqueue(&buffer1,s[i]);for (i=middle+1; i<=high; i++) enqueue(&buffer2,s[i]);
i = low;while (!(empty_queue(&buffer1) || empty_queue(&buffer2))) {
if (headq(&buffer1) <= headq(&buffer2))s[i++] = dequeue(&buffer1);
elses[i++] = dequeue(&buffer2);
}
while (!empty_queue(&buffer1)) s[i++] = dequeue(&buffer1);while (!empty_queue(&buffer2)) s[i++] = dequeue(&buffer2);
}
14
Running Time
• Divide: lgn• Conquer: 0 • Combine: n• O(n log n) time in the worst case
15
Properties of Sorting Algorithms
• Not In place– a constant number of elements of the input array are
ever stored outside the array
• Comparison based– the only operation we can perform on keys is to
compare two keys– A non-comparison based sorting algorithm
• looks at values of individual elements• requires some prior knowledge
• Stable– elements with the same key keep their order
16
Homework 3
• Template Dynamic Array & Bubble Sort
• Deadline: 22:00, Sep. ?, 2011