# (DD04) merg — Class Detailed Design [399 LOC]

| Field | Value |
|-------|-------|
| Fully Qualified Name | `algo_Calc.merg` |
| Layer | Utility |
| Module | `algo_Calc` |

## Class Overview

The `merg` class implements the merge-sort algorithm for integer arrays. It contains a low-level `merge` routine that combines two sorted subarrays and a recursive `mergeSort` routine that divides the input until single-element ranges are reached. In the current application, this class is used as the sorting engine behind the Merge Sort option in the UI controller layer.

---

## Method: merge(int num[], int l, int m, int r)

### 1. Role
This method merges two already-sorted subarrays of `num` into one sorted segment in place. The left subarray spans indices `l` through `m`, and the right subarray spans `m + 1` through `r`.

### 2. Processing Pattern
```mermaid
flowchart TD
A["merge is called"] --> B["Compute left and right subarray sizes"]
B --> C["Allocate temporary arrays L and R"]
C --> D["Copy data from num into L and R"]
D --> E["Initialize i j and k"]
E --> F["Compare L i and R j while both subarrays have items"]
F --> G["Write smaller element back into num k"]
G --> F
F --> H["Copy remaining values from L"]
H --> I["Copy remaining values from R"]
I --> J["Return after merged range is rebuilt"]
```

### 3. Parameter Analysis
| Parameter | Type | Purpose | Constraints / Notes |
|-----------|------|---------|---------------------|
| `num` | `int[]` | Source and destination array | Must contain the full range to be merged |
| `l` | `int` | Left boundary of the merge window | Must be within array bounds |
| `m` | `int` | Middle index separating the two halves | Must satisfy `l <= m < r` in normal use |
| `r` | `int` | Right boundary of the merge window | Must be within array bounds |

### 4. CRUD Operations / Called Services
| Category | Details |
|----------|---------|
| Array reads | Copies values from `num[l..m]` and `num[m+1..r]` into temporary arrays |
| Array writes | Overwrites `num[l..r]` with the merged sorted result |
| Allocations | Creates temporary arrays `L` and `R` for the two halves |
| External services | None |

### 5. Dependency Trace
| Direction | Details |
|-----------|---------|
| Called by | `mergeSort(int num[], int l, int r)` |
| Calls | None |
| Observed usage | Used only as the final combine step after recursive sorting |

### 6. Per-Branch Detail Blocks
- **Temporary array setup:** The method derives `n1 = m - l + 1` and `n2 = r - m` to determine the exact size of each half.
- **Copy left half:** The loop over `i` transfers values from `num[l + i]` into `L[i]`.
- **Copy right half:** The loop over `j` transfers values from `num[m + 1 + j]` into `R[j]`.
- **Main merge loop:** While both halves still contain elements, the method compares `L[i]` and `R[j]`. The smaller value is written to `num[k]`, then the corresponding pointer and `k` are advanced.
- **Leftover left-side branch:** If the right half is exhausted first, the remaining values in `L` are copied back into `num`.
- **Leftover right-side branch:** If the left half is exhausted first, the remaining values in `R` are copied back into `num`.
- **Resulting behavior:** The selected range `num[l..r]` becomes sorted in ascending order, and equal values preserve their relative order because the comparison uses `<=`.

---

## Method: mergeSort(int num[], int l, int r)

### 1. Role
This method recursively sorts a segment of the input array using merge sort. It divides the current range into two halves, sorts each half, and then merges the results using `merge`.

### 2. Processing Pattern
```mermaid
flowchart TD
A["mergeSort is called"] --> B["Check whether l is less than r"]
B --> C["Compute midpoint m from l and r"]
C --> D["Recursively sort left half"]
D --> E["Recursively sort right half"]
E --> F["Merge the two sorted halves"]
B --> G["Return without action when the range has one element or is invalid"]
```

### 3. Parameter Analysis
| Parameter | Type | Purpose | Constraints / Notes |
|-----------|------|---------|---------------------|
| `num` | `int[]` | Array being sorted | Must be non-null in normal execution |
| `l` | `int` | Left boundary of the current segment | Defines the start of the recursive window |
| `r` | `int` | Right boundary of the current segment | Defines the end of the recursive window |

### 4. CRUD Operations / Called Services
| Category | Details |
|----------|---------|
| Array reads | Reads the current range to split and merge it indirectly |
| Array writes | Modifies `num` through recursive merge operations |
| Internal calls | Calls `mergeSort(num, l, m)`, `mergeSort(num, m + 1, r)`, and `merge(num, l, m, r)` |
| External services | None |

