|
| 1 | +# Bubble Sort Documentation |
| 2 | + |
| 3 | + |
| 4 | +In Bubble Sort, sorting takes place by *stepping through all the elements one-by-one and comparing it with the adjacent element and swapping them if required*. With each iteration the largest element in the given array, is shifted towards the last place or the highest index in the array. |
| 5 | + |
| 6 | +## Steps in Bubble Sort |
| 7 | + 1. Starting with the first element(index = 0), compare the current element with the next element of the array. |
| 8 | + 2. If the current element is greater than the next element of the array, swap them. |
| 9 | + 3. If the current element is less than the next element, move to the next element. Repeat Step 1. |
| 10 | + |
| 11 | + |
| 12 | +It includes **two loops** : |
| 13 | + |
| 14 | +1. Outer loop decides the number of traversals the algorithm makes over the array i.e. $n-1$. |
| 15 | + |
| 16 | +2. Inner loop decides upto where the algorithm parses the array in each traversal i.e. $n-i-1$, where $i$ is the iterator for the outer loop. |
| 17 | + |
| 18 | +In nth traversal, the nth largest element is pushed to the right side of the array while comparing two elements at a time. |
| 19 | + |
| 20 | +## Time Complexity |
| 21 | + |
| 22 | +Bubble Sort makes $n-1$ comparisons will be done in the first traversal, $n-2$ in second traversal, $n-3$ in third traversal and so on. So the total number of comparisons will be, |
| 23 | +$$ |
| 24 | + (n-1) + (n-2) + (n-3) + ..... + 1 = (n(n+1))/2 = O(n^2) |
| 25 | +$$ |
| 26 | +Hence the time complexity of Bubble Sort is $O(n^2)$. |
| 27 | + |
| 28 | +The main advantage of Bubble Sort is the simplicity of the algorithm. The space complexity for Bubble Sort is $O(1)$, because only a single additional memory space is required i.e. for temp variable. |
| 29 | + |
| 30 | +Also, the best case time complexity will be $O(n)$, it is when the |
| 31 | +list is already sorted.Following are the Time and Space complexity for the Bubble Sort |
| 32 | +algorithm. |
| 33 | + |
| 34 | + - Worst Case Time Complexity [ Big-O ]: $O(n^2 )$ |
| 35 | + - Best Case Time Complexity [Big-omega]: $O(n)$ |
| 36 | + - Average Time Complexity [Big-theta]: $O(n^2 )$ |
| 37 | + - Space Complexity: $O(1)$ |
| 38 | + |
| 39 | +### Dry run of the algorithm : |
| 40 | + |
| 41 | +Let $arr = [3,5,2,4,0,1]$ |
| 42 | + |
| 43 | +***Traversal i = 0 :*** |
| 44 | +**Step j = 0 :** |
| 45 | + |
| 46 | +$arr[j] = 3, arr[j+1] = 5$ |
| 47 | +$arr[j] >arr[j+1] => False$ |
| 48 | +*Not Swapped!* |
| 49 | +**Final Array** = $3,5,2,4,0,1$ |
| 50 | + |
| 51 | + |
| 52 | +**Traversal i = 0 :** |
| 53 | +**Step j = 0 :** |
| 54 | + |
| 55 | +$arr[j] = 3, arr[j+1] = 5$ |
| 56 | +$arr[j] >arr[j+1] => False$ |
| 57 | +*Not Swapped!* |
| 58 | +**Final Array** = $3,5,2,4,0,1$ |
| 59 | + |
| 60 | +**Step j = 1 :** |
| 61 | + |
| 62 | +$arr[j] = 5, arr[j+1] = 2$ |
| 63 | +$arr[j]>arr[j+1] => True$ |
| 64 | +*Swapped!* |
| 65 | +**Final Array** = $3,2,5,4,0,1$ |
| 66 | + |
| 67 | +**Step j = 2 :** |
| 68 | + |
| 69 | +$arr[j] = 5, arr[j+1] = 4$ |
| 70 | +$arr[j]>arr[j+1] => True$ |
| 71 | +*Swapped!* |
| 72 | +**Final Array** = $3,2,4,5,0,1$ |
| 73 | + |
| 74 | +**Step j = 3 :** |
| 75 | + |
| 76 | +$arr[j] = 5, arr[j+1] = 0$ |
| 77 | +$arr[j]>arr[j+1] => True$ |
| 78 | +*Swapped!* |
| 79 | +**Final Array** = $3,2,4,0,5,1$ |
| 80 | + |
| 81 | +**Step j = 4 :** |
| 82 | + |
| 83 | +$arr[j] = 5, arr[j+1] = 1$ |
| 84 | +$arr[j]>arr[j+1] => True$ |
| 85 | +*Swapped!* |
| 86 | +**Final Array** = $3,2,4,0,1,5$ |
| 87 | + |
| 88 | +**Traversal i = 1 :** |
| 89 | +**Step j = 0 :** |
| 90 | + |
| 91 | +$arr[j] = 3 , arr[j+1] = 2$ |
| 92 | +$arr[j]>arr[j+1] => True$ |
| 93 | +*Swapped!* |
| 94 | +**Final Array** = $2,3,4,0,1,5$ |
| 95 | + |
| 96 | +**Step j = 1 :** |
| 97 | + |
| 98 | +$arr[j] = 3, arr[j+1] = 4$ |
| 99 | +$arr[j]>arr[j+1] => False$ |
| 100 | +*Not Swapped!* |
| 101 | +**Final Array** = $2,3,4,0,1,5$ |
| 102 | + |
| 103 | +**Step j = 2 :** |
| 104 | + |
| 105 | +$arr[j] = 4, arr[j+1] = 0$ |
| 106 | +$arr[j]>arr[j+1] => True$ |
| 107 | +*Swapped!* |
| 108 | +**Final Array** = $2,3,0,4,1,5$ |
| 109 | + |
| 110 | +**Step j = 3 :** |
| 111 | + |
| 112 | +$arr[j] = 4, arr[j+1] = 1$ |
| 113 | +$arr[j]>arr[j+1] => True$ |
| 114 | +*Swapped!* |
| 115 | +**Final Array** = $2,3,0,1,4,5$ |
| 116 | + |
| 117 | +**Traversal i = 2 :** |
| 118 | + |
| 119 | +**Step j = 0 :** |
| 120 | + |
| 121 | +$arr[j] = 2, arr[j+1] = 3$ |
| 122 | +$arr[j]>arr[j+1] => False$ |
| 123 | +*Not Swapped!* |
| 124 | +**Final Array** = $2,3,0,1,4,5$ |
| 125 | + |
| 126 | +**Step j = 1 :** |
| 127 | + |
| 128 | +$arr[j] = 3, arr[j+1] = 0$ |
| 129 | +$arr[j]>arr[j+1] => True$ |
| 130 | +*Swapped!* |
| 131 | +**Final Array** = $2,0,3,1,4,5$ |
| 132 | + |
| 133 | +**Step j = 2 :** |
| 134 | + |
| 135 | +$arr[j] = 3, arr[j+1] = 1$ |
| 136 | +$arr[j]>arr[j+1] => True$ |
| 137 | +*Swapped!* |
| 138 | +**Final Array** = $2,0,1,3,4,5$ |
| 139 | + |
| 140 | +**Traversal i = 3:** |
| 141 | + |
| 142 | +**Step j = 0 :** |
| 143 | + |
| 144 | +$arr[j] = 2, arr[j+1] = 0$ |
| 145 | +$arr[j]>arr[j+1] => True$ |
| 146 | +*Swapped!* |
| 147 | +**Final Array** = $0,2,1,3,4,5$ |
| 148 | + |
| 149 | +**Step j = 1 :** |
| 150 | + |
| 151 | +$arr[j] = 2, arr[j+1] = 1$ |
| 152 | +$arr[j]>arr[j+1] => True$ |
| 153 | +*Swapped!* |
| 154 | +**Final Array** = $0,1,2,3,4,5$ |
| 155 | + |
| 156 | +**Traversal i = 4 :** |
| 157 | + |
| 158 | +**Step j = 0 :** |
| 159 | + |
| 160 | +$arr[j] = 0, arr[j+1] = 1$ |
| 161 | +$arr[j]>arr[j+1] => False$ |
| 162 | +*Not Swapped!* |
| 163 | +**Final Array** = $0,1,2,3,4,5$ |
| 164 | + |
| 165 | +**Traversal i = 5 :** |
| 166 | + |
| 167 | +For $i = 5$, |
| 168 | +Inner loop doesn't execute even once i.e. |
| 169 | +$j\ in\ range (0,-1) => False$ |
| 170 | +Thus the sorted array is returned. |
| 171 | + |
| 172 | +## Optimised Bubble Sort |
| 173 | + |
| 174 | +In Optimized Bubble Sort, sorting takes place in the same way as in Bubble Sort except the fact that *if no swaps take place in any iteration the outer loop breaks and the sorted array is returned.* |
| 175 | + |
| 176 | +In the above example itself, if we add the flag variable `swapped` it will be false after the 4th iteration and sorted array will be returned after four interations only. |
| 177 | + |
| 178 | + |
| 179 | +### Dry run of the algorithm after optimization : |
| 180 | + |
| 181 | +$arr = [3,5,2,4,0,1]$ |
| 182 | + |
| 183 | + |
| 184 | +**Traversal i = 0 :** |
| 185 | +**Step j = 0 :** |
| 186 | + |
| 187 | +$arr[j] = 3, arr[j+1] = 5$ |
| 188 | +$arr[j]>arr[j+1] => False$ |
| 189 | +*Not Swapped!* |
| 190 | +`Swapped` $= False$ |
| 191 | +**Final Array** = 3,5,2,4,0,1 |
| 192 | + |
| 193 | +**Step j = 1 :** |
| 194 | + |
| 195 | +$arr[j] = 5, arr[j+1] = 2$ |
| 196 | +$arr[j]>arr[j+1] => True$ |
| 197 | +*Swapped!* |
| 198 | +`Swapped` $= True$ |
| 199 | +**Final Array** = $3,2,5,4,0,1$ |
| 200 | + |
| 201 | +**Step j = 2 :** |
| 202 | + |
| 203 | +$arr[j] = 5, arr[j+1] = 4$ |
| 204 | +$arr[j]>arr[j+1] => True$ |
| 205 | +*Swapped!* |
| 206 | +`Swapped` $= True$ |
| 207 | +**Final Array** = $3,2,4,5,0,1$ |
| 208 | + |
| 209 | +**Step j = 3 :** |
| 210 | + |
| 211 | +$arr[j] = 5, arr[j+1] = 0$ |
| 212 | +$arr[j]>arr[j+1] => True$ |
| 213 | +*Swapped!* |
| 214 | +`Swapped` $= True$ |
| 215 | +**Final Array** = $3,2,4,0,5,1$ |
| 216 | + |
| 217 | +**Step j = 4 :** |
| 218 | + |
| 219 | +$arr[j] = 5, arr[j+1] = 1$ |
| 220 | +$arr[j]>arr[j+1] => True$ |
| 221 | +*Swapped!* |
| 222 | +`Swapped` $= True$ |
| 223 | +**Final Array** = $3,2,4,0,1,5$ |
| 224 | + |
| 225 | + |
| 226 | +**Traversal i = 1 : ** |
| 227 | +**Step j = 0 :** |
| 228 | + |
| 229 | +$arr[j] = 3 , arr[j+1] = 2$ |
| 230 | +$arr[j]>arr[j+1] => True$ |
| 231 | +*Swapped!* |
| 232 | +`Swapped` $= True$ |
| 233 | +**Final Array** = $2,3,4,0,1,5$ |
| 234 | + |
| 235 | +**Step j = 1 :** |
| 236 | + |
| 237 | +$arr[j] = 3, arr[j+1] = 4$ |
| 238 | +$arr[j]>arr[j+1] => False$ |
| 239 | +*Not Swapped!* |
| 240 | +`Swapped` $= True$ |
| 241 | +**Final Array** = $2,3,4,0,1,5$ |
| 242 | + |
| 243 | + |
| 244 | +**Step j = 2 :** |
| 245 | + |
| 246 | +$arr[j] = 4, arr[j+1] = 0$ |
| 247 | +$arr[j]>arr[j+1] => True$ |
| 248 | +*Swapped!* |
| 249 | +`Swapped` $= True$ |
| 250 | +**Final Array** = $2,3,0,4,1,5$ |
| 251 | + |
| 252 | +**Step j = 3 :** |
| 253 | + |
| 254 | +$arr[j] = 4, arr[j+1] = 1$ |
| 255 | +$arr[j]>arr[j+1] => True$ |
| 256 | +*Swapped!* |
| 257 | +`Swapped` $= True$ |
| 258 | +**Final Array** = $2,3,0,1,4,5$ |
| 259 | + |
| 260 | +**Traversal i = 2 :** |
| 261 | + |
| 262 | +**Step j = 0 :** |
| 263 | + |
| 264 | +$arr[j] = 2, arr[j+1] = 3$ |
| 265 | +$arr[j]>arr[j+1] => False$ |
| 266 | +*Not Swapped!* |
| 267 | +`Swapped` $= False$ |
| 268 | +**Final Array** = $2,3,0,1,4,5$ |
| 269 | + |
| 270 | +**Step j = 1 :** |
| 271 | + |
| 272 | +$arr[j] = 3, arr[j+1] = 0$ |
| 273 | +$arr[j]>arr[j+1] => True$ |
| 274 | +*Swapped!* |
| 275 | +`Swapped` $= True$ |
| 276 | +**Final Array** = $2,0,3,1,4,5$ |
| 277 | + |
| 278 | +**Step j = 2 :** |
| 279 | + |
| 280 | +$arr[j] = 3, arr[j+1] = 1$ |
| 281 | +$arr[j]>arr[j+1] => True$ |
| 282 | +*Swapped!* |
| 283 | +`Swapped` $= True$ |
| 284 | +**Final Array** = $2,0,1,3,4,5$ |
| 285 | + |
| 286 | + |
| 287 | +**Step j = 0 :** |
| 288 | + |
| 289 | +$arr[j] = 3, arr[j+1] = 5$ |
| 290 | +$arr[j]>arr[j+1] => False$ |
| 291 | +*Not Swapped!* |
| 292 | +`Swapped` $= False$ |
| 293 | +**Final Array** = 3,5,2,4,0,1 |
| 294 | + |
| 295 | +**Step j = 1 :** |
| 296 | + |
| 297 | +$arr[j] = 5, arr[j+1] = 2$ |
| 298 | +$arr[j]>arr[j+1] => True$ |
| 299 | +*Swapped!* |
| 300 | +`Swapped` $= True$ |
| 301 | +**Final Array** = $3,2,5,4,0,1$ |
| 302 | + |
| 303 | +**Step j = 2 :** |
| 304 | + |
| 305 | +$arr[j] = 5, arr[j+1] = 4$ |
| 306 | +$arr[j]>arr[j+1] => True$ |
| 307 | +*Swapped!* |
| 308 | +`Swapped` $= True$ |
| 309 | +**Final Array** = $3,2,4,5,0,1$ |
| 310 | + |
| 311 | +**Step j = 3 :** |
| 312 | + |
| 313 | +$arr[j] = 5, arr[j+1] = 0$ |
| 314 | +$arr[j]>arr[j+1] => True$ |
| 315 | +*Swapped!* |
| 316 | +`Swapped` $= True$ |
| 317 | +**Final Array** = $3,2,4,0,5,1$ |
| 318 | + |
| 319 | +**Step j = 4 :** |
| 320 | + |
| 321 | +$arr[j] = 5, arr[j+1] = 1$ |
| 322 | +$arr[j]>arr[j+1] => True$ |
| 323 | +*Swapped!* |
| 324 | +`Swapped` $= True$ |
| 325 | +**Final Array** = $3,2,4,0,1,5$ |
| 326 | + |
| 327 | + |
| 328 | +**Traversal i = 1 : ** |
| 329 | +**Step j = 0 :** |
| 330 | + |
| 331 | +$arr[j] = 3 , arr[j+1] = 2$ |
| 332 | +$arr[j]>arr[j+1] => True$ |
| 333 | +*Swapped!* |
| 334 | +`Swapped` $= True$ |
| 335 | +**Final Array** = $2,3,4,0,1,5$ |
| 336 | + |
| 337 | +**Traversal i = 3:** |
| 338 | + |
| 339 | +**Step j = 0 :** |
| 340 | + |
| 341 | +$arr[j] = 2, arr[j+1] = 0$ |
| 342 | +$arr[j]>arr[j+1] => True$ |
| 343 | +*Swapped!* |
| 344 | +`Swapped` $= True$ |
| 345 | +**Final Array** = $0,2,1,3,4,5$ |
| 346 | + |
| 347 | +**Step j = 1 :** |
| 348 | + |
| 349 | +$arr[j] = 2, arr[j+1] = 1$ |
| 350 | +$arr[j]>arr[j+1] => True$ |
| 351 | +*Swapped!* |
| 352 | +`Swapped` $= True$ |
| 353 | +**Final Array** = $0,1,2,3,4,5$ |
| 354 | + |
| 355 | +**Traversal i = 4 :** |
| 356 | + |
| 357 | +**Step j = 0 :** |
| 358 | + |
| 359 | +$arr[j] = 0, arr[j+1] = 1$ |
| 360 | +$arr[j]>arr[j+1] => False$ |
| 361 | +*Not Swapped!* |
| 362 | +`Swapped` $= False$ |
| 363 | +**Final Array** = $0,1,2,3,4,5$ |
| 364 | + |
| 365 | +Since `Swapped` is False $=>$ Break the outer loop |
| 366 | +return sorted array = $0,1,2,3,4,5$ |
0 commit comments