Skip to content

Commit 9af565d

Browse files
committed
simplify binary search
1 parent 2f603c8 commit 9af565d

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

ipython_nbs/search/binary_search.ipynb

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
"output_type": "stream",
1111
"text": [
1212
"Sebastian Raschka \n",
13-
"last updated: 2017-07-25 \n",
13+
"last updated: 2017-08-14 \n",
1414
"\n",
1515
"CPython 3.6.1\n",
16-
"IPython 6.0.0\n"
16+
"IPython 6.1.0\n"
1717
]
1818
}
1919
],
@@ -47,6 +47,13 @@
4747
"![](images/binary_search/ex-1-2.png)"
4848
]
4949
},
50+
{
51+
"cell_type": "markdown",
52+
"metadata": {},
53+
"source": [
54+
"## Binary Search Implementation"
55+
]
56+
},
5057
{
5158
"cell_type": "code",
5259
"execution_count": 2,
@@ -61,7 +68,7 @@
6168
" max_idx = len(array)\n",
6269
" \n",
6370
" while min_idx < max_idx:\n",
64-
" middle_idx = min_idx + (max_idx - min_idx) // 2\n",
71+
" middle_idx = (min_idx + max_idx) // 2\n",
6572
"\n",
6673
" if array[middle_idx] == value:\n",
6774
" return middle_idx\n",
@@ -112,7 +119,7 @@
112119
],
113120
"source": [
114121
"binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n",
115-
" value=2)"
122+
" value=2)"
116123
]
117124
},
118125
{
@@ -160,9 +167,7 @@
160167
{
161168
"cell_type": "code",
162169
"execution_count": 7,
163-
"metadata": {
164-
"collapsed": true
165-
},
170+
"metadata": {},
166171
"outputs": [],
167172
"source": [
168173
"binary_search(array=[1, 2, 4, 7, 8, 10, 11],\n",

0 commit comments

Comments
 (0)