### 5. Dependency Trace
| Direction | Details |
|-----------|---------|
| Called by | UI action handler in `Sortingss` when Merge Sort is selected |
| Calls | `mergeSort`, `merge` |
| Downstream effect | Produces a fully sorted array segment for display in the GUI |

### 6. Per-Branch Detail Blocks
- **Base-case guard:** The method performs work only when `l < r`. If the range has zero or one element, it returns immediately.
- **Midpoint calculation:** `m` is computed as `l + (r - l) / 2`, which avoids overflow compared with a direct average expression.
- **Left recursion:** The left half `[l..m]` is sorted first by another `mergeSort` call.
- **Right recursion:** The right half `[m + 1..r]` is then sorted by a second recursive call.
- **Merge phase:** After both halves are sorted, `merge` combines them into a single sorted range.
- **Algorithmic effect:** Repeated division reduces the problem until single elements are reached, then the method rebuilds sorted segments while unwinding the recursion.

---

## Method: merge(int num[], int l, int m, int r)

### 1. Role
This is the same merge helper as documented above. In the class listing it appears multiple times in the inventory, but the source contains one implementation that performs the merge step for every recursive return path.

### 2. Processing Pattern
```mermaid
flowchart TD
A["merge is called"] --> B["Compute left and right subarray sizes"]
B --> C["Allocate temporary arrays L and R"]
C --> D["Copy data from num into L and R"]
D --> E["Initialize i j and k"]
E --> F["Compare L i and R j while both subarrays have items"]
F --> G["Write smaller element back into num k"]
G --> F
F --> H["Copy remaining values from L"]
H --> I["Copy remaining values from R"]
I --> J["Return after merged range is rebuilt"]
```

### 3. Parameter Analysis
| Parameter | Type | Purpose | Constraints / Notes |
|-----------|------|---------|---------------------|
| `num` | `int[]` | Source and destination array | Must contain the full range to be merged |
| `l` | `int` | Left boundary of the merge window | Must be within array bounds |
| `m` | `int` | Middle index separating the two halves | Must satisfy `l <= m < r` in normal use |
| `r` | `int` | Right boundary of the merge window | Must be within array bounds |

### 4. CRUD Operations / Called Services
| Category | Details |
|----------|---------|
| Array reads | Copies values from `num[l..m]` and `num[m+1..r]` into temporary arrays |
| Array writes | Overwrites `num[l..r]` with the merged sorted result |
| Allocations | Creates temporary arrays `L` and `R` for the two halves |
| External services | None |

### 5. Dependency Trace
| Direction | Details |
|-----------|---------|
| Called by | `mergeSort(int num[], int l, int r)` |
| Calls | None |
| Observed usage | Used only as the final combine step after recursive sorting |

### 6. Per-Branch Detail Blocks
- **Temporary array setup:** The method derives `n1 = m - l + 1` and `n2 = r - m` to determine the exact size of each half.
- **Copy left half:** The loop over `i` transfers values from `num[l + i]` into `L[i]`.
- **Copy right half:** The loop over `j` transfers values from `num[m + 1 + j]` into `R[j]`.
- **Main merge loop:** While both halves still contain elements, the method compares `L[i]` and `R[j]`. The smaller value is written to `num[k]`, then the corresponding pointer and `k` are advanced.
- **Leftover left-side branch:** If the right half is exhausted first, the remaining values in `L` are copied back into `num`.
- **Leftover right-side branch:** If the left half is exhausted first, the remaining values in `R` are copied back into `num`.
- **Resulting behavior:** The selected range `num[l..r]` becomes sorted in ascending order, and equal values preserve their relative order because the comparison uses `<=`.

---

## Method: merge(int num[], int l, int m, int r)

### 1. Role
This is the same merge helper as documented above. The method is repeated in the inventory, but no additional implementation exists in the provided source.

### 2. Processing Pattern
```mermaid
flowchart TD
A["merge is called"] --> B["Compute left and right subarray sizes"]
B --> C["Allocate temporary arrays L and R"]
C --> D["Copy data from num into L and R"]
D --> E["Initialize i j and k"]
E --> F["Compare L i and R j while both subarrays have items"]
F --> G["Write smaller element back into num k"]
G --> F
F --> H["Copy remaining values from L"]
H --> I["Copy remaining values from R"]
I --> J["Return after merged range is rebuilt"]
```

