|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Finding Max Element in the Array\n", |
| 8 | + "\n", |
| 9 | + "We can find the maximum element in the array by iterating through the array and updating the maximum element value throughout the array. The algorithm used to do the same is as below : \n", |
| 10 | + "\n", |
| 11 | + "Max(A[0....n-1]):\n", |
| 12 | + "\n", |
| 13 | + " max = A[0]\n", |
| 14 | + " for i <- 1 to n-1:\n", |
| 15 | + " if A[i]>max:\n", |
| 16 | + " max = A[i]\n", |
| 17 | + " return max" |
| 18 | + ] |
| 19 | + }, |
| 20 | + { |
| 21 | + "cell_type": "code", |
| 22 | + "execution_count": 78, |
| 23 | + "metadata": {}, |
| 24 | + "outputs": [], |
| 25 | + "source": [ |
| 26 | + "from datetime import datetime as dt\n", |
| 27 | + "import random" |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "markdown", |
| 32 | + "metadata": {}, |
| 33 | + "source": [ |
| 34 | + "#### This is a random array generator which generates an array of 1000 elements in the range [0, 2^31-1]" |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "cell_type": "code", |
| 39 | + "execution_count": 84, |
| 40 | + "metadata": {}, |
| 41 | + "outputs": [], |
| 42 | + "source": [ |
| 43 | + "def random_arr_generator() -> list:\n", |
| 44 | + " random_arr = [round(random.randint(0, pow(2,31)-1)) for i in range(1000)]\n", |
| 45 | + " return random_arr" |
| 46 | + ] |
| 47 | + }, |
| 48 | + { |
| 49 | + "cell_type": "markdown", |
| 50 | + "metadata": {}, |
| 51 | + "source": [ |
| 52 | + "#### The following algorithm first generates a random array of 1000 numbers and then follows the above mentioned algorithm to find the maximum element in the array. \n", |
| 53 | + "##### (You can use the commented out line to print the time taken in milliseconds for one execution of the function) " |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "code", |
| 58 | + "execution_count": 92, |
| 59 | + "metadata": {}, |
| 60 | + "outputs": [], |
| 61 | + "source": [ |
| 62 | + "# FUNCTION DEFINITION\n", |
| 63 | + "\n", |
| 64 | + "def Max_element() -> int:\n", |
| 65 | + " start = dt.now()\n", |
| 66 | + " arr = random_arr_generator()\n", |
| 67 | + " max_ele = random_arr[0]\n", |
| 68 | + " for i in arr:\n", |
| 69 | + " if i>max_ele:\n", |
| 70 | + " max_ele = i\n", |
| 71 | + " time_taken = dt.now() - start\n", |
| 72 | + "# print(\"Time Taken : \"+ str(time_taken.total_seconds()*1000)+\"ms\")\n", |
| 73 | + " return max_ele\n", |
| 74 | + " " |
| 75 | + ] |
| 76 | + }, |
| 77 | + { |
| 78 | + "cell_type": "code", |
| 79 | + "execution_count": 93, |
| 80 | + "metadata": {}, |
| 81 | + "outputs": [ |
| 82 | + { |
| 83 | + "data": { |
| 84 | + "text/plain": [ |
| 85 | + "2146623331" |
| 86 | + ] |
| 87 | + }, |
| 88 | + "execution_count": 93, |
| 89 | + "metadata": {}, |
| 90 | + "output_type": "execute_result" |
| 91 | + } |
| 92 | + ], |
| 93 | + "source": [ |
| 94 | + "# Function Call\n", |
| 95 | + "\n", |
| 96 | + "Max_element()" |
| 97 | + ] |
| 98 | + }, |
| 99 | + { |
| 100 | + "cell_type": "code", |
| 101 | + "execution_count": 94, |
| 102 | + "metadata": {}, |
| 103 | + "outputs": [ |
| 104 | + { |
| 105 | + "name": "stdout", |
| 106 | + "output_type": "stream", |
| 107 | + "text": [ |
| 108 | + "2.68 ms ± 669 µs per loop (mean ± std. dev. of 7 runs, 50 loops each)\n" |
| 109 | + ] |
| 110 | + } |
| 111 | + ], |
| 112 | + "source": [ |
| 113 | + "%%timeit -n 50\n", |
| 114 | + "\n", |
| 115 | + "# timeit module is used to find the average time for execution\n", |
| 116 | + "# -n argument is used to give the number of times the following lines to execute\n", |
| 117 | + "\n", |
| 118 | + "\n", |
| 119 | + "Max_element()" |
| 120 | + ] |
| 121 | + }, |
| 122 | + { |
| 123 | + "cell_type": "code", |
| 124 | + "execution_count": 91, |
| 125 | + "metadata": {}, |
| 126 | + "outputs": [ |
| 127 | + { |
| 128 | + "name": "stdout", |
| 129 | + "output_type": "stream", |
| 130 | + "text": [ |
| 131 | + "Time Taken : 2.1679999999999997ms\n" |
| 132 | + ] |
| 133 | + }, |
| 134 | + { |
| 135 | + "data": { |
| 136 | + "text/plain": [ |
| 137 | + "2143704702" |
| 138 | + ] |
| 139 | + }, |
| 140 | + "execution_count": 91, |
| 141 | + "metadata": {}, |
| 142 | + "output_type": "execute_result" |
| 143 | + } |
| 144 | + ], |
| 145 | + "source": [ |
| 146 | + "# Used the commented line to print time taken for one execution\n", |
| 147 | + "\n", |
| 148 | + "Max_element()" |
| 149 | + ] |
| 150 | + } |
| 151 | + ], |
| 152 | + "metadata": { |
| 153 | + "kernelspec": { |
| 154 | + "display_name": "Python 3", |
| 155 | + "language": "python", |
| 156 | + "name": "python3" |
| 157 | + }, |
| 158 | + "language_info": { |
| 159 | + "codemirror_mode": { |
| 160 | + "name": "ipython", |
| 161 | + "version": 3 |
| 162 | + }, |
| 163 | + "file_extension": ".py", |
| 164 | + "mimetype": "text/x-python", |
| 165 | + "name": "python", |
| 166 | + "nbconvert_exporter": "python", |
| 167 | + "pygments_lexer": "ipython3", |
| 168 | + "version": "3.7.6" |
| 169 | + } |
| 170 | + }, |
| 171 | + "nbformat": 4, |
| 172 | + "nbformat_minor": 4 |
| 173 | +} |
0 commit comments