├── README.md ├── queue_LL_implemetation.ipynb ├── dynamic_arrays.ipynb ├── stacks_using_arrays.ipynb ├── Linked_List.ipynb ├── stacks_using_linkedlists.ipynb ├── hashing_linear_probing.ipynb ├── hashing_via_chaining.ipynb └── searching_sorting.ipynb /README.md: -------------------------------------------------------------------------------- 1 | # dsa-using-python 2 | codes related to my dsa course on python 3 | -------------------------------------------------------------------------------- /queue_LL_implemetation.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [] 7 | }, 8 | "kernelspec": { 9 | "name": "python3", 10 | "display_name": "Python 3" 11 | } 12 | }, 13 | "cells": [ 14 | { 15 | "cell_type": "code", 16 | "metadata": { 17 | "id": "PurBULUa-0Ud" 18 | }, 19 | "source": [ 20 | "class Node:\n", 21 | "\n", 22 | " def __init__(self,value):\n", 23 | "\n", 24 | " self.data = value\n", 25 | " self.next = None" 26 | ], 27 | "execution_count": null, 28 | "outputs": [] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "metadata": { 33 | "id": "crm3gdWj_n8i" 34 | }, 35 | "source": [ 36 | "class Queue:\n", 37 | "\n", 38 | " def __init__(self):\n", 39 | " self.front = None\n", 40 | " self.rear = None\n", 41 | "\n", 42 | " def enqueue(self,value):\n", 43 | "\n", 44 | " new_node = Node(value)\n", 45 | "\n", 46 | " if self.front == None:\n", 47 | " self.front = new_node\n", 48 | " self.rear = new_node\n", 49 | "\n", 50 | " else:\n", 51 | " self.rear.next = new_node\n", 52 | " self.rear = new_node\n", 53 | "\n", 54 | " def dequeue(self):\n", 55 | "\n", 56 | " if self.front == None:\n", 57 | " return \"Queue empty\"\n", 58 | " else:\n", 59 | " self.front = self.front.next\n", 60 | "\n", 61 | " def is_empty(self):\n", 62 | " return self.front == None\n", 63 | "\n", 64 | " def front_item(self):\n", 65 | " if (not self.is_empty()):\n", 66 | " return self.front.data\n", 67 | " else:\n", 68 | " return \"Empty queue\"\n", 69 | "\n", 70 | " def rear_item(self):\n", 71 | " if (not self.is_empty()):\n", 72 | " return self.rear.data\n", 73 | " else:\n", 74 | " return \"Empty queue\"\n", 75 | " \n", 76 | "\n", 77 | " def traverse(self):\n", 78 | "\n", 79 | " temp = self.front\n", 80 | "\n", 81 | " while temp is not None:\n", 82 | " print(temp.data,end=' ')\n", 83 | " temp = temp.next\n" 84 | ], 85 | "execution_count": null, 86 | "outputs": [] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "metadata": { 91 | "id": "It7a1H5kBfbF" 92 | }, 93 | "source": [ 94 | "q = Queue()" 95 | ], 96 | "execution_count": null, 97 | "outputs": [] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "metadata": { 102 | "id": "tsInv6hyBhUn" 103 | }, 104 | "source": [ 105 | "q.enqueue(3)\n", 106 | "q.enqueue(4)\n", 107 | "q.enqueue(5)\n", 108 | "q.enqueue(7)" 109 | ], 110 | "execution_count": null, 111 | "outputs": [] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "metadata": { 116 | "id": "ImvsCMX5Bm7S" 117 | }, 118 | "source": [ 119 | "q.traverse()" 120 | ], 121 | "execution_count": null, 122 | "outputs": [] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "metadata": { 127 | "colab": { 128 | "base_uri": "https://localhost:8080/", 129 | "height": 35 130 | }, 131 | "id": "cDKTXJyUBpD9", 132 | "outputId": "010edb40-1070-42ac-8763-6861ae825334" 133 | }, 134 | "source": [ 135 | "q.dequeue()" 136 | ], 137 | "execution_count": null, 138 | "outputs": [ 139 | { 140 | "output_type": "execute_result", 141 | "data": { 142 | "application/vnd.google.colaboratory.intrinsic+json": { 143 | "type": "string" 144 | }, 145 | "text/plain": [ 146 | "'Queue empty'" 147 | ] 148 | }, 149 | "metadata": { 150 | "tags": [] 151 | }, 152 | "execution_count": 36 153 | } 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "metadata": { 159 | "id": "w9ncP6MlB0KK" 160 | }, 161 | "source": [ 162 | "q.enqueue(8)" 163 | ], 164 | "execution_count": null, 165 | "outputs": [] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "metadata": { 170 | "colab": { 171 | "base_uri": "https://localhost:8080/" 172 | }, 173 | "id": "p_WrD96IB4oa", 174 | "outputId": "c08f14c5-330b-4012-c27a-7a482d867ecb" 175 | }, 176 | "source": [ 177 | "q.is_empty()" 178 | ], 179 | "execution_count": null, 180 | "outputs": [ 181 | { 182 | "output_type": "execute_result", 183 | "data": { 184 | "text/plain": [ 185 | "False" 186 | ] 187 | }, 188 | "metadata": { 189 | "tags": [] 190 | }, 191 | "execution_count": 48 192 | } 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "metadata": { 198 | "colab": { 199 | "base_uri": "https://localhost:8080/" 200 | }, 201 | "id": "3HN0CH3IEI3K", 202 | "outputId": "631baeda-fe47-4cc8-9b20-6206152e9bcb" 203 | }, 204 | "source": [ 205 | "q.front_item()" 206 | ], 207 | "execution_count": null, 208 | "outputs": [ 209 | { 210 | "output_type": "execute_result", 211 | "data": { 212 | "text/plain": [ 213 | "4" 214 | ] 215 | }, 216 | "metadata": { 217 | "tags": [] 218 | }, 219 | "execution_count": 53 220 | } 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "metadata": { 226 | "colab": { 227 | "base_uri": "https://localhost:8080/" 228 | }, 229 | "id": "WxhnIMn6ELpY", 230 | "outputId": "54dbd4a1-5aa5-4e4d-f20a-4fbb3d11714f" 231 | }, 232 | "source": [ 233 | "q.rear_item()" 234 | ], 235 | "execution_count": null, 236 | "outputs": [ 237 | { 238 | "output_type": "execute_result", 239 | "data": { 240 | "text/plain": [ 241 | "7" 242 | ] 243 | }, 244 | "metadata": { 245 | "tags": [] 246 | }, 247 | "execution_count": 52 248 | } 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "metadata": { 254 | "id": "yzG_VbWxEXcH" 255 | }, 256 | "source": [ 257 | "q.dequeue()" 258 | ], 259 | "execution_count": null, 260 | "outputs": [] 261 | }, 262 | { 263 | "cell_type": "code", 264 | "metadata": { 265 | "id": "q7wOxuqpEgoD" 266 | }, 267 | "source": [ 268 | "def check_balanced_parenthesis(s):\n", 269 | " stk = Stack()\n", 270 | " for item in s:\n", 271 | " if item == '(':\n", 272 | " stk.push(item)\n", 273 | " elif item == '{':\n", 274 | " stk.push(item)\n", 275 | " elif item == '[':\n", 276 | " stk.push(item)\n", 277 | " elif item == ')':\n", 278 | " if stk.peek() == '(':\n", 279 | " stk.pop()\n", 280 | " else:\n", 281 | " return False\n", 282 | " elif item == '}':\n", 283 | " if stk.peek() == '{':\n", 284 | " stk.pop()\n", 285 | " else:\n", 286 | " return False\n", 287 | " elif item == ']':\n", 288 | " if stk.peek() == '[':\n", 289 | " stk.pop()\n", 290 | " else:\n", 291 | " return False\n", 292 | " else:\n", 293 | " continue\n", 294 | " \n", 295 | " return stk.is_empty()\n", 296 | "\n", 297 | "s=\"{[(a+b) + (c+d)]}\"\n", 298 | "print(check_balanced_parenthesis(s))\n" 299 | ], 300 | "execution_count": null, 301 | "outputs": [] 302 | } 303 | ] 304 | } -------------------------------------------------------------------------------- /dynamic_arrays.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [] 7 | }, 8 | "kernelspec": { 9 | "name": "python3", 10 | "display_name": "Python 3" 11 | }, 12 | "language_info": { 13 | "name": "python" 14 | } 15 | }, 16 | "cells": [ 17 | { 18 | "cell_type": "code", 19 | "source": [ 20 | "import ctypes\n", 21 | "# to create C type ka array" 22 | ], 23 | "metadata": { 24 | "id": "QZ5u8mep3z4q" 25 | }, 26 | "execution_count": null, 27 | "outputs": [] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "source": [ 32 | "class MeraList:\n", 33 | "\n", 34 | " def __init__(self):\n", 35 | " self.size = 1\n", 36 | " self.n = 0\n", 37 | " # create a C type ka array with size -> self.size\n", 38 | " self.A = self.__make_array(self.size)\n", 39 | "\n", 40 | " def __len__(self):\n", 41 | " return self.n\n", 42 | "\n", 43 | " def append(self,item):\n", 44 | " # check if vacant\n", 45 | " if self.n == self.size:\n", 46 | " # array is full -> resize\n", 47 | " self.__resize(self.size*2)\n", 48 | "\n", 49 | " self.A[self.n] = item\n", 50 | " self.n = self.n + 1\n", 51 | "\n", 52 | " def pop(self):\n", 53 | " if self.n == 0:\n", 54 | " return 'Empty List'\n", 55 | " print(self.A[self.n-1])\n", 56 | " self.n = self.n - 1\n", 57 | "\n", 58 | " def clear(self):\n", 59 | " self.n = 0\n", 60 | " self.size = 1\n", 61 | "\n", 62 | " def find(self,item):\n", 63 | "\n", 64 | " for i in range(self.n):\n", 65 | " if self.A[i] == item:\n", 66 | " return i\n", 67 | " return 'ValueError - not in list'\n", 68 | "\n", 69 | " def insert(self,pos,item):\n", 70 | "\n", 71 | " if self.n == self.size:\n", 72 | " self.__resize(self.size*2)\n", 73 | "\n", 74 | " for i in range(self.n,pos,-1):\n", 75 | " self.A[i] = self.A[i-1]\n", 76 | "\n", 77 | " self.A[pos] = item\n", 78 | " self.n = self.n + 1\n", 79 | "\n", 80 | " def remove(self,item):\n", 81 | " # search and get pos\n", 82 | " pos = self.find(item)\n", 83 | " if type(pos) == int:\n", 84 | " # delete\n", 85 | " self.__delitem__(pos)\n", 86 | " else:\n", 87 | " return pos\n", 88 | "\n", 89 | " def __resize(self,new_capacity):\n", 90 | " # create a new array with new capacity\n", 91 | " B = self.__make_array(new_capacity)\n", 92 | " self.size = new_capacity\n", 93 | " # copy the content of old array to new one\n", 94 | " for i in range(self.n):\n", 95 | " B[i] = self.A[i]\n", 96 | " # reassign A\n", 97 | " self.A = B\n", 98 | "\n", 99 | " def __str__(self):\n", 100 | " result = ''\n", 101 | " for i in range(self.n):\n", 102 | " result = result + str(self.A[i]) + ','\n", 103 | "\n", 104 | " return '[' + result[:-1] + ']'\n", 105 | "\n", 106 | " def __getitem__(self,index):\n", 107 | "\n", 108 | " if 0<= index < self.n:\n", 109 | " return self.A[index]\n", 110 | " else:\n", 111 | " return 'IndexError'\n", 112 | "\n", 113 | " def __delitem__(self,pos):\n", 114 | " # delete pos wala item\n", 115 | " if 0<= pos < self.n:\n", 116 | " for i in range(pos,self.n-1):\n", 117 | " self.A[i] = self.A[i+1]\n", 118 | "\n", 119 | " self.n = self.n - 1\n", 120 | "\n", 121 | " def __make_array(self,capacity):\n", 122 | " # referential array(C type)\n", 123 | " return (capacity*ctypes.py_object)()" 124 | ], 125 | "metadata": { 126 | "id": "FJ_5iTUh4Jcv" 127 | }, 128 | "execution_count": null, 129 | "outputs": [] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "source": [ 134 | "L = MeraList()" 135 | ], 136 | "metadata": { 137 | "id": "0dr0QIug5WXt" 138 | }, 139 | "execution_count": null, 140 | "outputs": [] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "source": [ 145 | "L.append(1)\n", 146 | "L.append('hello')\n", 147 | "L.append(False)\n", 148 | "L.append(4.5)" 149 | ], 150 | "metadata": { 151 | "id": "kRiwtEgJ5Y5l" 152 | }, 153 | "execution_count": null, 154 | "outputs": [] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "source": [ 159 | "print(L)" 160 | ], 161 | "metadata": { 162 | "colab": { 163 | "base_uri": "https://localhost:8080/" 164 | }, 165 | "id": "JrdjNFWC8Wbn", 166 | "outputId": "170821bd-8d0f-4016-aa9d-f064cb270e64" 167 | }, 168 | "execution_count": null, 169 | "outputs": [ 170 | { 171 | "output_type": "stream", 172 | "name": "stdout", 173 | "text": [ 174 | "[1,False]\n" 175 | ] 176 | } 177 | ] 178 | }, 179 | { 180 | "cell_type": "code", 181 | "source": [ 182 | "L.remove(4.5)" 183 | ], 184 | "metadata": { 185 | "colab": { 186 | "base_uri": "https://localhost:8080/", 187 | "height": 35 188 | }, 189 | "id": "WbFSLfpy8tK-", 190 | "outputId": "75daf089-3d0c-4d19-8f49-7930d70e06af" 191 | }, 192 | "execution_count": null, 193 | "outputs": [ 194 | { 195 | "output_type": "execute_result", 196 | "data": { 197 | "text/plain": [ 198 | "'ValueError - not in list'" 199 | ], 200 | "application/vnd.google.colaboratory.intrinsic+json": { 201 | "type": "string" 202 | } 203 | }, 204 | "metadata": {}, 205 | "execution_count": 175 206 | } 207 | ] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "source": [ 212 | "L = [10,20,30]\n", 213 | "L" 214 | ], 215 | "metadata": { 216 | "colab": { 217 | "base_uri": "https://localhost:8080/" 218 | }, 219 | "id": "eFiGLQqB5emG", 220 | "outputId": "e3000a0e-d9d7-4018-f312-84ad7288287f" 221 | }, 222 | "execution_count": null, 223 | "outputs": [ 224 | { 225 | "output_type": "execute_result", 226 | "data": { 227 | "text/plain": [ 228 | "[10, 20, 30]" 229 | ] 230 | }, 231 | "metadata": {}, 232 | "execution_count": 161 233 | } 234 | ] 235 | }, 236 | { 237 | "cell_type": "code", 238 | "source": [ 239 | "L.remove(200)" 240 | ], 241 | "metadata": { 242 | "colab": { 243 | "base_uri": "https://localhost:8080/", 244 | "height": 168 245 | }, 246 | "id": "PdLotNNm5vo7", 247 | "outputId": "51db0c71-0370-4d25-cdac-955b50c6ff0f" 248 | }, 249 | "execution_count": null, 250 | "outputs": [ 251 | { 252 | "output_type": "error", 253 | "ename": "ValueError", 254 | "evalue": "ignored", 255 | "traceback": [ 256 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 257 | "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", 258 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mL\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m200\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 259 | "\u001b[0;31mValueError\u001b[0m: list.remove(x): x not in list" 260 | ] 261 | } 262 | ] 263 | }, 264 | { 265 | "cell_type": "code", 266 | "source": [ 267 | "L" 268 | ], 269 | "metadata": { 270 | "colab": { 271 | "base_uri": "https://localhost:8080/" 272 | }, 273 | "id": "AO7K_n7-5wxT", 274 | "outputId": "737f3c40-e31a-4b0c-9920-b369e77b2625" 275 | }, 276 | "execution_count": null, 277 | "outputs": [ 278 | { 279 | "output_type": "execute_result", 280 | "data": { 281 | "text/plain": [ 282 | "[10]" 283 | ] 284 | }, 285 | "metadata": {}, 286 | "execution_count": 165 287 | } 288 | ] 289 | }, 290 | { 291 | "cell_type": "code", 292 | "source": [], 293 | "metadata": { 294 | "id": "JtTWvkXL-6za" 295 | }, 296 | "execution_count": null, 297 | "outputs": [] 298 | } 299 | ] 300 | } -------------------------------------------------------------------------------- /stacks_using_arrays.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [] 7 | }, 8 | "kernelspec": { 9 | "name": "python3", 10 | "display_name": "Python 3" 11 | } 12 | }, 13 | "cells": [ 14 | { 15 | "cell_type": "code", 16 | "metadata": { 17 | "id": "RjIMZZX3f1Ch" 18 | }, 19 | "source": [ 20 | "L = [1,2,3,4]" 21 | ], 22 | "execution_count": null, 23 | "outputs": [] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "metadata": { 28 | "colab": { 29 | "base_uri": "https://localhost:8080/" 30 | }, 31 | "id": "i8VazNGWf-YN", 32 | "outputId": "9c9d45a5-9b4e-4929-94ea-82151e73cb6c" 33 | }, 34 | "source": [ 35 | "L" 36 | ], 37 | "execution_count": null, 38 | "outputs": [ 39 | { 40 | "output_type": "execute_result", 41 | "data": { 42 | "text/plain": [ 43 | "[1, 2, 3, 4]" 44 | ] 45 | }, 46 | "metadata": { 47 | "tags": [] 48 | }, 49 | "execution_count": 2 50 | } 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "metadata": { 56 | "id": "UiIpVRO0f-0j" 57 | }, 58 | "source": [ 59 | "L.append(5)" 60 | ], 61 | "execution_count": null, 62 | "outputs": [] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "metadata": { 67 | "colab": { 68 | "base_uri": "https://localhost:8080/" 69 | }, 70 | "id": "voe1YP-NgCDv", 71 | "outputId": "7dd1f17d-5007-465a-ff2e-445d210fafa6" 72 | }, 73 | "source": [ 74 | "L" 75 | ], 76 | "execution_count": null, 77 | "outputs": [ 78 | { 79 | "output_type": "execute_result", 80 | "data": { 81 | "text/plain": [ 82 | "[1, 2, 3, 4, 5]" 83 | ] 84 | }, 85 | "metadata": { 86 | "tags": [] 87 | }, 88 | "execution_count": 4 89 | } 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "metadata": { 95 | "colab": { 96 | "base_uri": "https://localhost:8080/" 97 | }, 98 | "id": "vstomh3wgCXe", 99 | "outputId": "c1bddf17-fd05-430a-b22a-c059b0a880b4" 100 | }, 101 | "source": [ 102 | "L.pop()" 103 | ], 104 | "execution_count": null, 105 | "outputs": [ 106 | { 107 | "output_type": "execute_result", 108 | "data": { 109 | "text/plain": [ 110 | "5" 111 | ] 112 | }, 113 | "metadata": { 114 | "tags": [] 115 | }, 116 | "execution_count": 5 117 | } 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "metadata": { 123 | "colab": { 124 | "base_uri": "https://localhost:8080/" 125 | }, 126 | "id": "yx7YKkDqgEz2", 127 | "outputId": "0648787d-4c78-4374-ea44-69bd3c902281" 128 | }, 129 | "source": [ 130 | "L" 131 | ], 132 | "execution_count": null, 133 | "outputs": [ 134 | { 135 | "output_type": "execute_result", 136 | "data": { 137 | "text/plain": [ 138 | "[1, 2, 3, 4]" 139 | ] 140 | }, 141 | "metadata": { 142 | "tags": [] 143 | }, 144 | "execution_count": 6 145 | } 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "metadata": { 151 | "colab": { 152 | "base_uri": "https://localhost:8080/" 153 | }, 154 | "id": "KYmGVExqgF3j", 155 | "outputId": "644c0591-e1fb-4748-f4e5-b1186116924f" 156 | }, 157 | "source": [ 158 | "L[-1]" 159 | ], 160 | "execution_count": null, 161 | "outputs": [ 162 | { 163 | "output_type": "execute_result", 164 | "data": { 165 | "text/plain": [ 166 | "4" 167 | ] 168 | }, 169 | "metadata": { 170 | "tags": [] 171 | }, 172 | "execution_count": 7 173 | } 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "metadata": { 179 | "id": "a5Z--5QmgU2y" 180 | }, 181 | "source": [ 182 | "class Stack:\n", 183 | "\n", 184 | " def __init__(self,size):\n", 185 | " self.size = size\n", 186 | " self.__stack = [None] * self.size\n", 187 | " self.top = -1\n", 188 | "\n", 189 | " def push(self,value):\n", 190 | "\n", 191 | " if self.top == self.size - 1:\n", 192 | " return \"Overflow\"\n", 193 | " else:\n", 194 | " self.top+=1\n", 195 | " self.stack[self.top] = value\n", 196 | "\n", 197 | " def pop(self):\n", 198 | "\n", 199 | " if self.top == -1:\n", 200 | " return \"Empty\"\n", 201 | " else:\n", 202 | " data = self.stack[self.top]\n", 203 | " self.top-=1\n", 204 | " print(data)\n", 205 | "\n", 206 | " def traverse(self):\n", 207 | "\n", 208 | " for i in range(self.top + 1):\n", 209 | " print(self.stack[i],end=' ')\n", 210 | "\n", 211 | "\n" 212 | ], 213 | "execution_count": null, 214 | "outputs": [] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "metadata": { 219 | "id": "NNTP4kXygv2d" 220 | }, 221 | "source": [ 222 | "s = Stack(3)" 223 | ], 224 | "execution_count": null, 225 | "outputs": [] 226 | }, 227 | { 228 | "cell_type": "code", 229 | "metadata": { 230 | "colab": { 231 | "base_uri": "https://localhost:8080/" 232 | }, 233 | "id": "MWh7McaggyFV", 234 | "outputId": "66724a9c-1414-43e2-ec69-1e54733c9023" 235 | }, 236 | "source": [ 237 | "s.stack" 238 | ], 239 | "execution_count": null, 240 | "outputs": [ 241 | { 242 | "output_type": "execute_result", 243 | "data": { 244 | "text/plain": [ 245 | "[4, 5, 6]" 246 | ] 247 | }, 248 | "metadata": { 249 | "tags": [] 250 | }, 251 | "execution_count": 34 252 | } 253 | ] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "metadata": { 258 | "id": "jz9odQBpgz3r" 259 | }, 260 | "source": [ 261 | "s.push(4)" 262 | ], 263 | "execution_count": null, 264 | "outputs": [] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "metadata": { 269 | "id": "MGwhjaCQhXQV" 270 | }, 271 | "source": [ 272 | "s.push(5)" 273 | ], 274 | "execution_count": null, 275 | "outputs": [] 276 | }, 277 | { 278 | "cell_type": "code", 279 | "metadata": { 280 | "id": "RxEcP-mphf1g" 281 | }, 282 | "source": [ 283 | "s.push(6)" 284 | ], 285 | "execution_count": null, 286 | "outputs": [] 287 | }, 288 | { 289 | "cell_type": "code", 290 | "metadata": { 291 | "colab": { 292 | "base_uri": "https://localhost:8080/" 293 | }, 294 | "id": "vQtQ-GNLhl7x", 295 | "outputId": "0a387076-6bcf-42d6-ba44-70b64aedb1a0" 296 | }, 297 | "source": [ 298 | "s.pop()" 299 | ], 300 | "execution_count": null, 301 | "outputs": [ 302 | { 303 | "output_type": "stream", 304 | "text": [ 305 | "6\n" 306 | ], 307 | "name": "stdout" 308 | } 309 | ] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "metadata": { 314 | "colab": { 315 | "base_uri": "https://localhost:8080/" 316 | }, 317 | "id": "WEtUHwWqhr4R", 318 | "outputId": "f4494beb-1080-4cb9-b36b-1d9789afeac6" 319 | }, 320 | "source": [ 321 | "s.pop()" 322 | ], 323 | "execution_count": null, 324 | "outputs": [ 325 | { 326 | "output_type": "stream", 327 | "text": [ 328 | "5\n" 329 | ], 330 | "name": "stdout" 331 | } 332 | ] 333 | }, 334 | { 335 | "cell_type": "code", 336 | "metadata": { 337 | "colab": { 338 | "base_uri": "https://localhost:8080/" 339 | }, 340 | "id": "qnel184ziXlN", 341 | "outputId": "c4aa5bb0-1421-4490-b485-bb6e7260791e" 342 | }, 343 | "source": [ 344 | "s.pop()" 345 | ], 346 | "execution_count": null, 347 | "outputs": [ 348 | { 349 | "output_type": "stream", 350 | "text": [ 351 | "4\n" 352 | ], 353 | "name": "stdout" 354 | } 355 | ] 356 | }, 357 | { 358 | "cell_type": "code", 359 | "metadata": { 360 | "colab": { 361 | "base_uri": "https://localhost:8080/", 362 | "height": 35 363 | }, 364 | "id": "JkUtaiZdiacd", 365 | "outputId": "8f9cbb9d-870c-4383-acfe-736bfde5ceba" 366 | }, 367 | "source": [ 368 | "s.pop()" 369 | ], 370 | "execution_count": null, 371 | "outputs": [ 372 | { 373 | "output_type": "execute_result", 374 | "data": { 375 | "application/vnd.google.colaboratory.intrinsic+json": { 376 | "type": "string" 377 | }, 378 | "text/plain": [ 379 | "'Empty'" 380 | ] 381 | }, 382 | "metadata": { 383 | "tags": [] 384 | }, 385 | "execution_count": 54 386 | } 387 | ] 388 | }, 389 | { 390 | "cell_type": "code", 391 | "metadata": { 392 | "colab": { 393 | "base_uri": "https://localhost:8080/" 394 | }, 395 | "id": "eJymQRFoidPA", 396 | "outputId": "4a455b1f-8262-4113-c055-0f9d1b56334f" 397 | }, 398 | "source": [ 399 | "s.traverse()" 400 | ], 401 | "execution_count": null, 402 | "outputs": [ 403 | { 404 | "output_type": "stream", 405 | "text": [ 406 | "5 " 407 | ], 408 | "name": "stdout" 409 | } 410 | ] 411 | }, 412 | { 413 | "cell_type": "code", 414 | "metadata": { 415 | "id": "YnySdksri9z3" 416 | }, 417 | "source": [ 418 | "s.push(5)" 419 | ], 420 | "execution_count": null, 421 | "outputs": [] 422 | }, 423 | { 424 | "cell_type": "code", 425 | "metadata": { 426 | "id": "Px-om90CjL8A" 427 | }, 428 | "source": [], 429 | "execution_count": null, 430 | "outputs": [] 431 | } 432 | ] 433 | } -------------------------------------------------------------------------------- /Linked_List.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [] 7 | }, 8 | "kernelspec": { 9 | "name": "python3", 10 | "display_name": "Python 3" 11 | }, 12 | "language_info": { 13 | "name": "python" 14 | } 15 | }, 16 | "cells": [ 17 | { 18 | "cell_type": "code", 19 | "execution_count": null, 20 | "metadata": { 21 | "id": "3IdNglXYQi7z" 22 | }, 23 | "outputs": [], 24 | "source": [ 25 | "class Node:\n", 26 | "\n", 27 | " def __init__(self,value):\n", 28 | " self.data = value\n", 29 | " self.next = None" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "source": [ 35 | "a = Node(1)\n", 36 | "b = Node(2)\n", 37 | "c = Node(3)" 38 | ], 39 | "metadata": { 40 | "id": "_zNaH474Q840" 41 | }, 42 | "execution_count": null, 43 | "outputs": [] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "source": [ 48 | "print(c.data)" 49 | ], 50 | "metadata": { 51 | "colab": { 52 | "base_uri": "https://localhost:8080/" 53 | }, 54 | "id": "h6ilPlrARD4_", 55 | "outputId": "e2379320-b1b9-4700-df0d-7ea957c3a6b8" 56 | }, 57 | "execution_count": null, 58 | "outputs": [ 59 | { 60 | "output_type": "stream", 61 | "name": "stdout", 62 | "text": [ 63 | "3\n" 64 | ] 65 | } 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "source": [ 71 | "a.next = b\n", 72 | "b.next = c" 73 | ], 74 | "metadata": { 75 | "id": "P7uiJH95RJBg" 76 | }, 77 | "execution_count": null, 78 | "outputs": [] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "source": [ 83 | "print(c.next)" 84 | ], 85 | "metadata": { 86 | "colab": { 87 | "base_uri": "https://localhost:8080/" 88 | }, 89 | "id": "HHST4ARXRnC0", 90 | "outputId": "3d6d2278-93c5-467f-fa88-c767aa4149a1" 91 | }, 92 | "execution_count": null, 93 | "outputs": [ 94 | { 95 | "output_type": "stream", 96 | "name": "stdout", 97 | "text": [ 98 | "None\n" 99 | ] 100 | } 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "source": [ 106 | "int(0x7fde268dfdd0)" 107 | ], 108 | "metadata": { 109 | "colab": { 110 | "base_uri": "https://localhost:8080/" 111 | }, 112 | "id": "uEZs2eLQR7Xf", 113 | "outputId": "944a8020-c2fa-448d-b5f2-0f624385f7ff" 114 | }, 115 | "execution_count": null, 116 | "outputs": [ 117 | { 118 | "output_type": "execute_result", 119 | "data": { 120 | "text/plain": [ 121 | "140592106307024" 122 | ] 123 | }, 124 | "metadata": {}, 125 | "execution_count": 16 126 | } 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "source": [ 132 | "class Node:\n", 133 | "\n", 134 | " def __init__(self,value):\n", 135 | " self.data = value\n", 136 | " self.next = None" 137 | ], 138 | "metadata": { 139 | "id": "5iNlAQR3R91c" 140 | }, 141 | "execution_count": null, 142 | "outputs": [] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "source": [ 147 | "class LinkedList:\n", 148 | "\n", 149 | " def __init__(self):\n", 150 | "\n", 151 | " # Empty Linked List\n", 152 | " self.head = None\n", 153 | " # no of nodes in the LL\n", 154 | " self.n = 0\n", 155 | "\n", 156 | " def __len__(self):\n", 157 | " return self.n\n", 158 | "\n", 159 | " def insert_head(self,value):\n", 160 | "\n", 161 | " # new node\n", 162 | " new_node = Node(value)\n", 163 | "\n", 164 | " # create connection\n", 165 | " new_node.next = self.head\n", 166 | "\n", 167 | " # reassign head\n", 168 | " self.head = new_node\n", 169 | "\n", 170 | " # increment n\n", 171 | " self.n = self.n + 1\n", 172 | "\n", 173 | " def __str__(self):\n", 174 | "\n", 175 | " curr = self.head\n", 176 | "\n", 177 | " result = ''\n", 178 | "\n", 179 | " while curr != None:\n", 180 | " result = result + str(curr.data) + '->'\n", 181 | " curr = curr.next\n", 182 | "\n", 183 | " return result[:-2]\n", 184 | "\n", 185 | " def append(self,value):\n", 186 | "\n", 187 | " new_node = Node(value)\n", 188 | "\n", 189 | " if self.head == None:\n", 190 | " # empty\n", 191 | " self.head = new_node\n", 192 | " self.n = self.n + 1\n", 193 | " return\n", 194 | "\n", 195 | " curr = self.head\n", 196 | "\n", 197 | " while curr.next != None:\n", 198 | " curr = curr.next\n", 199 | "\n", 200 | " # you are at the last node\n", 201 | " curr.next = new_node\n", 202 | " self.n = self.n + 1\n", 203 | "\n", 204 | " def insert_after(self,after,value):\n", 205 | "\n", 206 | " new_node = Node(value)\n", 207 | "\n", 208 | " curr = self.head\n", 209 | "\n", 210 | " while curr != None:\n", 211 | " if curr.data == after:\n", 212 | " break\n", 213 | " curr = curr.next\n", 214 | "\n", 215 | " if curr != None:\n", 216 | " new_node.next = curr.next\n", 217 | " curr.next = new_node\n", 218 | " self.n = self.n + 1\n", 219 | " else:\n", 220 | " return 'Item not found'\n", 221 | "\n", 222 | " def clear(self):\n", 223 | " self.head = None\n", 224 | " self.n = 0\n", 225 | "\n", 226 | " def delete_head(self):\n", 227 | "\n", 228 | " if self.head == None:\n", 229 | " # empty\n", 230 | " return 'Empty LL'\n", 231 | "\n", 232 | " self.head = self.head.next\n", 233 | " self.n = self.n - 1\n", 234 | "\n", 235 | " def pop(self):\n", 236 | "\n", 237 | " if self.head == None:\n", 238 | " # empty\n", 239 | " return 'Empty LL'\n", 240 | "\n", 241 | " curr = self.head\n", 242 | "\n", 243 | " # kya linked list me 1 item hai?\n", 244 | " if curr.next == None:\n", 245 | " # head hi hoga(delete from head)\n", 246 | " return self.delete_head()\n", 247 | " \n", 248 | "\n", 249 | " while curr.next.next != None:\n", 250 | " curr = curr.next\n", 251 | "\n", 252 | " # curr -> 2nd last node\n", 253 | " curr.next = None\n", 254 | " self.n = self.n - 1\n", 255 | "\n", 256 | " def remove(self,value):\n", 257 | "\n", 258 | " if self.head == None:\n", 259 | " return 'Empty LL'\n", 260 | "\n", 261 | " if self.head.data == value:\n", 262 | " # you want to remove the head node\n", 263 | " return self.delete_head()\n", 264 | "\n", 265 | " curr = self.head\n", 266 | "\n", 267 | " while curr.next != None:\n", 268 | " if curr.next.data == value:\n", 269 | " break\n", 270 | " curr = curr.next\n", 271 | "\n", 272 | " # 2 cases item mil gaya\n", 273 | " # item nai mila\n", 274 | " if curr.next == None:\n", 275 | " # item nai mila\n", 276 | " return 'Not Found'\n", 277 | " else:\n", 278 | " curr.next = curr.next.next\n", 279 | " self.n = self.n - 1\n", 280 | "\n", 281 | " def search(self,item):\n", 282 | "\n", 283 | " curr = self.head\n", 284 | " pos = 0\n", 285 | "\n", 286 | " while curr != None:\n", 287 | " if curr.data == item:\n", 288 | " return pos\n", 289 | " curr = curr.next\n", 290 | " pos = pos + 1\n", 291 | "\n", 292 | " return 'Not Found'\n", 293 | "\n", 294 | " def __getitem__(self,index):\n", 295 | "\n", 296 | " curr = self.head\n", 297 | " pos = 0\n", 298 | "\n", 299 | " while curr != None:\n", 300 | " if pos == index:\n", 301 | " return curr.data\n", 302 | " curr = curr.next\n", 303 | " pos = pos + 1\n", 304 | "\n", 305 | " return 'IndexError'\n", 306 | "\n", 307 | "\n", 308 | " \n", 309 | "\n", 310 | " \n", 311 | "\n", 312 | " " 313 | ], 314 | "metadata": { 315 | "id": "RzuzauLNScVg" 316 | }, 317 | "execution_count": null, 318 | "outputs": [] 319 | }, 320 | { 321 | "cell_type": "code", 322 | "source": [ 323 | "L = LinkedList()" 324 | ], 325 | "metadata": { 326 | "id": "gWf5rFdZS-3p" 327 | }, 328 | "execution_count": null, 329 | "outputs": [] 330 | }, 331 | { 332 | "cell_type": "code", 333 | "source": [ 334 | "L.insert_head(1)\n", 335 | "L.append(2)\n", 336 | "L.append(3)\n", 337 | "L.delete_head()\n", 338 | "L.insert_head(4)" 339 | ], 340 | "metadata": { 341 | "id": "Z5yMPIzQTCZr" 342 | }, 343 | "execution_count": null, 344 | "outputs": [] 345 | }, 346 | { 347 | "cell_type": "code", 348 | "source": [ 349 | "print(L)" 350 | ], 351 | "metadata": { 352 | "colab": { 353 | "base_uri": "https://localhost:8080/" 354 | }, 355 | "id": "4HQ9RdjSTEhX", 356 | "outputId": "65cf9986-4a61-418b-b0fe-814b286b5224" 357 | }, 358 | "execution_count": null, 359 | "outputs": [ 360 | { 361 | "output_type": "stream", 362 | "name": "stdout", 363 | "text": [ 364 | "4->4->1->2->3->2->3\n" 365 | ] 366 | } 367 | ] 368 | }, 369 | { 370 | "cell_type": "code", 371 | "source": [ 372 | "L[4]" 373 | ], 374 | "metadata": { 375 | "colab": { 376 | "base_uri": "https://localhost:8080/", 377 | "height": 35 378 | }, 379 | "id": "YbwkHMAJVH0Y", 380 | "outputId": "17e981b8-26f5-4cb0-94f4-0f407c41005f" 381 | }, 382 | "execution_count": null, 383 | "outputs": [ 384 | { 385 | "output_type": "execute_result", 386 | "data": { 387 | "text/plain": [ 388 | "'IndexError'" 389 | ], 390 | "application/vnd.google.colaboratory.intrinsic+json": { 391 | "type": "string" 392 | } 393 | }, 394 | "metadata": {}, 395 | "execution_count": 229 396 | } 397 | ] 398 | }, 399 | { 400 | "cell_type": "code", 401 | "source": [ 402 | "print(L)" 403 | ], 404 | "metadata": { 405 | "colab": { 406 | "base_uri": "https://localhost:8080/" 407 | }, 408 | "id": "Sgp2ql-PYYJw", 409 | "outputId": "3355ea8e-5f0f-4021-ffed-d1487fc72a7e" 410 | }, 411 | "execution_count": null, 412 | "outputs": [ 413 | { 414 | "output_type": "stream", 415 | "name": "stdout", 416 | "text": [ 417 | "\n" 418 | ] 419 | } 420 | ] 421 | }, 422 | { 423 | "cell_type": "code", 424 | "source": [], 425 | "metadata": { 426 | "id": "3Ur3zrkDcoiI" 427 | }, 428 | "execution_count": null, 429 | "outputs": [] 430 | } 431 | ] 432 | } -------------------------------------------------------------------------------- /stacks_using_linkedlists.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [] 7 | }, 8 | "kernelspec": { 9 | "name": "python3", 10 | "display_name": "Python 3" 11 | } 12 | }, 13 | "cells": [ 14 | { 15 | "cell_type": "code", 16 | "metadata": { 17 | "id": "hZmdW65Hrg8u" 18 | }, 19 | "source": [ 20 | "class Node:\n", 21 | "\n", 22 | " def __init__(self,value):\n", 23 | " self.data = value\n", 24 | " self.next = None" 25 | ], 26 | "execution_count": null, 27 | "outputs": [] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "metadata": { 32 | "id": "rtt-E3Borsef" 33 | }, 34 | "source": [ 35 | "class Stack:\n", 36 | "\n", 37 | " def __init__(self):\n", 38 | " self.top = None\n", 39 | "\n", 40 | " def push(self, value):\n", 41 | "\n", 42 | " new_node = Node(value)\n", 43 | "\n", 44 | " new_node.next = self.top\n", 45 | "\n", 46 | " self.top = new_node\n", 47 | "\n", 48 | " def pop(self):\n", 49 | "\n", 50 | " if self.top is None:\n", 51 | " return \"Stack Empty\"\n", 52 | " else:\n", 53 | " data = self.top.data\n", 54 | " self.top = self.top.next\n", 55 | " return data\n", 56 | "\n", 57 | " def is_empty(self):\n", 58 | " return self.top == None\n", 59 | "\n", 60 | " def peek(self):\n", 61 | "\n", 62 | " if self.top is None:\n", 63 | " return \"Stack Empty\"\n", 64 | " else:\n", 65 | " return self.top.data\n", 66 | "\n", 67 | " def traverse(self):\n", 68 | "\n", 69 | " temp = self.top\n", 70 | "\n", 71 | " while temp is not None:\n", 72 | " print(temp.data,end=' ')\n", 73 | " temp = temp.next\n", 74 | "\n", 75 | " def size(self):\n", 76 | "\n", 77 | " temp = self.top\n", 78 | " counter = 0\n", 79 | "\n", 80 | " while temp is not None:\n", 81 | " temp = temp.next\n", 82 | " counter+=1\n", 83 | "\n", 84 | " return counter\n", 85 | "\n", 86 | "\n", 87 | "\n", 88 | " " 89 | ], 90 | "execution_count": null, 91 | "outputs": [] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "metadata": { 96 | "id": "BGbRtvBtsxWv" 97 | }, 98 | "source": [ 99 | "def reverse_string(string):\n", 100 | " s = Stack()\n", 101 | " for i in string:\n", 102 | "\n", 103 | " s.push(i)\n", 104 | "\n", 105 | " res = \"\"\n", 106 | "\n", 107 | " while (not s.is_empty()):\n", 108 | " res = res + s.pop()\n", 109 | "\n", 110 | " print(res)\n" 111 | ], 112 | "execution_count": null, 113 | "outputs": [] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "metadata": { 118 | "id": "QhsdbhM6szTX", 119 | "colab": { 120 | "base_uri": "https://localhost:8080/" 121 | }, 122 | "outputId": "78119fa7-6394-4190-f9cd-ef493960ad47" 123 | }, 124 | "source": [ 125 | "reverse_string(\"Haldia\")" 126 | ], 127 | "execution_count": null, 128 | "outputs": [ 129 | { 130 | "output_type": "stream", 131 | "text": [ 132 | "aidlaH\n" 133 | ], 134 | "name": "stdout" 135 | } 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "metadata": { 141 | "id": "ZrgxN_GUs3qz" 142 | }, 143 | "source": [ 144 | "def text_editor(text,pattern):\n", 145 | "\n", 146 | " u = Stack()\n", 147 | " r = Stack()\n", 148 | "\n", 149 | " for i in text:\n", 150 | " u.push(i)\n", 151 | "\n", 152 | " for i in pattern:\n", 153 | "\n", 154 | " if i == 'u':\n", 155 | " data = u.pop()\n", 156 | " r.push(data)\n", 157 | " else:\n", 158 | " data = r.pop()\n", 159 | " u.push(data)\n", 160 | "\n", 161 | " res = \"\"\n", 162 | "\n", 163 | " while(not u.is_empty()):\n", 164 | " res = u.pop() + res\n", 165 | "\n", 166 | " print(res)" 167 | ], 168 | "execution_count": null, 169 | "outputs": [] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "metadata": { 174 | "id": "oQPCKEfcs5R-", 175 | "colab": { 176 | "base_uri": "https://localhost:8080/" 177 | }, 178 | "outputId": "d1d5fa57-d2dc-4738-c684-c877d6e75918" 179 | }, 180 | "source": [ 181 | "text_editor(\"Hello\",\"uurruuuur\")" 182 | ], 183 | "execution_count": null, 184 | "outputs": [ 185 | { 186 | "output_type": "stream", 187 | "text": [ 188 | "He\n" 189 | ], 190 | "name": "stdout" 191 | } 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "metadata": { 197 | "colab": { 198 | "base_uri": "https://localhost:8080/" 199 | }, 200 | "id": "m654b_-1tk3K", 201 | "outputId": "005a98c8-bb28-4bae-a3d6-f75142a6e7c8" 202 | }, 203 | "source": [ 204 | "s.peek()" 205 | ], 206 | "execution_count": null, 207 | "outputs": [ 208 | { 209 | "output_type": "execute_result", 210 | "data": { 211 | "text/plain": [ 212 | "2" 213 | ] 214 | }, 215 | "metadata": { 216 | "tags": [] 217 | }, 218 | "execution_count": 22 219 | } 220 | ] 221 | }, 222 | { 223 | "cell_type": "code", 224 | "metadata": { 225 | "id": "KAK7xz32uAYC" 226 | }, 227 | "source": [ 228 | "def brackets(expr):\n", 229 | "\n", 230 | " s = Stack()\n", 231 | "\n", 232 | " for i in expr:\n", 233 | "\n", 234 | " if i == '(':\n", 235 | " s.push(i)\n", 236 | " elif i == ')':\n", 237 | " if s.peek() == '(':\n", 238 | " s.pop()\n", 239 | " else:\n", 240 | " print(\"Imbalanced\")\n", 241 | " return \n", 242 | "\n", 243 | " if (s.is_empty()):\n", 244 | " print(\"Balanced\")\n", 245 | " else:\n", 246 | " print(\"Imbalanced\")\n", 247 | "\n" 248 | ], 249 | "execution_count": null, 250 | "outputs": [] 251 | }, 252 | { 253 | "cell_type": "code", 254 | "metadata": { 255 | "id": "QaLvtu9G1TNI" 256 | }, 257 | "source": [ 258 | "L = [\n", 259 | " [0,1,0,0],\n", 260 | " [0,0,1,1],\n", 261 | " [1,0,0,1],\n", 262 | " [0,0,0,0]\n", 263 | "]" 264 | ], 265 | "execution_count": null, 266 | "outputs": [] 267 | }, 268 | { 269 | "cell_type": "code", 270 | "metadata": { 271 | "id": "o8VYAoTf1Xd4" 272 | }, 273 | "source": [ 274 | "def celebrity(L):\n", 275 | "\n", 276 | " s = Stack()\n", 277 | "\n", 278 | " for i in range(len(L)):\n", 279 | " s.push(i)\n", 280 | "\n", 281 | " while s.size() >= 2:\n", 282 | "\n", 283 | " i = s.pop()\n", 284 | " j = s.pop()\n", 285 | "\n", 286 | " if L[i][j] == 0:\n", 287 | " # j is not a celebrity\n", 288 | " s.push(i)\n", 289 | " else:\n", 290 | " # i is not a celebrity\n", 291 | " s.push(j)\n", 292 | "\n", 293 | " cel = s.pop()\n", 294 | "\n", 295 | " for i in range(len(L)):\n", 296 | "\n", 297 | " if i != cel:\n", 298 | "\n", 299 | " if L[i][cel] != 1 or L[cel][i] != 0:\n", 300 | " print(\"No celebrity\")\n", 301 | " return\n", 302 | " print(\"Celebrity is\",cel)\n" 303 | ], 304 | "execution_count": null, 305 | "outputs": [] 306 | }, 307 | { 308 | "cell_type": "code", 309 | "metadata": { 310 | "colab": { 311 | "base_uri": "https://localhost:8080/" 312 | }, 313 | "id": "lTBOAdHl3g_h", 314 | "outputId": "216850a0-166b-49c8-fe31-21511c3c3db3" 315 | }, 316 | "source": [ 317 | "celebrity(L)" 318 | ], 319 | "execution_count": null, 320 | "outputs": [ 321 | { 322 | "output_type": "stream", 323 | "text": [ 324 | "No celebrity\n" 325 | ], 326 | "name": "stdout" 327 | } 328 | ] 329 | }, 330 | { 331 | "cell_type": "code", 332 | "metadata": { 333 | "colab": { 334 | "base_uri": "https://localhost:8080/" 335 | }, 336 | "id": "aHFtqsXH3i8y", 337 | "outputId": "d0031394-070d-43bc-d629-13012308e8ce" 338 | }, 339 | "source": [ 340 | "s=Stack()\n", 341 | "s.push(1)\n", 342 | "s.push(2)\n", 343 | "s.push(3)\n", 344 | "\n", 345 | "s.size()" 346 | ], 347 | "execution_count": null, 348 | "outputs": [ 349 | { 350 | "output_type": "execute_result", 351 | "data": { 352 | "text/plain": [ 353 | "3" 354 | ] 355 | }, 356 | "metadata": { 357 | "tags": [] 358 | }, 359 | "execution_count": 31 360 | } 361 | ] 362 | }, 363 | { 364 | "cell_type": "code", 365 | "metadata": { 366 | "colab": { 367 | "base_uri": "https://localhost:8080/" 368 | }, 369 | "id": "ro6tBQmZ3_zz", 370 | "outputId": "59648a8c-30e6-4b2b-a308-3a4728374729" 371 | }, 372 | "source": [ 373 | "L" 374 | ], 375 | "execution_count": null, 376 | "outputs": [ 377 | { 378 | "output_type": "execute_result", 379 | "data": { 380 | "text/plain": [ 381 | "[[0, 1, 0, 1], [0, 0, 1, 1], [1, 0, 0, 1], [0, 0, 0, 0]]" 382 | ] 383 | }, 384 | "metadata": { 385 | "tags": [] 386 | }, 387 | "execution_count": 32 388 | } 389 | ] 390 | }, 391 | { 392 | "cell_type": "code", 393 | "metadata": { 394 | "colab": { 395 | "base_uri": "https://localhost:8080/" 396 | }, 397 | "id": "K_elaP-x4O0c", 398 | "outputId": "1f3daf0c-63ab-4f40-8a94-e92153cfc95c" 399 | }, 400 | "source": [ 401 | "i = 3\n", 402 | "j = 2\n", 403 | "\n", 404 | "L[i][j]" 405 | ], 406 | "execution_count": null, 407 | "outputs": [ 408 | { 409 | "output_type": "execute_result", 410 | "data": { 411 | "text/plain": [ 412 | "1" 413 | ] 414 | }, 415 | "metadata": { 416 | "tags": [] 417 | }, 418 | "execution_count": 35 419 | } 420 | ] 421 | }, 422 | { 423 | "cell_type": "code", 424 | "metadata": { 425 | "id": "lWg-PCxT4THU" 426 | }, 427 | "source": [ 428 | "a = Stack()\n", 429 | "b = Stack()" 430 | ], 431 | "execution_count": null, 432 | "outputs": [] 433 | }, 434 | { 435 | "cell_type": "code", 436 | "metadata": { 437 | "id": "I5I6UDEOHneS" 438 | }, 439 | "source": [ 440 | "def enqueue_stack(value):\n", 441 | " a.push(value)\n", 442 | "\n", 443 | "def dequeue_stack():\n", 444 | "\n", 445 | " if not b.is_empty():\n", 446 | " b.pop()\n", 447 | " else:\n", 448 | " if a.is_empty():\n", 449 | " print(\"Empty queue\")\n", 450 | " else:\n", 451 | " while(not a.is_empty()):\n", 452 | " data = a.pop()\n", 453 | " b.push(data)\n", 454 | "\n", 455 | " b.pop()\n" 456 | ], 457 | "execution_count": null, 458 | "outputs": [] 459 | }, 460 | { 461 | "cell_type": "code", 462 | "metadata": { 463 | "id": "-MVNBe9nIclt" 464 | }, 465 | "source": [], 466 | "execution_count": null, 467 | "outputs": [] 468 | } 469 | ] 470 | } -------------------------------------------------------------------------------- /hashing_linear_probing.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [] 7 | }, 8 | "kernelspec": { 9 | "name": "python3", 10 | "display_name": "Python 3" 11 | } 12 | }, 13 | "cells": [ 14 | { 15 | "cell_type": "code", 16 | "metadata": { 17 | "id": "wPH5PvaLmjQH" 18 | }, 19 | "source": [ 20 | "class Dictionary:\n", 21 | "\n", 22 | " def __init__(self, size):\n", 23 | " self.size = size\n", 24 | " self.slots = [None] * self.size\n", 25 | " self.data = [None] * self.size\n", 26 | "\n", 27 | " def put(self, key, value):\n", 28 | " hash_value = self.hash_function(key)\n", 29 | "\n", 30 | " if self.slots[hash_value] == None:\n", 31 | " self.slots[hash_value] = key\n", 32 | " self.data[hash_value] = value\n", 33 | "\n", 34 | " else:\n", 35 | "\n", 36 | " if self.slots[hash_value] == key:\n", 37 | " self.data[hash_value] = value\n", 38 | " else:\n", 39 | " new_hash_value = self.rehash(hash_value)\n", 40 | "\n", 41 | " while self.slots[new_hash_value] != None and self.slots[new_hash_value] != key:\n", 42 | " new_hash_value = self.rehash(new_hash_value)\n", 43 | "\n", 44 | " if self.slots[new_hash_value] == None:\n", 45 | " self.slots[new_hash_value] = key\n", 46 | " self.data[new_hash_value] = value\n", 47 | " else:\n", 48 | " self.data[new_hash_value] = value\n", 49 | "\n", 50 | " def get(self, key):\n", 51 | " start_position = self.hash_function(key)\n", 52 | " current_position = start_position\n", 53 | "\n", 54 | " while self.slots[current_position] != None:\n", 55 | "\n", 56 | " if self.slots[current_position] == key:\n", 57 | " return self.data[current_position]\n", 58 | " \n", 59 | " current_position = self.rehash(current_position)\n", 60 | "\n", 61 | " if current_position == start_position:\n", 62 | " return \"Not Found\"\n", 63 | "\n", 64 | " return \"None wala Not Found\"\n", 65 | "\n", 66 | " def __str__(self):\n", 67 | "\n", 68 | " for i in range(len(self.slots)):\n", 69 | " if self.slots[i] != None:\n", 70 | " print(self.slots[i],\":\",self.data[i],end=' ')\n", 71 | "\n", 72 | " return \"\"\n", 73 | "\n", 74 | " def __getitem__(self,key):\n", 75 | " return self.get(key)\n", 76 | "\n", 77 | " def __setitem__(self,key,value):\n", 78 | " self.put(key,value)\n", 79 | " \n", 80 | " def rehash(self, old_hash):\n", 81 | " return (old_hash + 1) % self.size\n", 82 | "\n", 83 | "\n", 84 | " def hash_function(self, key):\n", 85 | "\n", 86 | " return abs(hash(key)) % self.size\n" 87 | ], 88 | "execution_count": null, 89 | "outputs": [] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "metadata": { 94 | "colab": { 95 | "base_uri": "https://localhost:8080/" 96 | }, 97 | "id": "pRrZUGLKng3b", 98 | "outputId": "995d5090-d3b6-488c-8ef0-2cabe4a29873" 99 | }, 100 | "source": [ 101 | "hash(123)" 102 | ], 103 | "execution_count": null, 104 | "outputs": [ 105 | { 106 | "output_type": "execute_result", 107 | "data": { 108 | "text/plain": [ 109 | "123" 110 | ] 111 | }, 112 | "metadata": { 113 | "tags": [] 114 | }, 115 | "execution_count": 2 116 | } 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "metadata": { 122 | "colab": { 123 | "base_uri": "https://localhost:8080/" 124 | }, 125 | "id": "5mieHHXgnidl", 126 | "outputId": "a8f54ce1-79e5-4cc1-e20c-4e1f556898ba" 127 | }, 128 | "source": [ 129 | "abs(hash(\"python\")) % 5" 130 | ], 131 | "execution_count": null, 132 | "outputs": [ 133 | { 134 | "output_type": "execute_result", 135 | "data": { 136 | "text/plain": [ 137 | "1" 138 | ] 139 | }, 140 | "metadata": { 141 | "tags": [] 142 | }, 143 | "execution_count": 10 144 | } 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "metadata": { 150 | "colab": { 151 | "base_uri": "https://localhost:8080/" 152 | }, 153 | "id": "Ccm0C1oFnqlJ", 154 | "outputId": "88bc968b-e660-404f-a838-57ba465d78f5" 155 | }, 156 | "source": [ 157 | "hash(1.5)" 158 | ], 159 | "execution_count": null, 160 | "outputs": [ 161 | { 162 | "output_type": "execute_result", 163 | "data": { 164 | "text/plain": [ 165 | "1152921504606846977" 166 | ] 167 | }, 168 | "metadata": { 169 | "tags": [] 170 | }, 171 | "execution_count": 6 172 | } 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "metadata": { 178 | "colab": { 179 | "base_uri": "https://localhost:8080/" 180 | }, 181 | "id": "UeEPpj7gnw9x", 182 | "outputId": "aa1608a1-747e-4646-def7-c1a0f43de2b1" 183 | }, 184 | "source": [ 185 | "hash((1,2,3))" 186 | ], 187 | "execution_count": null, 188 | "outputs": [ 189 | { 190 | "output_type": "execute_result", 191 | "data": { 192 | "text/plain": [ 193 | "2528502973977326415" 194 | ] 195 | }, 196 | "metadata": { 197 | "tags": [] 198 | }, 199 | "execution_count": 8 200 | } 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "metadata": { 206 | "id": "Tsahuc9dn1RW" 207 | }, 208 | "source": [ 209 | "D1 = Dictionary(3)" 210 | ], 211 | "execution_count": null, 212 | "outputs": [] 213 | }, 214 | { 215 | "cell_type": "code", 216 | "metadata": { 217 | "colab": { 218 | "base_uri": "https://localhost:8080/" 219 | }, 220 | "id": "H-IBVGlMq6CY", 221 | "outputId": "8351a58e-4a49-41da-8ae4-22f6a5eedc5b" 222 | }, 223 | "source": [ 224 | "print(D1.slots)\n", 225 | "print(D1.data)" 226 | ], 227 | "execution_count": null, 228 | "outputs": [ 229 | { 230 | "output_type": "stream", 231 | "text": [ 232 | "[None, None, None]\n", 233 | "[None, None, None]\n" 234 | ], 235 | "name": "stdout" 236 | } 237 | ] 238 | }, 239 | { 240 | "cell_type": "code", 241 | "metadata": { 242 | "id": "5zpRV5qWq-KQ" 243 | }, 244 | "source": [ 245 | "D1['python'] = 56" 246 | ], 247 | "execution_count": null, 248 | "outputs": [] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "metadata": { 253 | "id": "tJMGxuxXrBuA" 254 | }, 255 | "source": [ 256 | "D1['c'] = 1000" 257 | ], 258 | "execution_count": null, 259 | "outputs": [] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "metadata": { 264 | "colab": { 265 | "base_uri": "https://localhost:8080/", 266 | "height": 35 267 | }, 268 | "id": "knXP48RYr3qt", 269 | "outputId": "4af90b14-f8a0-4c09-9154-83a52fd37c3f" 270 | }, 271 | "source": [ 272 | "D1[\"dtjtr\"]" 273 | ], 274 | "execution_count": null, 275 | "outputs": [ 276 | { 277 | "output_type": "execute_result", 278 | "data": { 279 | "application/vnd.google.colaboratory.intrinsic+json": { 280 | "type": "string" 281 | }, 282 | "text/plain": [ 283 | "'None wala Not Found'" 284 | ] 285 | }, 286 | "metadata": { 287 | "tags": [] 288 | }, 289 | "execution_count": 54 290 | } 291 | ] 292 | }, 293 | { 294 | "cell_type": "code", 295 | "metadata": { 296 | "colab": { 297 | "base_uri": "https://localhost:8080/" 298 | }, 299 | "id": "KyVIC_r9r6L_", 300 | "outputId": "72c3d122-a3e2-4d05-e818-4dbc290af2a4" 301 | }, 302 | "source": [ 303 | "print(D1)" 304 | ], 305 | "execution_count": null, 306 | "outputs": [ 307 | { 308 | "output_type": "stream", 309 | "text": [ 310 | "c : 1000 python : 56 \n" 311 | ], 312 | "name": "stdout" 313 | } 314 | ] 315 | }, 316 | { 317 | "cell_type": "code", 318 | "metadata": { 319 | "colab": { 320 | "base_uri": "https://localhost:8080/", 321 | "height": 279 322 | }, 323 | "id": "9m8irDJcvArW", 324 | "outputId": "291f042a-c013-4d9f-c1a8-f4a27a12a1cc" 325 | }, 326 | "source": [ 327 | "D1[[1,2,3]] = \"Hello\"" 328 | ], 329 | "execution_count": null, 330 | "outputs": [ 331 | { 332 | "output_type": "error", 333 | "ename": "TypeError", 334 | "evalue": "ignored", 335 | "traceback": [ 336 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 337 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 338 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mD1\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"Hello\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 339 | "\u001b[0;32m\u001b[0m in \u001b[0;36m__setitem__\u001b[0;34m(self, key, value)\u001b[0m\n\u001b[1;32m 57\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 58\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__setitem__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mvalue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 59\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mvalue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 60\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 61\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mrehash\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mold_hash\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 340 | "\u001b[0;32m\u001b[0m in \u001b[0;36mput\u001b[0;34m(self, key, value)\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mhash_value\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mhash_function\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 10\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mslots\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mhash_value\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 341 | "\u001b[0;32m\u001b[0m in \u001b[0;36mhash_function\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m 65\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mhash_function\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 66\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 67\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mabs\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mhash\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msize\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 342 | "\u001b[0;31mTypeError\u001b[0m: unhashable type: 'list'" 343 | ] 344 | } 345 | ] 346 | }, 347 | { 348 | "cell_type": "code", 349 | "metadata": { 350 | "colab": { 351 | "base_uri": "https://localhost:8080/", 352 | "height": 167 353 | }, 354 | "id": "78eTkCdrvQEO", 355 | "outputId": "80db0b4f-6f13-4c9f-b586-6298981db375" 356 | }, 357 | "source": [ 358 | "D2 = {[1,2,3]:\"Hello\"}" 359 | ], 360 | "execution_count": null, 361 | "outputs": [ 362 | { 363 | "output_type": "error", 364 | "ename": "TypeError", 365 | "evalue": "ignored", 366 | "traceback": [ 367 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 368 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 369 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mD2\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\"Hello\"\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 370 | "\u001b[0;31mTypeError\u001b[0m: unhashable type: 'list'" 371 | ] 372 | } 373 | ] 374 | }, 375 | { 376 | "cell_type": "code", 377 | "metadata": { 378 | "id": "whz_C2lRvWx4" 379 | }, 380 | "source": [], 381 | "execution_count": null, 382 | "outputs": [] 383 | } 384 | ] 385 | } -------------------------------------------------------------------------------- /hashing_via_chaining.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [] 7 | }, 8 | "kernelspec": { 9 | "name": "python3", 10 | "display_name": "Python 3" 11 | } 12 | }, 13 | "cells": [ 14 | { 15 | "cell_type": "code", 16 | "metadata": { 17 | "id": "5RCi9eWfv3KK" 18 | }, 19 | "source": [ 20 | "class Node:\n", 21 | "\n", 22 | " def __init__(self,key,value):\n", 23 | " self.key = key\n", 24 | " self.value = value\n", 25 | " self.next = None" 26 | ], 27 | "execution_count": null, 28 | "outputs": [] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "metadata": { 33 | "id": "mQ4C69JewaWJ" 34 | }, 35 | "source": [ 36 | "class LL:\n", 37 | "\n", 38 | " def __init__(self):\n", 39 | " self.head = None\n", 40 | "\n", 41 | " def add(self, key, value):\n", 42 | "\n", 43 | " new_node = Node(key, value)\n", 44 | "\n", 45 | " if self.head == None:\n", 46 | " self.head = new_node\n", 47 | " else:\n", 48 | "\n", 49 | " temp = self.head\n", 50 | "\n", 51 | " while temp.next != None:\n", 52 | " temp = temp.next\n", 53 | "\n", 54 | " temp.next = new_node\n", 55 | "\n", 56 | " def delete_head(self):\n", 57 | "\n", 58 | " if self.head == None:\n", 59 | " return \"Empty\"\n", 60 | " else:\n", 61 | " self.head = self.head.next\n", 62 | "\n", 63 | " def remove(self, key):\n", 64 | " if self.head.key == key:\n", 65 | " self.delete_head()\n", 66 | " return \n", 67 | "\n", 68 | " if self.head == None:\n", 69 | " return \"Empty\"\n", 70 | " else:\n", 71 | "\n", 72 | " temp = self.head\n", 73 | "\n", 74 | " while temp.next != None:\n", 75 | " if temp.next.key == key:\n", 76 | " break\n", 77 | " temp = temp.next\n", 78 | "\n", 79 | " if temp.next == None:\n", 80 | " return \"Not Found\"\n", 81 | " else:\n", 82 | " temp.next = temp.next.next\n", 83 | " \n", 84 | "\n", 85 | " def traverse(self):\n", 86 | "\n", 87 | " temp = self.head\n", 88 | "\n", 89 | " while temp != None:\n", 90 | "\n", 91 | " print(temp.key,\"-->\",temp.value,\" \", end=\" \")\n", 92 | " temp = temp.next\n", 93 | "\n", 94 | " def size(self):\n", 95 | "\n", 96 | " temp = self.head\n", 97 | " counter = 0\n", 98 | "\n", 99 | " while temp != None:\n", 100 | "\n", 101 | " counter += 1\n", 102 | " temp = temp.next\n", 103 | "\n", 104 | " return counter\n", 105 | "\n", 106 | " def search(self,key):\n", 107 | "\n", 108 | " temp = self.head\n", 109 | " pos = 0\n", 110 | "\n", 111 | " while temp != None:\n", 112 | "\n", 113 | " if temp.key == key:\n", 114 | " return pos\n", 115 | "\n", 116 | " temp = temp.next\n", 117 | " pos += 1\n", 118 | "\n", 119 | " return -1\n", 120 | "\n", 121 | " def get_node_at_index(self,index):\n", 122 | "\n", 123 | " temp = self.head\n", 124 | " counter = 0\n", 125 | "\n", 126 | " while temp is not None:\n", 127 | "\n", 128 | " if counter == index:\n", 129 | " return temp\n", 130 | " temp = temp.next\n", 131 | " counter+=1\n" 132 | ], 133 | "execution_count": null, 134 | "outputs": [] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "metadata": { 139 | "id": "KJq7iUJox5qM" 140 | }, 141 | "source": [ 142 | "class Dictionary:\n", 143 | "\n", 144 | " def __init__(self, capacity):\n", 145 | "\n", 146 | " self.capacity = capacity\n", 147 | " self.size = 0\n", 148 | " # create array of LL\n", 149 | " self.buckets = self.make_array(self.capacity)\n", 150 | "\n", 151 | " def make_array(self,capacity):\n", 152 | "\n", 153 | " L = []\n", 154 | " for i in range(capacity):\n", 155 | " L.append(LL())\n", 156 | " return L\n", 157 | "\n", 158 | " def __setitem__(self,key,value):\n", 159 | " self.put(key,value)\n", 160 | "\n", 161 | " def __getitem__(self,key):\n", 162 | " return self.get(key)\n", 163 | "\n", 164 | " def __delitem__(self,key):\n", 165 | "\n", 166 | " bucket_index = self.hash_function(key)\n", 167 | "\n", 168 | " self.buckets[bucket_index].remove(key)\n", 169 | "\n", 170 | " def __str__(self):\n", 171 | "\n", 172 | " for i in self.buckets:\n", 173 | " i.traverse()\n", 174 | "\n", 175 | " return \"\"\n", 176 | "\n", 177 | " def __len__(self):\n", 178 | " return self.size\n", 179 | "\n", 180 | "\n", 181 | " def get(self,key):\n", 182 | "\n", 183 | " bucket_index = self.hash_function(key)\n", 184 | "\n", 185 | " res = self.buckets[bucket_index].search(key)\n", 186 | "\n", 187 | " if res == -1:\n", 188 | " return \"Not Present\"\n", 189 | " else:\n", 190 | " node = self.buckets[bucket_index].get_node_at_index(res)\n", 191 | " return node.value\n", 192 | "\n", 193 | "\n", 194 | " def put(self, key, value):\n", 195 | "\n", 196 | " bucket_index = self.hash_function(key)\n", 197 | "\n", 198 | " node_index = self.get_node_index(bucket_index, key)\n", 199 | "\n", 200 | " if node_index == -1:\n", 201 | " # insert\n", 202 | " self.buckets[bucket_index].add(key,value)\n", 203 | " self.size+=1\n", 204 | "\n", 205 | " load_factor = self.size/self.capacity\n", 206 | " print(load_factor)\n", 207 | "\n", 208 | " if (load_factor >= 2):\n", 209 | " self.rehash()\n", 210 | " else:\n", 211 | " # update\n", 212 | " node = self.buckets[bucket_index].get_node_at_index(node_index)\n", 213 | " node.value = value\n", 214 | "\n", 215 | " def rehash(self):\n", 216 | " self.capacity = self.capacity * 2\n", 217 | " old_buckets = self.buckets\n", 218 | " self.size = 0\n", 219 | " self.buckets = self.make_array(self.capacity)\n", 220 | "\n", 221 | " for i in old_buckets:\n", 222 | " for j in range(i.size()):\n", 223 | " node = i.get_node_at_index(j)\n", 224 | " key_item = node.key\n", 225 | " value_item = node.value\n", 226 | " self.put(key_item,value_item)\n", 227 | "\n", 228 | "\n", 229 | "\n", 230 | " def get_node_index(self,bucket_index, key):\n", 231 | "\n", 232 | " node_index = self.buckets[bucket_index].search(key)\n", 233 | "\n", 234 | " return node_index\n", 235 | "\n", 236 | " def hash_function(self,key):\n", 237 | " return abs(hash(key)) % self.capacity\n", 238 | "\n" 239 | ], 240 | "execution_count": null, 241 | "outputs": [] 242 | }, 243 | { 244 | "cell_type": "code", 245 | "metadata": { 246 | "id": "f-1NfMh1voU2" 247 | }, 248 | "source": [ 249 | "# get items\n", 250 | "# traverse\n", 251 | "# delete" 252 | ], 253 | "execution_count": null, 254 | "outputs": [] 255 | }, 256 | { 257 | "cell_type": "code", 258 | "metadata": { 259 | "id": "1g-0TdPgylif" 260 | }, 261 | "source": [ 262 | "L = []\n", 263 | "\n", 264 | "for i in range(3):\n", 265 | " L.append(LL())" 266 | ], 267 | "execution_count": null, 268 | "outputs": [] 269 | }, 270 | { 271 | "cell_type": "code", 272 | "metadata": { 273 | "colab": { 274 | "base_uri": "https://localhost:8080/" 275 | }, 276 | "id": "gnel5azbynrp", 277 | "outputId": "7d0d5b02-187b-4daa-b2b4-8f856741cff9" 278 | }, 279 | "source": [ 280 | "L" 281 | ], 282 | "execution_count": null, 283 | "outputs": [ 284 | { 285 | "output_type": "execute_result", 286 | "data": { 287 | "text/plain": [ 288 | "[<__main__.LL at 0x7f9d69437cc0>,\n", 289 | " <__main__.LL at 0x7f9d69437da0>,\n", 290 | " <__main__.LL at 0x7f9d69437eb8>]" 291 | ] 292 | }, 293 | "metadata": { 294 | "tags": [] 295 | }, 296 | "execution_count": 5 297 | } 298 | ] 299 | }, 300 | { 301 | "cell_type": "code", 302 | "metadata": { 303 | "colab": { 304 | "base_uri": "https://localhost:8080/" 305 | }, 306 | "id": "eiHqIb3Ayqcc", 307 | "outputId": "e02a1662-632e-4112-c062-4cfae494a026" 308 | }, 309 | "source": [ 310 | "type(L[0])" 311 | ], 312 | "execution_count": null, 313 | "outputs": [ 314 | { 315 | "output_type": "execute_result", 316 | "data": { 317 | "text/plain": [ 318 | "__main__.LL" 319 | ] 320 | }, 321 | "metadata": { 322 | "tags": [] 323 | }, 324 | "execution_count": 6 325 | } 326 | ] 327 | }, 328 | { 329 | "cell_type": "code", 330 | "metadata": { 331 | "id": "4ErwNKDOyxC_" 332 | }, 333 | "source": [ 334 | "L = [LL()] * 3" 335 | ], 336 | "execution_count": null, 337 | "outputs": [] 338 | }, 339 | { 340 | "cell_type": "code", 341 | "metadata": { 342 | "colab": { 343 | "base_uri": "https://localhost:8080/" 344 | }, 345 | "id": "GNyxjx1GzImN", 346 | "outputId": "4cffafa6-96ab-4832-9cc5-a96c97c351d1" 347 | }, 348 | "source": [ 349 | "L" 350 | ], 351 | "execution_count": null, 352 | "outputs": [ 353 | { 354 | "output_type": "execute_result", 355 | "data": { 356 | "text/plain": [ 357 | "[<__main__.LL at 0x7f9d693902b0>,\n", 358 | " <__main__.LL at 0x7f9d693902b0>,\n", 359 | " <__main__.LL at 0x7f9d693902b0>]" 360 | ] 361 | }, 362 | "metadata": { 363 | "tags": [] 364 | }, 365 | "execution_count": 8 366 | } 367 | ] 368 | }, 369 | { 370 | "cell_type": "code", 371 | "metadata": { 372 | "id": "ebSyUUwOzJNL" 373 | }, 374 | "source": [ 375 | "L = LL()" 376 | ], 377 | "execution_count": null, 378 | "outputs": [] 379 | }, 380 | { 381 | "cell_type": "code", 382 | "metadata": { 383 | "id": "8DIaEzDz2Qkm" 384 | }, 385 | "source": [ 386 | "L.add(2,3)" 387 | ], 388 | "execution_count": null, 389 | "outputs": [] 390 | }, 391 | { 392 | "cell_type": "code", 393 | "metadata": { 394 | "id": "S2tQqsow2SDP" 395 | }, 396 | "source": [ 397 | "L.add(4,5)" 398 | ], 399 | "execution_count": null, 400 | "outputs": [] 401 | }, 402 | { 403 | "cell_type": "code", 404 | "metadata": { 405 | "id": "xburqnbf2TIL" 406 | }, 407 | "source": [ 408 | "L.add(6,7)" 409 | ], 410 | "execution_count": null, 411 | "outputs": [] 412 | }, 413 | { 414 | "cell_type": "code", 415 | "metadata": { 416 | "colab": { 417 | "base_uri": "https://localhost:8080/" 418 | }, 419 | "id": "v4M2yvXb2UV5", 420 | "outputId": "ceb8af33-4642-43d9-f384-71b00c513362" 421 | }, 422 | "source": [ 423 | "L.traverse()" 424 | ], 425 | "execution_count": null, 426 | "outputs": [ 427 | { 428 | "output_type": "stream", 429 | "text": [ 430 | "2 --> 3 4 --> 5 6 --> 7 " 431 | ], 432 | "name": "stdout" 433 | } 434 | ] 435 | }, 436 | { 437 | "cell_type": "code", 438 | "metadata": { 439 | "colab": { 440 | "base_uri": "https://localhost:8080/" 441 | }, 442 | "id": "nGsk8KSb2Wni", 443 | "outputId": "101cb8cc-836f-464b-b7c8-cc3041fe832a" 444 | }, 445 | "source": [ 446 | "L.get_node_at_index(0).key" 447 | ], 448 | "execution_count": null, 449 | "outputs": [ 450 | { 451 | "output_type": "execute_result", 452 | "data": { 453 | "text/plain": [ 454 | "2" 455 | ] 456 | }, 457 | "metadata": { 458 | "tags": [] 459 | }, 460 | "execution_count": 18 461 | } 462 | ] 463 | }, 464 | { 465 | "cell_type": "code", 466 | "metadata": { 467 | "id": "S7_1IJsO2dOf" 468 | }, 469 | "source": [ 470 | "D1 = Dictionary(3)" 471 | ], 472 | "execution_count": null, 473 | "outputs": [] 474 | }, 475 | { 476 | "cell_type": "code", 477 | "metadata": { 478 | "colab": { 479 | "base_uri": "https://localhost:8080/" 480 | }, 481 | "id": "Dfyo_T733ydy", 482 | "outputId": "481df50d-5ca4-4b36-d4e6-ced30b1f4f12" 483 | }, 484 | "source": [ 485 | "D1.put(\"c\",20000)" 486 | ], 487 | "execution_count": null, 488 | "outputs": [ 489 | { 490 | "output_type": "stream", 491 | "text": [ 492 | "0.3333333333333333\n" 493 | ], 494 | "name": "stdout" 495 | } 496 | ] 497 | }, 498 | { 499 | "cell_type": "code", 500 | "metadata": { 501 | "colab": { 502 | "base_uri": "https://localhost:8080/", 503 | "height": 35 504 | }, 505 | "id": "nsbZt6pp3-Fc", 506 | "outputId": "1bd9cd08-44fd-457c-9075-13d5d71fb15e" 507 | }, 508 | "source": [ 509 | "D1[\"java\"]" 510 | ], 511 | "execution_count": null, 512 | "outputs": [ 513 | { 514 | "output_type": "execute_result", 515 | "data": { 516 | "application/vnd.google.colaboratory.intrinsic+json": { 517 | "type": "string" 518 | }, 519 | "text/plain": [ 520 | "'Not Present'" 521 | ] 522 | }, 523 | "metadata": { 524 | "tags": [] 525 | }, 526 | "execution_count": 25 527 | } 528 | ] 529 | }, 530 | { 531 | "cell_type": "code", 532 | "metadata": { 533 | "id": "-83pOiph4gZJ" 534 | }, 535 | "source": [ 536 | "del D1[\"java\"]" 537 | ], 538 | "execution_count": null, 539 | "outputs": [] 540 | }, 541 | { 542 | "cell_type": "code", 543 | "metadata": { 544 | "id": "gDBNpIki4k-U", 545 | "colab": { 546 | "base_uri": "https://localhost:8080/" 547 | }, 548 | "outputId": "10f24aae-2945-416f-bee1-6869a3154d51" 549 | }, 550 | "source": [ 551 | "D1.put(\"Java\",56)" 552 | ], 553 | "execution_count": null, 554 | "outputs": [ 555 | { 556 | "output_type": "stream", 557 | "text": [ 558 | "0.5\n" 559 | ], 560 | "name": "stdout" 561 | } 562 | ] 563 | }, 564 | { 565 | "cell_type": "code", 566 | "metadata": { 567 | "id": "4t6613Mq4tsG" 568 | }, 569 | "source": [ 570 | "D1 = Dictionary(4)" 571 | ], 572 | "execution_count": null, 573 | "outputs": [] 574 | }, 575 | { 576 | "cell_type": "code", 577 | "metadata": { 578 | "colab": { 579 | "base_uri": "https://localhost:8080/" 580 | }, 581 | "id": "-_px_9XJ7M4N", 582 | "outputId": "9c962f2b-e743-4208-9bce-9cae866deb98" 583 | }, 584 | "source": [ 585 | "D1.put(\"php\",34)" 586 | ], 587 | "execution_count": null, 588 | "outputs": [ 589 | { 590 | "output_type": "stream", 591 | "text": [ 592 | "0.6666666666666666\n" 593 | ], 594 | "name": "stdout" 595 | } 596 | ] 597 | }, 598 | { 599 | "cell_type": "code", 600 | "metadata": { 601 | "id": "giRQ40pm7Y2f", 602 | "colab": { 603 | "base_uri": "https://localhost:8080/" 604 | }, 605 | "outputId": "1fa50109-4d19-4b33-8a6a-5b9c082f4d2b" 606 | }, 607 | "source": [ 608 | "D1[\"matlab\"] = 45" 609 | ], 610 | "execution_count": null, 611 | "outputs": [ 612 | { 613 | "output_type": "stream", 614 | "text": [ 615 | "1.0\n" 616 | ], 617 | "name": "stdout" 618 | } 619 | ] 620 | }, 621 | { 622 | "cell_type": "code", 623 | "metadata": { 624 | "colab": { 625 | "base_uri": "https://localhost:8080/" 626 | }, 627 | "id": "4B8OPQrgxwMy", 628 | "outputId": "0e82f476-d62c-4d4a-a2d8-33900140cbd6" 629 | }, 630 | "source": [ 631 | "print(D1)" 632 | ], 633 | "execution_count": null, 634 | "outputs": [ 635 | { 636 | "output_type": "stream", 637 | "text": [ 638 | "php --> 34 matlab --> 45 c --> 20000 \n" 639 | ], 640 | "name": "stdout" 641 | } 642 | ] 643 | }, 644 | { 645 | "cell_type": "code", 646 | "metadata": { 647 | "colab": { 648 | "base_uri": "https://localhost:8080/" 649 | }, 650 | "id": "epVCxmJpxypX", 651 | "outputId": "86edeb6b-baf7-456d-947c-8646cd0e6738" 652 | }, 653 | "source": [ 654 | "len(D1)" 655 | ], 656 | "execution_count": null, 657 | "outputs": [ 658 | { 659 | "output_type": "execute_result", 660 | "data": { 661 | "text/plain": [ 662 | "3" 663 | ] 664 | }, 665 | "metadata": { 666 | "tags": [] 667 | }, 668 | "execution_count": 41 669 | } 670 | ] 671 | }, 672 | { 673 | "cell_type": "code", 674 | "metadata": { 675 | "id": "k9IVgAlX0XFo" 676 | }, 677 | "source": [], 678 | "execution_count": null, 679 | "outputs": [] 680 | } 681 | ] 682 | } -------------------------------------------------------------------------------- /searching_sorting.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [] 7 | }, 8 | "kernelspec": { 9 | "display_name": "Python 3", 10 | "name": "python3" 11 | } 12 | }, 13 | "cells": [ 14 | { 15 | "cell_type": "code", 16 | "metadata": { 17 | "id": "jqVB3ib40scd" 18 | }, 19 | "source": [ 20 | "# Linear Searching\n", 21 | "# Binary\n", 22 | "\n", 23 | "# Sleep Sort\n", 24 | "# Monkey Sort\n", 25 | "# Bubble Sort\n", 26 | "# Selection\n", 27 | "# Merge Sort\n", 28 | "# Quick Sort\n", 29 | "# Insertion Sort" 30 | ], 31 | "execution_count": null, 32 | "outputs": [] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "metadata": { 37 | "id": "Vcoqh6ob1dwG" 38 | }, 39 | "source": [ 40 | "# Linear Search\n", 41 | "\n", 42 | "# Brute Force\n", 43 | "\n", 44 | "def linear_search(arr,item):\n", 45 | "\n", 46 | " for i in range(len(arr)):\n", 47 | "\n", 48 | " if arr[i] == item:\n", 49 | " return i\n", 50 | "\n", 51 | " return -1" 52 | ], 53 | "execution_count": null, 54 | "outputs": [] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "metadata": { 59 | "colab": { 60 | "base_uri": "https://localhost:8080/" 61 | }, 62 | "id": "0gIHvKcb151X", 63 | "outputId": "791af712-4de4-492c-f43c-037292fcae74" 64 | }, 65 | "source": [ 66 | "arr = [12,34,56,1,67,100,47,99]\n", 67 | "\n", 68 | "linear_search(arr,121)" 69 | ], 70 | "execution_count": null, 71 | "outputs": [ 72 | { 73 | "output_type": "execute_result", 74 | "data": { 75 | "text/plain": [ 76 | "-1" 77 | ] 78 | }, 79 | "metadata": { 80 | "tags": [] 81 | }, 82 | "execution_count": 6 83 | } 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "metadata": { 89 | "id": "8aFnf5s42GKE" 90 | }, 91 | "source": [ 92 | "# Time Complexity is O(N)\n", 93 | "# No sorting required" 94 | ], 95 | "execution_count": null, 96 | "outputs": [] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "metadata": { 101 | "id": "m1_ZWchm2dRL" 102 | }, 103 | "source": [ 104 | "# Binary Search\n", 105 | "\n", 106 | "# Sorted Array \n", 107 | "\n", 108 | "def binary_search(arr, low, high, item):\n", 109 | "\n", 110 | " #print(\"low = \",low,\"high = \",high,end=' ')\n", 111 | "\n", 112 | " if low <= high:\n", 113 | " # search\n", 114 | "\n", 115 | " mid = (low + high)//2\n", 116 | "\n", 117 | " #print(\"mid value is\",arr[mid])\n", 118 | "\n", 119 | " if arr[mid] == item:\n", 120 | " return mid\n", 121 | " elif arr[mid] > item:\n", 122 | " return binary_search(arr, low, mid-1,item)\n", 123 | " else:\n", 124 | " return binary_search(arr,mid+1,high,item)\n", 125 | " else:\n", 126 | " return -1" 127 | ], 128 | "execution_count": null, 129 | "outputs": [] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "metadata": { 134 | "colab": { 135 | "base_uri": "https://localhost:8080/" 136 | }, 137 | "id": "TIHQdlLE4HOJ", 138 | "outputId": "29799497-bc89-4e41-a43d-f3b8754eef91" 139 | }, 140 | "source": [ 141 | "arr = [12,24,35,46,57,68,80,99,100]\n", 142 | "print(binary_search(arr,0,len(arr)-1,-5))" 143 | ], 144 | "execution_count": null, 145 | "outputs": [ 146 | { 147 | "output_type": "stream", 148 | "text": [ 149 | "-1\n" 150 | ], 151 | "name": "stdout" 152 | } 153 | ] 154 | }, 155 | { 156 | "cell_type": "code", 157 | "metadata": { 158 | "id": "y2akKfUq4YQR" 159 | }, 160 | "source": [ 161 | "# Sorting\n", 162 | "\n", 163 | "def is_sorted(arr):\n", 164 | "\n", 165 | " sorted = True\n", 166 | "\n", 167 | " for i in range(len(arr) - 1):\n", 168 | " if arr[i]>arr[i+1]:\n", 169 | " sorted = False\n", 170 | " \n", 171 | " return sorted" 172 | ], 173 | "execution_count": null, 174 | "outputs": [] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "metadata": { 179 | "colab": { 180 | "base_uri": "https://localhost:8080/" 181 | }, 182 | "id": "Q7ZlBwxY6lnF", 183 | "outputId": "9dddc45f-9416-4415-c8b5-fa2f6d911af6" 184 | }, 185 | "source": [ 186 | "arr = [1,2,3,4,8,6]\n", 187 | "is_sorted(arr)" 188 | ], 189 | "execution_count": null, 190 | "outputs": [ 191 | { 192 | "output_type": "execute_result", 193 | "data": { 194 | "text/plain": [ 195 | "False" 196 | ] 197 | }, 198 | "metadata": { 199 | "tags": [] 200 | }, 201 | "execution_count": 21 202 | } 203 | ] 204 | }, 205 | { 206 | "cell_type": "code", 207 | "metadata": { 208 | "colab": { 209 | "base_uri": "https://localhost:8080/" 210 | }, 211 | "id": "OQBwGjVO6r4I", 212 | "outputId": "d4b88293-8270-4134-ac03-5218c969f72e" 213 | }, 214 | "source": [ 215 | "# Monkey sort\n", 216 | "\n", 217 | "import random\n", 218 | "a=[1,2,3,4]\n", 219 | "random.shuffle(a)\n", 220 | "a" 221 | ], 222 | "execution_count": null, 223 | "outputs": [ 224 | { 225 | "output_type": "execute_result", 226 | "data": { 227 | "text/plain": [ 228 | "[2, 1, 3, 4]" 229 | ] 230 | }, 231 | "metadata": { 232 | "tags": [] 233 | }, 234 | "execution_count": 25 235 | } 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "metadata": { 241 | "id": "1w7aQ4DO7C6U" 242 | }, 243 | "source": [ 244 | "import time\n", 245 | "def monkey_sort(arr):\n", 246 | "\n", 247 | " while not is_sorted(arr):\n", 248 | " time.sleep(1)\n", 249 | " random.shuffle(arr)\n", 250 | " print(arr)\n", 251 | " print(arr)" 252 | ], 253 | "execution_count": null, 254 | "outputs": [] 255 | }, 256 | { 257 | "cell_type": "code", 258 | "metadata": { 259 | "colab": { 260 | "base_uri": "https://localhost:8080/" 261 | }, 262 | "id": "Br5OepXe7aXN", 263 | "outputId": "8a692cf7-d03e-49d8-c406-eb90a27e0c38" 264 | }, 265 | "source": [ 266 | "a = [12,24,11,56,34,20]\n", 267 | "monkey_sort(a)" 268 | ], 269 | "execution_count": null, 270 | "outputs": [ 271 | { 272 | "output_type": "stream", 273 | "text": [ 274 | "[24, 20, 11, 56, 12, 34]\n", 275 | "[11, 34, 12, 56, 20, 24]\n", 276 | "[20, 11, 24, 12, 34, 56]\n", 277 | "[24, 20, 34, 56, 11, 12]\n", 278 | "[56, 34, 12, 20, 11, 24]\n", 279 | "[34, 12, 11, 20, 56, 24]\n", 280 | "[56, 34, 20, 12, 11, 24]\n", 281 | "[11, 20, 34, 56, 24, 12]\n", 282 | "[24, 12, 11, 34, 56, 20]\n", 283 | "[20, 56, 11, 24, 34, 12]\n", 284 | "[56, 12, 24, 20, 11, 34]\n", 285 | "[56, 20, 24, 34, 11, 12]\n", 286 | "[56, 20, 24, 11, 34, 12]\n", 287 | "[56, 12, 24, 20, 34, 11]\n", 288 | "[11, 12, 20, 24, 34, 56]\n", 289 | "[11, 12, 20, 24, 34, 56]\n" 290 | ], 291 | "name": "stdout" 292 | } 293 | ] 294 | }, 295 | { 296 | "cell_type": "code", 297 | "metadata": { 298 | "id": "lIqq4fMp7f75" 299 | }, 300 | "source": [ 301 | "12 24 45 5 16" 302 | ], 303 | "execution_count": null, 304 | "outputs": [] 305 | }, 306 | { 307 | "cell_type": "code", 308 | "metadata": { 309 | "id": "gsiRM1N_85ty" 310 | }, 311 | "source": [ 312 | "5 12 16 24 45" 313 | ], 314 | "execution_count": null, 315 | "outputs": [] 316 | }, 317 | { 318 | "cell_type": "code", 319 | "metadata": { 320 | "id": "u_lW50sxGJiZ" 321 | }, 322 | "source": [ 323 | "def bubble_sort(arr):\n", 324 | "\n", 325 | " for i in range(len(arr) - 1):\n", 326 | " flag = 0\n", 327 | " for j in range(len(arr) - 1 - i):\n", 328 | " if arr[j] > arr[j+1]:\n", 329 | " arr[j],arr[j+1] = arr[j+1],arr[j]\n", 330 | " flag =1\n", 331 | "\n", 332 | " if flag == 0:\n", 333 | " break\n", 334 | " \n", 335 | " return arr" 336 | ], 337 | "execution_count": null, 338 | "outputs": [] 339 | }, 340 | { 341 | "cell_type": "code", 342 | "metadata": { 343 | "colab": { 344 | "base_uri": "https://localhost:8080/" 345 | }, 346 | "id": "oj6D4lmLHCNV", 347 | "outputId": "08223624-9edb-48ef-e014-76add5913fd5" 348 | }, 349 | "source": [ 350 | "arr = [23,12,34,11,100,56,78]\n", 351 | "bubble_sort(arr)" 352 | ], 353 | "execution_count": null, 354 | "outputs": [ 355 | { 356 | "output_type": "stream", 357 | "text": [ 358 | "[11, 12, 23, 34, 56, 78, 100]\n" 359 | ], 360 | "name": "stdout" 361 | } 362 | ] 363 | }, 364 | { 365 | "cell_type": "code", 366 | "metadata": { 367 | "id": "dVWdsgFfHFie" 368 | }, 369 | "source": [ 370 | "def selection_sort(arr):\n", 371 | "\n", 372 | " for i in range(len(arr) - 1):\n", 373 | "\n", 374 | " min = i\n", 375 | "\n", 376 | " for j in range(i+1,len(arr)):\n", 377 | " if arr[j] < arr[min]:\n", 378 | " min = j\n", 379 | " \n", 380 | " arr[i],arr[min] = arr[min],arr[i]\n", 381 | "\n", 382 | " return arr\n", 383 | "\n" 384 | ], 385 | "execution_count": null, 386 | "outputs": [] 387 | }, 388 | { 389 | "cell_type": "code", 390 | "metadata": { 391 | "colab": { 392 | "base_uri": "https://localhost:8080/" 393 | }, 394 | "id": "JB_zTL3-isvI", 395 | "outputId": "666a6e7d-5782-48f4-ba0a-e81f09c766fb" 396 | }, 397 | "source": [ 398 | "arr = [23,12,34,11,100,56,78]\n", 399 | "selection_sort(arr)" 400 | ], 401 | "execution_count": null, 402 | "outputs": [ 403 | { 404 | "output_type": "stream", 405 | "text": [ 406 | "1 pass Current min is 23\n", 407 | "Current item under observation 12\n", 408 | "Current item is less than min\n", 409 | "Now the min has become 12\n", 410 | "Current item under observation 34\n", 411 | "Current item under observation 11\n", 412 | "Current item is less than min\n", 413 | "Now the min has become 11\n", 414 | "Current item under observation 100\n", 415 | "Current item under observation 56\n", 416 | "Current item under observation 78\n", 417 | "[11, 12, 34, 23, 100, 56, 78]\n", 418 | "**************************************************\n", 419 | "2 pass Current min is 12\n", 420 | "Current item under observation 34\n", 421 | "Current item under observation 23\n", 422 | "Current item under observation 100\n", 423 | "Current item under observation 56\n", 424 | "Current item under observation 78\n", 425 | "[11, 12, 34, 23, 100, 56, 78]\n", 426 | "**************************************************\n", 427 | "3 pass Current min is 34\n", 428 | "Current item under observation 23\n", 429 | "Current item is less than min\n", 430 | "Now the min has become 23\n", 431 | "Current item under observation 100\n", 432 | "Current item under observation 56\n", 433 | "Current item under observation 78\n", 434 | "[11, 12, 23, 34, 100, 56, 78]\n", 435 | "**************************************************\n", 436 | "4 pass Current min is 34\n", 437 | "Current item under observation 100\n", 438 | "Current item under observation 56\n", 439 | "Current item under observation 78\n", 440 | "[11, 12, 23, 34, 100, 56, 78]\n", 441 | "**************************************************\n", 442 | "5 pass Current min is 100\n", 443 | "Current item under observation 56\n", 444 | "Current item is less than min\n", 445 | "Now the min has become 56\n", 446 | "Current item under observation 78\n", 447 | "[11, 12, 23, 34, 56, 100, 78]\n", 448 | "**************************************************\n", 449 | "6 pass Current min is 100\n", 450 | "Current item under observation 78\n", 451 | "Current item is less than min\n", 452 | "Now the min has become 78\n", 453 | "[11, 12, 23, 34, 56, 78, 100]\n", 454 | "**************************************************\n", 455 | "[11, 12, 23, 34, 56, 78, 100]\n" 456 | ], 457 | "name": "stdout" 458 | } 459 | ] 460 | }, 461 | { 462 | "cell_type": "code", 463 | "metadata": { 464 | "id": "UQ0IoyZEi1wd" 465 | }, 466 | "source": [ 467 | "L = []\n", 468 | "\n", 469 | "import random\n", 470 | "\n", 471 | "for i in range(10000):\n", 472 | " L.append(random.randint(1,10000))" 473 | ], 474 | "execution_count": null, 475 | "outputs": [] 476 | }, 477 | { 478 | "cell_type": "code", 479 | "metadata": { 480 | "colab": { 481 | "base_uri": "https://localhost:8080/" 482 | }, 483 | "id": "Q4gXj1bwk48c", 484 | "outputId": "149a726d-3bd1-40e1-a9b9-ed86862a97fa" 485 | }, 486 | "source": [ 487 | "len(L)" 488 | ], 489 | "execution_count": null, 490 | "outputs": [ 491 | { 492 | "output_type": "execute_result", 493 | "data": { 494 | "text/plain": [ 495 | "10000" 496 | ] 497 | }, 498 | "metadata": { 499 | "tags": [] 500 | }, 501 | "execution_count": 12 502 | } 503 | ] 504 | }, 505 | { 506 | "cell_type": "code", 507 | "metadata": { 508 | "id": "WbkyZJ1qk7Ds" 509 | }, 510 | "source": [ 511 | "L1 = L[:]\n", 512 | "# cloning" 513 | ], 514 | "execution_count": null, 515 | "outputs": [] 516 | }, 517 | { 518 | "cell_type": "code", 519 | "metadata": { 520 | "colab": { 521 | "base_uri": "https://localhost:8080/" 522 | }, 523 | "id": "oatQ31jPk_m1", 524 | "outputId": "eb6d0c35-ff33-4f98-e571-c2115e4e019e" 525 | }, 526 | "source": [ 527 | "import time\n", 528 | "\n", 529 | "start = time.time()\n", 530 | "bubble_sort(L)\n", 531 | "print(\"Time taken\",time.time() - start,\"secs\")" 532 | ], 533 | "execution_count": null, 534 | "outputs": [ 535 | { 536 | "output_type": "stream", 537 | "text": [ 538 | "Time taken 0.0018186569213867188 secs\n" 539 | ], 540 | "name": "stdout" 541 | } 542 | ] 543 | }, 544 | { 545 | "cell_type": "code", 546 | "metadata": { 547 | "colab": { 548 | "base_uri": "https://localhost:8080/" 549 | }, 550 | "id": "QzXY_fzLlQ9O", 551 | "outputId": "c0fca59b-d6f1-43f2-9060-d74d62979133" 552 | }, 553 | "source": [ 554 | "start = time.time()\n", 555 | "selection_sort(L1)\n", 556 | "print(\"Time taken\",time.time() - start,\"secs\")\n", 557 | "\n", 558 | "# Not Adaptive" 559 | ], 560 | "execution_count": null, 561 | "outputs": [ 562 | { 563 | "output_type": "stream", 564 | "text": [ 565 | "Time taken 3.4115519523620605 secs\n" 566 | ], 567 | "name": "stdout" 568 | } 569 | ] 570 | }, 571 | { 572 | "cell_type": "code", 573 | "metadata": { 574 | "id": "iJ2Xbh4llbJc" 575 | }, 576 | "source": [ 577 | "# Merge Sort" 578 | ], 579 | "execution_count": null, 580 | "outputs": [] 581 | }, 582 | { 583 | "cell_type": "code", 584 | "metadata": { 585 | "id": "e5Skvkui0uVx" 586 | }, 587 | "source": [ 588 | "def merge_arrays(arr1,arr2):\n", 589 | "\n", 590 | " i = 0\n", 591 | " j = 0\n", 592 | "\n", 593 | " new_arr = []\n", 594 | "\n", 595 | " while i\"\u001b[0;36m, line \u001b[0;32m5\u001b[0m\n\u001b[0;31m merge sort on left\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" 712 | ] 713 | } 714 | ] 715 | }, 716 | { 717 | "cell_type": "code", 718 | "metadata": { 719 | "id": "4UzPu-WZxpTQ" 720 | }, 721 | "source": [ 722 | "def quick_sort(arr):\n", 723 | "\n", 724 | " if len(arr) <= 1:\n", 725 | " return arr\n", 726 | "\n", 727 | " pivot = arr.pop()\n", 728 | "\n", 729 | " items_left = []\n", 730 | " items_right = []\n", 731 | "\n", 732 | " for item in arr:\n", 733 | " if item < pivot:\n", 734 | " items_left.append(item)\n", 735 | " else:\n", 736 | " items_right.append(item)\n", 737 | "\n", 738 | " return quick_sort(items_left) + [pivot] + quick_sort(items_right)\n", 739 | "\n", 740 | "\n" 741 | ], 742 | "execution_count": null, 743 | "outputs": [] 744 | }, 745 | { 746 | "cell_type": "code", 747 | "metadata": { 748 | "colab": { 749 | "base_uri": "https://localhost:8080/" 750 | }, 751 | "id": "yBKRs3a_yUq2", 752 | "outputId": "f284aa5a-0e81-416a-ae65-c5b01ae5dacf" 753 | }, 754 | "source": [ 755 | "arr = [2,1,4,6,3,7,5]\n", 756 | "quick_sort(arr)" 757 | ], 758 | "execution_count": null, 759 | "outputs": [ 760 | { 761 | "output_type": "execute_result", 762 | "data": { 763 | "text/plain": [ 764 | "[1, 2, 3, 4, 5, 6, 7]" 765 | ] 766 | }, 767 | "metadata": { 768 | "tags": [] 769 | }, 770 | "execution_count": 8 771 | } 772 | ] 773 | }, 774 | { 775 | "cell_type": "code", 776 | "metadata": { 777 | "id": "PNf7neZ1yZ1s" 778 | }, 779 | "source": [ 780 | "def quick_sort(arr,low,high):\n", 781 | "\n", 782 | " if len(arr) == 1:\n", 783 | " return arr\n", 784 | "\n", 785 | " if low < high:\n", 786 | "\n", 787 | " pi = partition(arr,low,high)\n", 788 | "\n", 789 | " quick_sort(arr,low,pi-1)\n", 790 | " quick_sort(arr,pi+1,high)" 791 | ], 792 | "execution_count": null, 793 | "outputs": [] 794 | }, 795 | { 796 | "cell_type": "code", 797 | "metadata": { 798 | "id": "RqlGnvbu4t2b" 799 | }, 800 | "source": [ 801 | "def partition(arr,low,high):\n", 802 | "\n", 803 | " i = low - 1\n", 804 | " pivot = arr[high]\n", 805 | "\n", 806 | " for j in range(low,high):\n", 807 | "\n", 808 | " if arr[j] <= pivot:\n", 809 | " i+=1\n", 810 | " arr[i],arr[j] = arr[j],arr[i]\n", 811 | " arr[i+1],arr[high] = arr[high],arr[i+1]\n", 812 | "\n", 813 | " return i+1" 814 | ], 815 | "execution_count": null, 816 | "outputs": [] 817 | }, 818 | { 819 | "cell_type": "code", 820 | "metadata": { 821 | "colab": { 822 | "base_uri": "https://localhost:8080/" 823 | }, 824 | "id": "6k7ip8Fm5fx7", 825 | "outputId": "f29344d4-f23d-41fa-e6c9-44f895de6b55" 826 | }, 827 | "source": [ 828 | "arr = [10, 7, 8, 9, 1, 5] \n", 829 | "n = len(arr) \n", 830 | "quick_sort(arr, 0, n-1)\n", 831 | "print(arr)" 832 | ], 833 | "execution_count": null, 834 | "outputs": [ 835 | { 836 | "output_type": "stream", 837 | "text": [ 838 | "[1, 5, 7, 8, 9, 10]\n" 839 | ], 840 | "name": "stdout" 841 | } 842 | ] 843 | }, 844 | { 845 | "cell_type": "code", 846 | "metadata": { 847 | "id": "Lf1z0WWC56wz" 848 | }, 849 | "source": [], 850 | "execution_count": null, 851 | "outputs": [] 852 | } 853 | ] 854 | } --------------------------------------------------------------------------------