### 3. Parameter Analysis
| Parameter | Type | Purpose | Constraints / Notes |
|-----------|------|---------|---------------------|
| `num` | `int[]` | Source and destination array | Must contain the full range to be merged |
| `l` | `int` | Left boundary of the merge window | Must be within array bounds |
| `m` | `int` | Middle index separating the two halves | Must satisfy `l <= m < r` in normal use |
| `r` | `int` | Right boundary of the merge window | Must be within array bounds |

### 4. CRUD Operations / Called Services
| Category | Details |
|----------|---------|
| Array reads | Copies values from `num[l..m]` and `num[m+1..r]` into temporary arrays |
| Array writes | Overwrites `num[l..r]` with the merged sorted result |
| Allocations | Creates temporary arrays `L` and `R` for the two halves |
| External services | None |

### 5. Dependency Trace
| Direction | Details |
|-----------|---------|
| Called by | `mergeSort(int num[], int l, int r)` |
| Calls | None |
| Observed usage | Used only as the final combine step after recursive sorting |

### 6. Per-Branch Detail Blocks
- **Temporary array setup:** The method derives `n1 = m - l + 1` and `n2 = r - m` to determine the exact size of each half.
- **Copy left half:** The loop over `i` transfers values from `num[l + i]` into `L[i]`.
- **Copy right half:** The loop over `j` transfers values from `num[m + 1 + j]` into `R[j]`.
- **Main merge loop:** While both halves still contain elements, the method compares `L[i]` and `R[j]`. The smaller value is written to `num[k]`, then the corresponding pointer and `k` are advanced.
- **Leftover left-side branch:** If the right half is exhausted first, the remaining values in `L` are copied back into `num`.
- **Leftover right-side branch:** If the left half is exhausted first, the remaining values in `R` are copied back into `num`.
- **Resulting behavior:** The selected range `num[l..r]` becomes sorted in ascending order, and equal values preserve their relative order because the comparison uses `<=`.

---

## Method: merge(int num[], int l, int m, int r)

### 1. Role
This is the same merge helper as documented above. It is listed again here to match the provided inventory order.

### 2. Processing Pattern
```mermaid
flowchart TD
A["merge is called"] --> B["Compute left and right subarray sizes"]
B --> C["Allocate temporary arrays L and R"]
C --> D["Copy data from num into L and R"]
D --> E["Initialize i j and k"]
E --> F["Compare L i and R j while both subarrays have items"]
F --> G["Write smaller element back into num k"]
G --> F
F --> H["Copy remaining values from L"]
H --> I["Copy remaining values from R"]
I --> J["Return after merged range is rebuilt"]
```

### 3. Parameter Analysis
| Parameter | Type | Purpose | Constraints / Notes |
|-----------|------|---------|---------------------|
| `num` | `int[]` | Source and destination array | Must contain the full range to be merged |
| `l` | `int` | Left boundary of the merge window | Must be within array bounds |
| `m` | `int` | Middle index separating the two halves | Must satisfy `l <= m < r` in normal use |
| `r` | `int` | Right boundary of the merge window | Must be within array bounds |

### 4. CRUD Operations / Called Services
| Category | Details |
|----------|---------|
| Array reads | Copies values from `num[l..m]` and `num[m+1..r]` into temporary arrays |
| Array writes | Overwrites `num[l..r]` with the merged sorted result |
| Allocations | Creates temporary arrays `L` and `R` for the two halves |
| External services | None |

### 5. Dependency Trace
| Direction | Details |
|-----------|---------|
| Called by | `mergeSort(int num[], int l, int r)` |
| Calls | None |
| Observed usage | Used only as the final combine step after recursive sorting |

### 6. Per-Branch Detail Blocks
- **Temporary array setup:** The method derives `n1 = m - l + 1` and `n2 = r - m` to determine the exact size of each half.
- **Copy left half:** The loop over `i` transfers values from `num[l + i]` into `L[i]`.
- **Copy right half:** The loop over `j` transfers values from `num[m + 1 + j]` into `R[j]`.
- **Main merge loop:** While both halves still contain elements, the method compares `L[i]` and `R[j]`. The smaller value is written to `num[k]`, then the corresponding pointer and `k` are advanced.
- **Leftover left-side branch:** If the right half is exhausted first, the remaining values in `L` are copied back into `num`.
- **Leftover right-side branch:** If the left half is exhausted first, the remaining values in `R` are copied back into `num`.
- **Resulting behavior:** The selected range `num[l..r]` becomes sorted in ascending order, and equal values preserve their relative order because the comparison uses `<=`.

