|
214 | 214 | },
|
215 | 215 | {
|
216 | 216 | "cell_type": "code",
|
217 |
| - "execution_count": null, |
| 217 | + "execution_count": 27, |
218 | 218 | "metadata": {},
|
219 |
| - "outputs": [], |
| 219 | + "outputs": [ |
| 220 | + { |
| 221 | + "name": "stdout", |
| 222 | + "output_type": "stream", |
| 223 | + "text": [ |
| 224 | + "The value found at location : 5\n" |
| 225 | + ] |
| 226 | + } |
| 227 | + ], |
220 | 228 | "source": [
|
221 |
| - "" |
| 229 | + "\"\"\"\n", |
| 230 | + "Binary Search Algorith for 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))" |
222 | 262 | ]
|
223 | 263 | },
|
224 | 264 | {
|
225 | 265 | "cell_type": "code",
|
226 |
| - "execution_count": null, |
| 266 | + "execution_count": 7, |
227 | 267 | "metadata": {},
|
228 |
| - "outputs": [], |
229 |
| - "source": [] |
| 268 | + "outputs": [ |
| 269 | + { |
| 270 | + "name": "stdout", |
| 271 | + "output_type": "stream", |
| 272 | + "text": [ |
| 273 | + "The value found at location : 1\n" |
| 274 | + ] |
| 275 | + } |
| 276 | + ], |
| 277 | + "source": [ |
| 278 | + "\"\"\"\n", |
| 279 | + "Binary Search Algorith for Ascending ordered Array\n", |
| 280 | + "\"\"\"\n", |
| 281 | + "def binary_search_Asc(asc_array,start,end,target):\n", |
| 282 | + " \n", |
| 283 | + " middle = 0\n", |
| 284 | + " \n", |
| 285 | + " if(start <= end):\n", |
| 286 | + " \n", |
| 287 | + " if asc_array[start] == target:\n", |
| 288 | + " return start\n", |
| 289 | + " if asc_array[end] == target:\n", |
| 290 | + " return end\n", |
| 291 | + " \n", |
| 292 | + " middle = (start+end)//2\n", |
| 293 | + " #print(middle)\n", |
| 294 | + " \n", |
| 295 | + " if asc_array[middle] == target:\n", |
| 296 | + " return middle\n", |
| 297 | + " \n", |
| 298 | + " if (asc_array[middle] > target):\n", |
| 299 | + " return binary_search_Asc(asc_array,start,middle-1,target)\n", |
| 300 | + " \n", |
| 301 | + " if (asc_array[middle] < target):\n", |
| 302 | + " return binary_search_Asc(asc_array,middle+1,end,target)\n", |
| 303 | + " \n", |
| 304 | + " return -1\n", |
| 305 | + "\n", |
| 306 | + "des_array = [873, 870, 868, 865, 863, 860, 857, 854, 851, 848]\n", |
| 307 | + "asc_array = [848,851,854,857,860,863,865,868,870,873]\n", |
| 308 | + "#print(len(des_array))\n", |
| 309 | + "print('The value found at location :',binary_search_Asc(asc_array,0,len(asc_array)-1,851))" |
| 310 | + ] |
230 | 311 | },
|
231 | 312 | {
|
232 | 313 | "cell_type": "code",
|
|
266 | 347 | "name": "python",
|
267 | 348 | "nbconvert_exporter": "python",
|
268 | 349 | "pygments_lexer": "ipython3",
|
269 |
| - "version": "3.7.4" |
| 350 | + "version": "3.7.3" |
270 | 351 | }
|
271 | 352 | },
|
272 | 353 | "nbformat": 4,
|
|
0 commit comments