Presentation is loading. Please wait.

Presentation is loading. Please wait.

Runtime of MergeSort CSC 172 SPRING 2009 LECTURE 17 b.

Similar presentations


Presentation on theme: "Runtime of MergeSort CSC 172 SPRING 2009 LECTURE 17 b."— Presentation transcript:

1 Runtime of MergeSort CSC 172 SPRING 2009 LECTURE 17 b

2 What is the RR for split? //O(1) //O(1) //O(1) //O(1) //O(1) //O(1)
public static Node split(Node head){ Node secondNode; if (head == null) return null; else if (head.getNext() == null) return null; else { secondNode = head.getNext(); head.setNext(secondNode.getNext()); secondNode.setNext(split(secondNode.getNext()); return secondNode; } //O(1) //O(1) //O(1) //O(1) //O(1) //O(1)

3 What is the RR for split? //O(1) //O(1) //O(1) //O(1) //O(1) //O(1)
public static Node split(Node head){ Node secondNode; if (head == null) return null; else if (head.getNext() == null) return null; else { secondNode = head.getNext(); head.setNext(secondNode.getNext()); secondNode.setNext(split(secondNode.getNext()); return secondNode; } //O(1) //O(1) //O(1) //O(1) //O(1) //O(1) //O(1) + T(n-1)

4 What is the RR for Merge? public static Node merge(Node list1, Node list2){ if (list1 == null) return list2; else if (list2 == null) return list1; else if (list1.getData.compareTo(list2.getData()) < 1) { list1.setNext(merge(list1.getNext(),list2); return list1; } else { list2.setNext(merge(list1,list2.getNext()); return list2; }

5 What is the RR for MergeSort?
public static Node mergeSort(Node list){ Node secondList; if (list == null) return null; else if (list.getNext() == null) return list; else { secondList = split(list); return merge(mergeSort(list),mergeSort(secondList)); } //O(1) //O(1) //O(1) //O(n)

6 What is the RR for MergeSort?
public static Node mergeSort(Node list){ Node secondList; if (list == null) return null; else if (list.getNext() == null) return list; else { secondList = split(list); return merge(mergeSort(list),mergeSort(secondList)); } //O(1) //O(1) //O(1) //O(n) What is the length of list? secondList? length(list) == length(secondList) = n/2

7 What is the RR for MergeSort?
public static Node mergeSort(Node list){ Node secondList; if (list == null) return null; else if (list.getNext() == null) return list; else { secondList = split(list); return merge(mergeSort(list),mergeSort(secondList)); } //O(1) //O(1) //O(1) //O(n) //O(n) What is the length of list? secondList? length(list) == length(secondList) = n/2 So, what is the cost of the call to merge?

8 What is the RR for MergeSort?
public static Node mergeSort(Node list){ Node secondList; if (list == null) return null; else if (list.getNext() == null) return list; else { secondList = split(list); return merge(mergeSort(list),mergeSort(secondList)); } //O(1) //O(1) //O(1) //O(n) //O(n) If the cost of mergeSort is TmergeSort(n): What is the cost of mergeSort is TmergeSort(list)? TmergeSort(secondlist)?

9 What is the RR for MergeSort?
public static Node mergeSort(Node list){ Node secondList; if (list == null) return null; else if (list.getNext() == null) return list; else { secondList = split(list); return merge(mergeSort(list),mergeSort(secondList) ); } //O(1) //O(1) //O(1) //O(n) //O(n) Tmergesort(n) = O(1)+O(n)+2Tmergesort(n/2)

10 What is the RR for MergeSort?

11 Recurrence Relation for MergeSort
Basis n == 0 TmergeSort(0) = O(1) Induction TmergeSort(n) = O(n) + 2TmergeSort(n/2)

12 Replace O(1)s by concrete constants
TmergeSort(1) = a TmergeSort(n) = bn + 2TmergeSort(n/2) TmergeSort(n)/n = b + TmergeSort(n/2)/(n/2) TmergeSort(n/2)/(n/2) = b + TmergeSort(n/4)/(n/4) TmergeSort(n/4)/(n/4) = b + TmergeSort(n/8)/(n/8) TmergeSort(n/8)/(n/8) = b + TmergeSort(n/16)/(n/16) TmergeSort(2)/(2) = b + TmergeSort(1)/(1) How many calls?

13 How many calls? log2n calls
TmergeSort(n)/n = blog2n + TmergeSort(1)/(1) TmergeSort(n) = nblog2n + na TmergeSort(n) = O(nlog2n)


Download ppt "Runtime of MergeSort CSC 172 SPRING 2009 LECTURE 17 b."

Similar presentations


Ads by Google