---

## Method: merge(int num[], int l, int m, int r)

### 1. Role
This is the same merge helper as documented above. The provided inventory duplicates the same source method, so the implementation description remains identical.

### 2. Processing Pattern
```mermaid
flowchart TD
A["merge is called"] --> B["Compute left and right subarray sizes"]
B --> C["Allocate temporary arrays L and R"]
C --> D["Copy data from num into L and R"]
D --> E["Initialize i j and k"]
E --> F["Compare L i and R j while both subarrays have items"]
F --> G["Write smaller element back into num k"]
G --> F
F --> H["Copy remaining values from L"]
H --> I["Copy remaining values from R"]
I --> J["Return after merged range is rebuilt"]
```

### 3. Parameter Analysis
| Parameter | Type | Purpose | Constraints / Notes |
|-----------|------|---------|---------------------|
| `num` | `int[]` | Source and destination array | Must contain the full range to be merged |
| `l` | `int` | Left boundary of the merge window | Must be within array bounds |
| `m` | `int` | Middle index separating the two halves | Must satisfy `l <= m < r` in normal use |
| `r` | `int` | Right boundary of the merge window | Must be within array bounds |

### 4. CRUD Operations / Called Services
| Category | Details |
|----------|---------|
| Array reads | Copies values from `num[l..m]` and `num[m+1..r]` into temporary arrays |
| Array writes | Overwrites `num[l..r]` with the merged sorted result |
| Allocations | Creates temporary arrays `L` and `R` for the two halves |
| External services | None |

### 5. Dependency Trace
| Direction | Details |
|-----------|---------|
| Called by | `mergeSort(int num[], int l, int r)` |
| Calls | None |
| Observed usage | Used only as the final combine step after recursive sorting |

### 6. Per-Branch Detail Blocks
- **Temporary array setup:** The method derives `n1 = m - l + 1` and `n2 = r - m` to determine the exact size of each half.
- **Copy left half:** The loop over `i` transfers values from `num[l + i]` into `L[i]`.
- **Copy right half:** The loop over `j` transfers values from `num[m + 1 + j]` into `R[j]`.
- **Main merge loop:** While both halves still contain elements, the method compares `L[i]` and `R[j]`. The smaller value is written to `num[k]`, then the corresponding pointer and `k` are advanced.
- **Leftover left-side branch:** If the right half is exhausted first, the remaining values in `L` are copied back into `num`.
- **Leftover right-side branch:** If the left half is exhausted first, the remaining values in `R` are copied back into `num`.
- **Resulting behavior:** The selected range `num[l..r]` becomes sorted in ascending order, and equal values preserve their relative order because the comparison uses `<=`.

---

## Method: merge(int num[], int l, int m, int r)

### 1. Role
This is the same merge helper as documented above. No separate implementation is present in the source file.

### 2. Processing Pattern
```mermaid
flowchart TD
A["merge is called"] --> B["Compute left and right subarray sizes"]
B --> C["Allocate temporary arrays L and R"]
C --> D["Copy data from num into L and R"]
D --> E["Initialize i j and k"]
E --> F["Compare L i and R j while both subarrays have items"]
F --> G["Write smaller element back into num k"]
G --> F
F --> H["Copy remaining values from L"]
H --> I["Copy remaining values from R"]
I --> J["Return after merged range is rebuilt"]
```

### 3. Parameter Analysis
| Parameter | Type | Purpose | Constraints / Notes |
|-----------|------|---------|---------------------|
| `num` | `int[]` | Source and destination array | Must contain the full range to be merged |
| `l` | `int` | Left boundary of the merge window | Must be within array bounds |
| `m` | `int` | Middle index separating the two halves | Must satisfy `l <= m < r` in normal use |
| `r` | `int` | Right boundary of the merge window | Must be within array bounds |

### 4. CRUD Operations / Called Services
| Category | Details |
|----------|---------|
| Array reads | Copies values from `num[l..m]` and `num[m+1..r]` into temporary arrays |
| Array writes | Overwrites `num[l..r]` with the merged sorted result |
| Allocations | Creates temporary arrays `L` and `R` for the two halves |
| External services | None |

