Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 190 additions & 0 deletions DAA Lab-2 .ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Binary Search using Single array"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Iterative Binary Search using array"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Given element is present at position 3\n"
]
}
],
"source": [
"def binary_search(arr, x): \n",
" low = 0\n",
" high = len(arr) - 1\n",
" mid = 0\n",
"\n",
" while low <= high: \n",
"\n",
" mid = (high + low) // 2\n",
"\n",
" \n",
" if arr[mid] < x: \n",
" low = mid + 1\n",
"\n",
" \n",
" elif arr[mid] > x: \n",
" high = mid - 1\n",
"\n",
" else: \n",
" return mid \n",
"\n",
" return -1\n",
" \n",
"arr = [ 5,6,7,8,9 ] \n",
"x = 7\n",
"#Calling the binary search function\n",
"result = binary_search(arr, x) \n",
"\n",
"if result != -1: \n",
" print(\"Given element is present at position\", str(result+1)) \n",
"else: \n",
"\tprint(\"Given element is not present in array\") \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Binary Search using Singly Linked List"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"7\n"
]
}
],
"source": [
"class Node: \n",
" def __init__(self, data): \n",
" self.data = data \n",
" self.next = None\n",
" self.prev = None\n",
" \n",
"def newNode(x):\n",
" temp = Node(0) \n",
" temp.data = x \n",
" temp.next = None\n",
" return temp \n",
"\n",
"def middle(start, last): \n",
" if (start == None): \n",
" return None\n",
" \n",
" slow = start \n",
" fast = start . next\n",
"\n",
" while (fast != last): \n",
" \n",
" fast = fast . next\n",
" if (fast != last): \n",
" \n",
" slow = slow . next\n",
" fast = fast . next\n",
" \n",
" return slow \n",
"\n",
"# Function for implementing the Binary \n",
"# Search on linked list \n",
"def binarySearch(head,value): \n",
"\n",
" start = head \n",
" last = None\n",
"\n",
" while True : \n",
" \n",
" # Find middle \n",
" mid = middle(start, last) \n",
"\n",
" # If middle is empty \n",
" if (mid == None): \n",
" return None\n",
"\n",
" # If value is present at middle \n",
" if (mid . data == value): \n",
" return mid.data \n",
"\n",
" # If value is more than mid \n",
" elif (mid . data < value): \n",
" start = mid . next\n",
"\n",
" # If the value is less than mid. \n",
" else: \n",
" last = mid \n",
"\n",
" if not (last == None or last != start): \n",
" break\n",
"\n",
" # value not present \n",
" return None\n",
"\n",
"# Driver Code \n",
"\n",
"head = newNode(1) \n",
"head.next = newNode(4) \n",
"head.next.next = newNode(7) \n",
"head.next.next.next = newNode(8) \n",
"head.next.next.next.next = newNode(9) \n",
"head.next.next.next.next.next = newNode(10) \n",
"value = 7\n",
"print(binarySearch(head, value) ) \n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
1 change: 1 addition & 0 deletions Lab programs/Lab 1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@