Skip to content

Commit a6b94df

Browse files
committed
Binary Search
1 parent a1ad2b0 commit a6b94df

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

Searching/BinarySearch.ipynb

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 4,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"name": "stdout",
10+
"output_type": "stream",
11+
"text": [
12+
"Element is present at index 3\n"
13+
]
14+
}
15+
],
16+
"source": [
17+
"def binarySearch(arr, l, r, x): \n",
18+
" \n",
19+
" while l <= r: \n",
20+
" \n",
21+
" mid = l + (r - l) // 2; \n",
22+
" \n",
23+
" if arr[mid] == x: \n",
24+
" return mid \n",
25+
" elif arr[mid] < x: \n",
26+
" l = mid + 1\n",
27+
" else: \n",
28+
" r = mid - 1\n",
29+
" \n",
30+
" return -1\n",
31+
" \n",
32+
"arr = [ 2, 3, 4, 10, 40 ] \n",
33+
"x = 10\n",
34+
" \n",
35+
"result = binarySearch(arr, 0, len(arr)-1, x) \n",
36+
" \n",
37+
"if result != -1: \n",
38+
" print (\"Element is present at index % d\" % result) \n",
39+
"else: \n",
40+
" print (\"Element is not present in array\") "
41+
]
42+
}
43+
],
44+
"metadata": {
45+
"kernelspec": {
46+
"display_name": "Python 3",
47+
"language": "python",
48+
"name": "python3"
49+
},
50+
"language_info": {
51+
"codemirror_mode": {
52+
"name": "ipython",
53+
"version": 3
54+
},
55+
"file_extension": ".py",
56+
"mimetype": "text/x-python",
57+
"name": "python",
58+
"nbconvert_exporter": "python",
59+
"pygments_lexer": "ipython3",
60+
"version": "3.7.4"
61+
}
62+
},
63+
"nbformat": 4,
64+
"nbformat_minor": 4
65+
}

0 commit comments

Comments
 (0)