### 5. Dependency Trace
| Direction | Details |
|-----------|---------|
| Called by | `mergeSort(int num[], int l, int r)` |
| Calls | None |
| Observed usage | Used only as the final combine step after recursive sorting |

### 6. Per-Branch Detail Blocks
- **Temporary array setup:** The method derives `n1 = m - l + 1` and `n2 = r - m` to determine the exact size of each half.
- **Copy left half:** The loop over `i` transfers values from `num[l + i]` into `L[i]`.
- **Copy right half:** The loop over `j` transfers values from `num[m + 1 + j]` into `R[j]`.
- **Main merge loop:** While both halves still contain elements, the method compares `L[i]` and `R[j]`. The smaller value is written to `num[k]`, then the corresponding pointer and `k` are advanced.
- **Leftover left-side branch:** If the right half is exhausted first, the remaining values in `L` are copied back into `num`.
- **Leftover right-side branch:** If the left half is exhausted first, the remaining values in `R` are copied back into `num`.
- **Resulting behavior:** The selected range `num[l..r]` becomes sorted in ascending order, and equal values preserve their relative order because the comparison uses `<=`.

---

## Method: merge(int num[], int l, int m, int r)

### 1. Role
This is the same merge helper as documented above. The inventory duplicates it several times, but only one implementation exists and it serves all merge-sort recursion steps.

### 2. Processing Pattern
```mermaid
flowchart TD
A["merge is called"] --> B["Compute left and right subarray sizes"]
B --> C["Allocate temporary arrays L and R"]
C --> D["Copy data from num into L and R"]
D --> E["Initialize i j and k"]
E --> F["Compare L i and R j while both subarrays have items"]
F --> G["Write smaller element back into num k"]
G --> F
F --> H["Copy remaining values from L"]
H --> I["Copy remaining values from R"]
I --> J["Return after merged range is rebuilt"]
```

### 3. Parameter Analysis
| Parameter | Type | Purpose | Constraints / Notes |
|-----------|------|---------|---------------------|
| `num` | `int[]` | Source and destination array | Must contain the full range to be merged |
| `l` | `int` | Left boundary of the merge window | Must be within array bounds |
| `m` | `int` | Middle index separating the two halves | Must satisfy `l <= m < r` in normal use |
| `r` | `int` | Right boundary of the merge window | Must be within array bounds |

### 4. CRUD Operations / Called Services
| Category | Details |
|----------|---------|
| Array reads | Copies values from `num[l..m]` and `num[m+1..r]` into temporary arrays |
| Array writes | Overwrites `num[l..r]` with the merged sorted result |
| Allocations | Creates temporary arrays `L` and `R` for the two halves |
| External services | None |

### 5. Dependency Trace
| Direction | Details |
|-----------|---------|
| Called by | `mergeSort(int num[], int l, int r)` |
| Calls | None |
| Observed usage | Used only as the final combine step after recursive sorting |

### 6. Per-Branch Detail Blocks
- **Temporary array setup:** The method derives `n1 = m - l + 1` and `n2 = r - m` to determine the exact size of each half.
- **Copy left half:** The loop over `i` transfers values from `num[l + i]` into `L[i]`.
- **Copy right half:** The loop over `j` transfers values from `num[m + 1 + j]` into `R[j]`.
- **Main merge loop:** While both halves still contain elements, the method compares `L[i]` and `R[j]`. The smaller value is written to `num[k]`, then the corresponding pointer and `k` are advanced.
- **Leftover left-side branch:** If the right half is exhausted first, the remaining values in `L` are copied back into `num`.
- **Leftover right-side branch:** If the left half is exhausted first, the remaining values in `R` are copied back into `num`.
- **Resulting behavior:** The selected range `num[l..r]` becomes sorted in ascending order, and equal values preserve their relative order because the comparison uses `<=`.

---

## Method: merge(int num[], int l, int m, int r)

### 1. Role
This is the same merge helper as documented above. It is repeated in the inventory to account for the total method count, but the class contains a single implementation.

### 2. Processing Pattern
```mermaid
flowchart TD
A["merge is called"] --> B["Compute left and right subarray sizes"]
B --> C["Allocate temporary arrays L and R"]
C --> D["Copy data from num into L and R"]
D --> E["Initialize i j and k"]
E --> F["Compare L i and R j while both subarrays have items"]
F --> G["Write smaller element back into num k"]
G --> F
F --> H["Copy remaining values from L"]
H --> I["Copy remaining values from R"]
I --> J["Return after merged range is rebuilt"]
```

