Skip to content

Instantly share code, notes, and snippets.

@uzl
Last active September 20, 2016 16:57
Show Gist options
  • Save uzl/ebe2363a43f3083a1b8bc6a0ef64ed52 to your computer and use it in GitHub Desktop.
Save uzl/ebe2363a43f3083a1b8bc6a0ef64ed52 to your computer and use it in GitHub Desktop.
Documents/research/Algorithm/Untitled.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "## Binary Search"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Python code: "
},
{
"metadata": {
"collapsed": true,
"trusted": true,
"ExecuteTime": {
"start_time": "2016-09-20T22:45:14.962000",
"end_time": "2016-09-20T22:45:14.970000"
}
},
"cell_type": "code",
"source": "# Iterative Binary Search Function\n# It returns location of x in given array arr if present,\n# else returns -1\ndef binarySearch(arr, l, r, x):\n \n while l <= r:\n \n mid = l + (r - l)/2;\n \n # Check if x is present at mid\n if arr[mid] == x:\n return mid\n \n # If x is greater, ignore left half\n elif arr[mid] < x:\n l = mid + 1\n \n # If x is smaller, ignore right half\n else:\n r = mid - 1\n \n # If we reach here, then the element was not present\n return -1",
"execution_count": 2,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2016-09-20T22:45:32.890000",
"end_time": "2016-09-20T22:45:32.896000"
},
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "# Test array\narr = [ 2, 3, 4, 10, 40 ]\nx = 10\n \n# Function call\nresult = binarySearch(arr, 0, len(arr)-1, x)\n \nif result != -1:\n print \"Element is present at index %d\" % result\nelse:\n print \"Element is not present in array\"",
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": "Element is present at index 3\n",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "<hr>\nC/c++ code"
},
{
"metadata": {
"trusted": true,
"collapsed": true
},
"cell_type": "code",
"source": "#include <stdio.h>\n \n// A iterative binary search function. It returns location of x in\n// given array arr[l..r] if present, otherwise -1\nint binarySearch(int arr[], int l, int r, int x)\n{\n while (l <= r)\n {\n int m = l + (r-l)/2;\n \n // Check if x is present at mid\n if (arr[m] == x) \n return m; \n \n // If x greater, ignore left half \n if (arr[m] < x) \n l = m + 1; \n \n // If x is smaller, ignore right half \n else\n r = m - 1; \n }\n \n // if we reach here, then element was not present\n return -1; \n}",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "Python [Root]",
"display_name": "Python [Root]",
"language": "python"
},
"latex_envs": {
"current_citInitial": 1,
"eqLabelWithNumbers": true,
"cite_by": "apalike",
"bibliofile": "biblio.bib",
"eqNumInitial": 0
},
"language_info": {
"mimetype": "text/x-python",
"nbconvert_exporter": "python",
"name": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12",
"file_extension": ".py",
"codemirror_mode": {
"version": 2,
"name": "ipython"
}
},
"anaconda-cloud": {},
"gist": {
"id": "ebe2363a43f3083a1b8bc6a0ef64ed52",
"data": {
"description": "Documents/research/Algorithm/Untitled.ipynb",
"public": true
}
},
"_draft": {
"nbviewer_url": "https://gist.github.com/ebe2363a43f3083a1b8bc6a0ef64ed52"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment