Data Structure & Algorithm Lecture 4 – Merge Sort & Divide and Conquer JJCAO.

16
Data Structure & Algorithm Lecture 4 – Merge Sort & Divide and Conquer JJCAO

Transcript of Data Structure & Algorithm Lecture 4 – Merge Sort & Divide and Conquer JJCAO.

Page 1: Data Structure & Algorithm Lecture 4 – Merge Sort & Divide and Conquer JJCAO.

Data Structure & Algorithm

Lecture 4 – Merge Sort & Divide and Conquer

JJCAO

Page 2: Data Structure & Algorithm Lecture 4 – Merge Sort & Divide and Conquer JJCAO.

2

Recitation - Asymptotic Notations

Page 3: Data Structure & Algorithm Lecture 4 – Merge Sort & Divide and Conquer JJCAO.

3

Recitation - Example

Total complexity:

Page 4: Data Structure & Algorithm Lecture 4 – Merge Sort & Divide and Conquer JJCAO.

4

The Sorting Problem

• Example:

• Input: A sequence of n numbers

• Output: A permutation (reordering) of the input sequence such that

Page 5: Data Structure & Algorithm Lecture 4 – Merge Sort & Divide and Conquer JJCAO.

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

Page 6: Data Structure & Algorithm Lecture 4 – Merge Sort & Divide and Conquer JJCAO.

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

Page 7: Data Structure & Algorithm Lecture 4 – Merge Sort & Divide and Conquer JJCAO.

7

Divide and Conquer

Page 8: Data Structure & Algorithm Lecture 4 – Merge Sort & Divide and Conquer JJCAO.

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

Page 9: Data Structure & Algorithm Lecture 4 – Merge Sort & Divide and Conquer JJCAO.

9

Mergesort(A[1, n]) Merge( MergeSort(A[1,n/2]), MergeSort(A[n/2+1,n]) )

Page 10: Data Structure & Algorithm Lecture 4 – Merge Sort & Divide and Conquer JJCAO.

10

Page 11: Data Structure & Algorithm Lecture 4 – Merge Sort & Divide and Conquer JJCAO.

11

Pseudo Code

Page 12: Data Structure & Algorithm Lecture 4 – Merge Sort & Divide and Conquer JJCAO.

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

Page 13: Data Structure & Algorithm Lecture 4 – Merge Sort & Divide and Conquer JJCAO.

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);

}

Page 14: Data Structure & Algorithm Lecture 4 – Merge Sort & Divide and Conquer JJCAO.

14

Running Time

• Divide: lgn• Conquer: 0 • Combine: n• O(n log n) time in the worst case

Page 15: Data Structure & Algorithm Lecture 4 – Merge Sort & Divide and Conquer JJCAO.

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

Page 16: Data Structure & Algorithm Lecture 4 – Merge Sort & Divide and Conquer JJCAO.

16

Homework 3

• Template Dynamic Array & Bubble Sort

• Deadline: 22:00, Sep. ?, 2011