| 29 | |
| 30 | // Merging of two sub array |
| 31 | function mergeArray(start, end) { |
| 32 | let mid = parseInt((start + end) >> 1); |
| 33 | let start1 = start, start2 = mid + 1 |
| 34 | let end1 = mid, end2 = end |
| 35 | |
| 36 | // Initial index of merged subarray |
| 37 | let index = start |
| 38 | |
| 39 | while (start1 <= end1 && start2 <= end2) { |
| 40 | if (arr[start1] <= arr[start2]) { |
| 41 | itmd[index] = arr[start1] |
| 42 | index = index + 1 |
| 43 | start1 = start1 + 1; |
| 44 | } |
| 45 | else if(arr[start1] > arr[start2]) { |
| 46 | itmd[index] = arr[start2] |
| 47 | index = index + 1 |
| 48 | start2 = start2 + 1; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // Copy the remaining elements of |
| 53 | // arr[], if there are any |
| 54 | while (start1 <= end1) { |
| 55 | itmd[index] = arr[start1] |
| 56 | index = index + 1 |
| 57 | start1 = start1 + 1; |
| 58 | } |
| 59 | |
| 60 | while (start2 <= end2) { |
| 61 | itmd[index] = arr[start2] |
| 62 | index = index + 1 |
| 63 | start2 = start2 + 1; |
| 64 | } |
| 65 | |
| 66 | index = start |
| 67 | while (index <= end) { |
| 68 | arr[index] = itmd[index]; |
| 69 | index++; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // Function for showing visualization |
| 74 | // effect |