Data Str & Algorithm HW Solutions 52

Data Str & Algorithm HW Solutions 52 - }...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
52 Chap. 7 Internal Sorting Sort. Thus, the calls to Insertion Sort are equivalent to the leaf nodes of a full binary tree. We know from the Full Binary Tree Theorem that the number of leaf nodes in a full binary tree of n nodes is d n/ 2 e . Thus, if there are n calls to Mergesort, there will be d n/ 2 e calls to Insertion Sort. 7.18 LList<int> mergesort(LList<int> inlist) { LList<int> templist[2]; if (inlist.length() <= 1) return inlist; inlist.setStart(); int curr = 0; // Split the elements among two sublists lists while (!inlist.isEmpty()) { int item; inlist.remove(item); templist[curr].append(item); curr = (curr + 1) % 2;
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: } mergesort(templist[0]); mergesort(templist[1]); // Now, merge the lists together templist[0].setFirst(); templist[1].setFirst(); while (!templist[0].isEmpty() || !templist[1].isEmpty()) { if (templist[0].isEmpty()) { templist[1].remove(item); inlist.append(item); } else if (templist[1].isEmpty()) { templist[0].remove(item); inlist.append(item); } else if (templist[0].currValue() &lt; templist[1].currValue()) { item = templist[0].remove(); inlist.append(item); } else { item = templist[1].remove(); inlist.append(item); } } return inlist; }...
View Full Document

Ask a homework question - tutors are online