Data Structure Algorithm Lecture 4 Merge Sort Divide

  • Slides: 16
Download presentation
Data Structure & Algorithm Lecture 4 – Merge Sort & Divide and Conquer JJCAO

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

Recitation - Asymptotic Notations 2

Recitation - Asymptotic Notations 2

Recitation - Example 3

Recitation - Example 3

The Sorting Problem • 4

The Sorting Problem • 4

Sorting - The Data • In practice, we usually sort records with keys and

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

Divide and Conquer • Divide – the problem into several (disjoint) subproblems • Conquer

Divide and Conquer • Divide – the problem into several (disjoint) subproblems • Conquer – the sub-problems by solving them recursively • Combine – the solutions to the sub-problems into a solution for the original problem 6

Divide and Conquer 7

Divide and Conquer 7

Merge Sort • Divide: Split the list into 2 equal sized sub-lists • Conquer:

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

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

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

10

10

Pseudo Code 11

Pseudo Code 11

Merge(A, p, q, r) p<=q < r 1. Point to the beginning of each

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

merge(item_type s[], int low, int middle, int high) { int i; /* counter */

merge(item_type s[], int low, int middle, int high) { int i; /* counter */ queue buffer 1, buffer 2; /* buffers to hold elements for merging */ init_queue(&buffer 1); init_queue(&buffer 2); for (i=low; i<=middle; i++) enqueue(&buffer 1, s[i]); for (i=middle+1; i<=high; i++) enqueue(&buffer 2, s[i]); i = low; while (!(empty_queue(&buffer 1) || empty_queue(&buffer 2))) { if (headq(&buffer 1) <= headq(&buffer 2)) s[i++] = dequeue(&buffer 1); else s[i++] = dequeue(&buffer 2); } } while (!empty_queue(&buffer 1)) s[i++] = dequeue(&buffer 1); while (!empty_queue(&buffer 2)) s[i++] = dequeue(&buffer 2); 13

Running Time • • Divide: lgn Conquer: 0 Combine: n O(n log n) time

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

Properties of Sorting Algorithms • Not In place – a constant number of elements

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

Homework 3 • Template Dynamic Array & Bubble Sort • Deadline: 22: 00, Sep.

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