Skip to content

Commit

Permalink
add visual explanation
Browse files Browse the repository at this point in the history
  • Loading branch information
gitgik committed Nov 18, 2020
1 parent 2062941 commit 38606bf
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions recursion/min_max.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,28 @@
"min_max([1, 2, 3, 4])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For array of size N, we are breaking down the problem into two subproblems of size `N / 2` plus `2` comparisons. \n",
"\n",
"```python\n",
"# Recursively break down array\n",
"[4, 2, 7, 5, -1, 3, 6] \n",
"[[4, 2, 7, 5], [-1, 3, 6]]\n",
"[[[4, 2], [7, 5]], [[-1, 3], [6]]]\n",
"\n",
"# Base case: reorder so that smaller comes before larger\n",
"[[[2, 4], [5, 7]], [[-1, 3], [6, 6]]]\n",
"\n",
"# Merge to find min and max\n",
"[[min(2, 5), max(4, 7)], [min(-1, 6), max(3, 6)]]\n",
"[min(2, -1), max(7, 6)]\n",
"[-1, 7] \n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down

0 comments on commit 38606bf

Please sign in to comment.