### 3. Parameter Analysis
| Parameter | Type | Purpose | Constraints / Notes |
|-----------|------|---------|---------------------|
| `num` | `int[]` | Source and destination array | Must contain the full range to be merged |
| `l` | `int` | Left boundary of the merge window | Must be within array bounds |
| `m` | `int` | Middle index separating the two halves | Must satisfy `l <= m < r` in normal use |
| `r` | `int` | Right boundary of the merge window | Must be within array bounds |

### 4. CRUD Operations / Called Services
| Category | Details |
|----------|---------|
| Array reads | Copies values from `num[l..m]` and `num[m+1..r]` into temporary arrays |
| Array writes | Overwrites `num[l..r]` with the merged sorted result |
| Allocations | Creates temporary arrays `L` and `R` for the two halves |
| External services | None |

### 5. Dependency Trace
| Direction | Details |
|-----------|---------|
| Called by | `mergeSort(int num[], int l, int r)` |
| Calls | None |
| Observed usage | Used only as the final combine step after recursive sorting |

### 6. Per-Branch Detail Blocks
- **Temporary array setup:** The method derives `n1 = m - l + 1` and `n2 = r - m` to determine the exact size of each half.
- **Copy left half:** The loop over `i` transfers values from `num[l + i]` into `L[i]`.
- **Copy right half:** The loop over `j` transfers values from `num[m + 1 + j]` into `R[j]`.
- **Main merge loop:** While both halves still contain elements, the method compares `L[i]` and `R[j]`. The smaller value is written to `num[k]`, then the corresponding pointer and `k` are advanced.
- **Leftover left-side branch:** If the right half is exhausted first, the remaining values in `L` are copied back into `num`.
- **Leftover right-side branch:** If the left half is exhausted first, the remaining values in `R` are copied back into `num`.
- **Resulting behavior:** The selected range `num[l..r]` becomes sorted in ascending order, and equal values preserve their relative order because the comparison uses `<=`.

---

## Method: mergeSort(int num[], int l, int r)

### 1. Role
This method recursively sorts a segment of the input array using merge sort. It divides the current range into two halves, sorts each half, and then merges the results using `merge`.

### 2. Processing Pattern
```mermaid
flowchart TD
A["mergeSort is called"] --> B["Check whether l is less than r"]
B --> C["Compute midpoint m from l and r"]
C --> D["Recursively sort left half"]
D --> E["Recursively sort right half"]
E --> F["Merge the two sorted halves"]
B --> G["Return without action when the range has one element or is invalid"]
```

### 3. Parameter Analysis
| Parameter | Type | Purpose | Constraints / Notes |
|-----------|------|---------|---------------------|
| `num` | `int[]` | Array being sorted | Must be non-null in normal execution |
| `l` | `int` | Left boundary of the current segment | Defines the start of the recursive window |
| `r` | `int` | Right boundary of the current segment | Defines the end of the recursive window |

### 4. CRUD Operations / Called Services
| Category | Details |
|----------|---------|
| Array reads | Reads the current range to split and merge it indirectly |
| Array writes | Modifies `num` through recursive merge operations |
| Internal calls | Calls `mergeSort(num, l, m)`, `mergeSort(num, m + 1, r)`, and `merge(num, l, m, r)` |
| External services | None |

### 5. Dependency Trace
| Direction | Details |
|-----------|---------|
| Called by | UI action handler in `Sortingss` when Merge Sort is selected |
| Calls | `mergeSort`, `merge` |
| Downstream effect | Produces a fully sorted array segment for display in the GUI |

### 6. Per-Branch Detail Blocks
- **Base-case guard:** The method performs work only when `l < r`. If the range has zero or one element, it returns immediately.
- **Midpoint calculation:** `m` is computed as `l + (r - l) / 2`, which avoids overflow compared with a direct average expression.
- **Left recursion:** The left half `[l..m]` is sorted first by another `mergeSort` call.
- **Right recursion:** The right half `[m + 1..r]` is then sorted by a second recursive call.
- **Merge phase:** After both halves are sorted, `merge` combines them into a single sorted range.
- **Algorithmic effect:** Repeated division reduces the problem until single elements are reached, then the method rebuilds sorted segments while unwinding the recursion.
