| 
 | 1 | +{  | 
 | 2 | + "cells": [  | 
 | 3 | +  {  | 
 | 4 | +   "cell_type": "markdown",  | 
 | 5 | +   "metadata": {},  | 
 | 6 | +   "source": [  | 
 | 7 | +    "# Merge Sort"  | 
 | 8 | +   ]  | 
 | 9 | +  },  | 
 | 10 | +  {  | 
 | 11 | +   "cell_type": "code",  | 
 | 12 | +   "execution_count": 31,  | 
 | 13 | +   "metadata": {},  | 
 | 14 | +   "outputs": [],  | 
 | 15 | +   "source": [  | 
 | 16 | +    "def merge_sort(array):\n",  | 
 | 17 | +    "    if len(array) < 2:\n",  | 
 | 18 | +    "        return array\n",  | 
 | 19 | +    "    \n",  | 
 | 20 | +    "    mid = len(array) // 2\n",  | 
 | 21 | +    "    left = merge_sort(array[:mid])\n",  | 
 | 22 | +    "    right = merge_sort(array[mid:])\n",  | 
 | 23 | +    "    \n",  | 
 | 24 | +    "    return merge(left, right)\n",  | 
 | 25 | +    "\n",  | 
 | 26 | +    "def merge(left, right):\n",  | 
 | 27 | +    "    result = []\n",  | 
 | 28 | +    "    i, j = 0, 0\n",  | 
 | 29 | +    "    while i < len(left) or j < len(right):\n",  | 
 | 30 | +    "        if left[i] <= right[j]:\n",  | 
 | 31 | +    "            result.append(left[i])\n",  | 
 | 32 | +    "            i += 1\n",  | 
 | 33 | +    "        else:\n",  | 
 | 34 | +    "            result.append(right[j])\n",  | 
 | 35 | +    "            j += 1\n",  | 
 | 36 | +    "        if i == len(left) or j == len(right):\n",  | 
 | 37 | +    "            result.extend(left[i:] or right[j:])\n",  | 
 | 38 | +    "            break\n",  | 
 | 39 | +    "    \n",  | 
 | 40 | +    "    return result"  | 
 | 41 | +   ]  | 
 | 42 | +  },  | 
 | 43 | +  {  | 
 | 44 | +   "cell_type": "markdown",  | 
 | 45 | +   "metadata": {},  | 
 | 46 | +   "source": [  | 
 | 47 | +    "\n",  | 
 | 48 | +    "### Time Complexity:\n",  | 
 | 49 | +    "\n",  | 
 | 50 | +    "- Best Case: O(n log2(n))\n",  | 
 | 51 | +    "- Average Case: O(n log2(n))\n",  | 
 | 52 | +    "- Worst Case:  O(n log2(n))"  | 
 | 53 | +   ]  | 
 | 54 | +  },  | 
 | 55 | +  {  | 
 | 56 | +   "cell_type": "markdown",  | 
 | 57 | +   "metadata": {},  | 
 | 58 | +   "source": [  | 
 | 59 | +    "### Why O(n log n) ?"  | 
 | 60 | +   ]  | 
 | 61 | +  },  | 
 | 62 | +  {  | 
 | 63 | +   "cell_type": "markdown",  | 
 | 64 | +   "metadata": {},  | 
 | 65 | +   "source": [  | 
 | 66 | +    "If you are given two sorted arrays(say A & B) of length n/2 then it will take O(n) time to merge and make a sorted array of length n.\n",  | 
 | 67 | +    "\n",  | 
 | 68 | +    "But if A and B are not sorted then we need to sort them first. For this we first divide array A and B of length n/2 each into two arrays of length n/4 and suppose these two arrays are already sorted.\n",  | 
 | 69 | +    "\n",  | 
 | 70 | +    "Now to merge two sorted array of length n/4 to make array A of length n/2 will take O(n/2) time and similarly array B formation will also take O(n/2) time.\n",  | 
 | 71 | +    "\n",  | 
 | 72 | +    "So total time to make array A and B both also took O(n). So at every stage it is taking O(n) time. So the total time for merge sort will be O(no. of stages * n).\n",  | 
 | 73 | +    "\n",  | 
 | 74 | +    "Here we are dividing array into two parts at every stage and we will continue dividing untill length of two divided array is one.\n",  | 
 | 75 | +    "\n",  | 
 | 76 | +    "So if length of array is eight then we need to divide it three times to get arrays of length one like this\n",  | 
 | 77 | +    "\n",  | 
 | 78 | +    "8 = 4+4 = 2+2+2+2 = 1+1+1+1+1+1+1+1\n",  | 
 | 79 | +    "\n",  | 
 | 80 | +    "So\n",  | 
 | 81 | +    "\n",  | 
 | 82 | +    "no. of stages = log2(8) = 3\n",  | 
 | 83 | +    "\n",  | 
 | 84 | +    "That is why merge sort is O(nlog(n)) with log2(n) iteration.\n"  | 
 | 85 | +   ]  | 
 | 86 | +  },  | 
 | 87 | +  {  | 
 | 88 | +   "cell_type": "markdown",  | 
 | 89 | +   "metadata": {},  | 
 | 90 | +   "source": [  | 
 | 91 | +    "## Code for executing and seeing the difference in time complexities"  | 
 | 92 | +   ]  | 
 | 93 | +  },  | 
 | 94 | +  {  | 
 | 95 | +   "cell_type": "markdown",  | 
 | 96 | +   "metadata": {},  | 
 | 97 | +   "source": [  | 
 | 98 | +    "### Best Case Performance:"  | 
 | 99 | +   ]  | 
 | 100 | +  },  | 
 | 101 | +  {  | 
 | 102 | +   "cell_type": "code",  | 
 | 103 | +   "execution_count": 32,  | 
 | 104 | +   "metadata": {},  | 
 | 105 | +   "outputs": [  | 
 | 106 | +    {  | 
 | 107 | +     "name": "stdout",  | 
 | 108 | +     "output_type": "stream",  | 
 | 109 | +     "text": [  | 
 | 110 | +      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]\n",  | 
 | 111 | +      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]\n"  | 
 | 112 | +     ]  | 
 | 113 | +    }  | 
 | 114 | +   ],  | 
 | 115 | +   "source": [  | 
 | 116 | +    "# elements are already sorted\n",  | 
 | 117 | +    "array = [i for i in range(1, 20)]\n",  | 
 | 118 | +    "\n",  | 
 | 119 | +    "print(array)\n",  | 
 | 120 | +    "# 20 ALREADY sorted elements need 18 iterations approx = n\n",  | 
 | 121 | +    "print(merge_sort(array))"  | 
 | 122 | +   ]  | 
 | 123 | +  },  | 
 | 124 | +  {  | 
 | 125 | +   "cell_type": "markdown",  | 
 | 126 | +   "metadata": {},  | 
 | 127 | +   "source": [  | 
 | 128 | +    "### Average Case Performance:"  | 
 | 129 | +   ]  | 
 | 130 | +  },  | 
 | 131 | +  {  | 
 | 132 | +   "cell_type": "code",  | 
 | 133 | +   "execution_count": 33,  | 
 | 134 | +   "metadata": {},  | 
 | 135 | +   "outputs": [  | 
 | 136 | +    {  | 
 | 137 | +     "name": "stdout",  | 
 | 138 | +     "output_type": "stream",  | 
 | 139 | +     "text": [  | 
 | 140 | +      "[5, 2, 17, 15, 3, 13, 9, 12, 7, 19, 11, 18, 14, 10, 1, 16, 4, 8, 6]\n",  | 
 | 141 | +      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]\n"  | 
 | 142 | +     ]  | 
 | 143 | +    }  | 
 | 144 | +   ],  | 
 | 145 | +   "source": [  | 
 | 146 | +    "import random\n",  | 
 | 147 | +    "# elements are randomly shuffled\n",  | 
 | 148 | +    "array = [i for i in range(1, 20)]\n",  | 
 | 149 | +    "random.shuffle(array)\n",  | 
 | 150 | +    "print(array)\n",  | 
 | 151 | +    "# 20 shuffled elements need 324 iterations approx = n * n\n",  | 
 | 152 | +    "print(merge_sort(array))"  | 
 | 153 | +   ]  | 
 | 154 | +  },  | 
 | 155 | +  {  | 
 | 156 | +   "cell_type": "markdown",  | 
 | 157 | +   "metadata": {},  | 
 | 158 | +   "source": [  | 
 | 159 | +    "### Worst Case Performance:"  | 
 | 160 | +   ]  | 
 | 161 | +  },  | 
 | 162 | +  {  | 
 | 163 | +   "cell_type": "code",  | 
 | 164 | +   "execution_count": 34,  | 
 | 165 | +   "metadata": {},  | 
 | 166 | +   "outputs": [  | 
 | 167 | +    {  | 
 | 168 | +     "name": "stdout",  | 
 | 169 | +     "output_type": "stream",  | 
 | 170 | +     "text": [  | 
 | 171 | +      "[19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]\n",  | 
 | 172 | +      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]\n"  | 
 | 173 | +     ]  | 
 | 174 | +    }  | 
 | 175 | +   ],  | 
 | 176 | +   "source": [  | 
 | 177 | +    "# elements are reverse sorted\n",  | 
 | 178 | +    "array = [i for i in range(1, 20)]\n",  | 
 | 179 | +    "# reversing the array\n",  | 
 | 180 | +    "array = array[::-1]\n",  | 
 | 181 | +    "\n",  | 
 | 182 | +    "print(array)\n",  | 
 | 183 | +    "# 20 REVERSE sorted elements need 324 iterations approx = n * n\n",  | 
 | 184 | +    "print(merge_sort(array))"  | 
 | 185 | +   ]  | 
 | 186 | +  }  | 
 | 187 | + ],  | 
 | 188 | + "metadata": {  | 
 | 189 | +  "kernelspec": {  | 
 | 190 | +   "display_name": "Python 3",  | 
 | 191 | +   "language": "python",  | 
 | 192 | +   "name": "python3"  | 
 | 193 | +  },  | 
 | 194 | +  "language_info": {  | 
 | 195 | +   "codemirror_mode": {  | 
 | 196 | +    "name": "ipython",  | 
 | 197 | +    "version": 3  | 
 | 198 | +   },  | 
 | 199 | +   "file_extension": ".py",  | 
 | 200 | +   "mimetype": "text/x-python",  | 
 | 201 | +   "name": "python",  | 
 | 202 | +   "nbconvert_exporter": "python",  | 
 | 203 | +   "pygments_lexer": "ipython3",  | 
 | 204 | +   "version": "3.5.2"  | 
 | 205 | +  }  | 
 | 206 | + },  | 
 | 207 | + "nbformat": 4,  | 
 | 208 | + "nbformat_minor": 2  | 
 | 209 | +}  | 
0 commit comments