Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Data Structure & Algorithm Lecture 4 – Merge Sort & Divide and Conquer JJCAO."— Presentation transcript:

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

2 Recitation - Asymptotic Notations 2

3 Recitation - Example 3

4 The Sorting Problem 4

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 5

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 6

7 Divide and Conquer 7

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 8

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

10 10

11 Pseudo Code 11

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 array 4.and advance the appropriate pointer Running Time: cm for some constant c > 0 & m = r-p+1 12

13 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); else s[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 14

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 15

16 Homework 3 Template Dynamic Array & Bubble Sort Deadline: 22:00, Sep. ?, 2011 16


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

Similar presentations


Ads by Google