Skip to content

Commit 9ffad85

Browse files
Added Binary search algorithm for descending array
1 parent 0105bfb commit 9ffad85

File tree

1 file changed

+46
-4
lines changed

1 file changed

+46
-4
lines changed

Python Basic Projects.ipynb

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,52 @@
214214
},
215215
{
216216
"cell_type": "code",
217-
"execution_count": null,
217+
"execution_count": 27,
218218
"metadata": {},
219-
"outputs": [],
220-
"source": []
219+
"outputs": [
220+
{
221+
"name": "stdout",
222+
"output_type": "stream",
223+
"text": [
224+
"The value found at location : 5\n"
225+
]
226+
}
227+
],
228+
"source": [
229+
"\"\"\"\n",
230+
"Binary Search Algorith for Ascending and Descending ordered Array\n",
231+
"\"\"\"\n",
232+
"\n",
233+
"def binary_search_desc(des_array,start,end,target):\n",
234+
" \n",
235+
" middle = 0\n",
236+
" \n",
237+
" if(start <= end):\n",
238+
" \n",
239+
" if des_array[start] == target:\n",
240+
" return start\n",
241+
" if des_array[end] == target:\n",
242+
" return end\n",
243+
" \n",
244+
" middle = (start+end)//2\n",
245+
" #print(middle)\n",
246+
" \n",
247+
" if des_array[middle] == target:\n",
248+
" return middle\n",
249+
" \n",
250+
" if (des_array[middle] > target):\n",
251+
" return binary_search_desc(des_array,middle+1,end,target)\n",
252+
" \n",
253+
" if (des_array[middle] < target):\n",
254+
" return binary_search_desc(des_array,start,middle-1,target)\n",
255+
" \n",
256+
" return -1\n",
257+
"\n",
258+
"des_array = [873, 870, 868, 865, 863, 860, 857, 854, 851, 848]\n",
259+
"\n",
260+
"#print(len(des_array))\n",
261+
"print('The value found at location :',binary_search_desc(des_array,0,len(des_array)-1,860))"
262+
]
221263
},
222264
{
223265
"cell_type": "code",
@@ -264,7 +306,7 @@
264306
"name": "python",
265307
"nbconvert_exporter": "python",
266308
"pygments_lexer": "ipython3",
267-
"version": "3.7.4"
309+
"version": "3.7.3"
268310
}
269311
},
270312
"nbformat": 4,

0 commit comments

Comments
 (0)