(a)
| 3 | # Iterative mergesort function to |
| 4 | # sort arr[0...n-1] |
| 5 | def mergeSort(a): |
| 6 | current_size = 1 |
| 7 | |
| 8 | # Outer loop for traversing Each |
| 9 | # sub array of current_size |
| 10 | while current_size < len(a) - 1: |
| 11 | left = 0 |
| 12 | # Inner loop for merge call |
| 13 | # in a sub array |
| 14 | # Each complete Iteration sorts |
| 15 | # the iterating sub array |
| 16 | while left < len(a) - 1: |
| 17 | # mid index = left index of |
| 18 | # sub array + current sub |
| 19 | # array size - 1 |
| 20 | mid = min((left + current_size - 1), (len(a) - 1)) |
| 21 | |
| 22 | # (False result,True result) |
| 23 | # [Condition] Can use current_size |
| 24 | # if 2 * current_size < len(a)-1 |
| 25 | # else len(a)-1 |
| 26 | right = (2 * current_size + left - 1, len(a) - 1)[ |
| 27 | 2 * current_size + left - 1 > len(a) - 1 |
| 28 | ] |
| 29 | |
| 30 | # Merge call for each sub array |
| 31 | merge(a, left, mid, right) |
| 32 | left = left + current_size * 2 |
| 33 | |
| 34 | # Increasing sub array size by |
| 35 | # multiple of 2 |
| 36 | current_size = 2 * current_size |
| 37 | |
| 38 | |
| 39 | # Merge Function |
no test coverage detected