Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| courses:cs211:winter2018:journals:devlinn:chapter5 [2018/03/12 21:38] – devlinn | courses:cs211:winter2018:journals:devlinn:chapter5 [2018/03/12 21:51] (current) – devlinn | ||
|---|---|---|---|
| Line 10: | Line 10: | ||
| These types of algorithms need a base case. We then need to denote // | These types of algorithms need a base case. We then need to denote // | ||
| - | Looking at Mergesort as an example, we can see how the process of unrolling takes place: we must identify a pattern in the running time across the first few levels, sum the running times over all levels, and arrive at a total running time. We can see that the first level of Mergesort has a problem of size //n// and takes at most //cn// time plus all the time spent in remaining recursive calls, the next level has two problems of size //n///2, taking at most //cn///2, two times, so //cn/, etc. To identify a pattern, we see than at level //j//, there are now // | + | Looking at Mergesort as an example, we can see how the process of unrolling takes place: we must identify a pattern in the running time across the first few levels, sum the running times over all levels, and arrive at a total running time. We can see that the first level of Mergesort has a problem of size //n// and takes at most //cn// time plus all the time spent in remaining recursive calls, the next level has two problems of size //n///2, taking at most //cn///2, two times, so //cn//, etc. To identify a pattern, we see than at level //j//, there are now // |
| This section is a little bit challenging to understand for me. I have a hard time with the concept of calculating the recurrence relation. I guess more practice might help. I would rate my understanding at a solid 6 right now. | This section is a little bit challenging to understand for me. I have a hard time with the concept of calculating the recurrence relation. I guess more practice might help. I would rate my understanding at a solid 6 right now. | ||
| Line 25: | Line 25: | ||
| This algorithm has the following recurrence: // | This algorithm has the following recurrence: // | ||
| + | |||
| + | ===== 5.3 Counting Inversions ===== | ||
| + | We can apply the divide-and-conquer method to many problems, including counting the number of inversions, which compares rankings and looks at those which are "out of order" | ||
| + | Merge-and-Count(A, | ||
| + | Maintain a Current pointer into each list, initialized to point to the front elements | ||
| + | Maintain a variable Count for the number of inversions, initialized to 0 | ||
| + | While both lists nonempty: | ||
| + | Let ai and bj be the elements pointed to by the Current pointer | ||
| + | Append the smaller of these two to the output list | ||
| + | If bj is the smaller element then | ||
| + | Increment Count by the number of elements remaining in A | ||
| + | Endif | ||
| + | Advance the Current pointer in the list from which the smaller element was selected | ||
| + | Endwhile | ||
| + | Once one list is empty, append the remainder of the other list to the output | ||
| + | Return Count and the merged list | ||
| + | |||
| + | Merge-and-Count takes // | ||
