![]() ![]() So, the number of swaps is on the order of N time complexity. If we have an array with exactly 16 elements, there are at most 8 swaps we could make. So, how many swaps would that be? As it turns out, a good estimate would be $N / 2$ times. If each of those pairs were reversed, we’d end up doing that many swaps. If we had an input that caused each of the base cases with exactly two elements to swap them, that would be a few more steps than any other input. The only real impact that the input would have is on the number of swaps made by merge sort. What would that look like? Put another way, would the values or the ordering of those values change anything about how merge sort operates? The first thing we should do is consider the worst-case input for merge sort. Analyzing a recursive algorithm requires quite a bit of math and understanding to do it properly, but we can get a pretty close answer using a bit of intuition about what it does.įor starters, let’s consider a diagram that shows all of the different recursive calls made by merge sort, as shown below. Now that we’ve reviewed the pseudocode for the merge sort algorithm, let’s see if we can analyze the time it takes to complete. This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Programming by Contract and Introduction to Performance ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |