---
# (DD32) Business Logic — merg.merge() [46 LOC]

| Field | Value |
|-------|-------|
| Fully Qualified Name | `algo_Calc.merg` |
| Layer | Utility |
| Module | `algo_Calc` (Package: `algo_Calc`) |

## 1. Role

### merg.merge()

This method performs the merge phase of a merge-sort implementation by combining two already sorted subarrays into one sorted range in the original array. It is a low-level array transformation routine rather than a business-service method, and its responsibility is to preserve ordering while copying values back into the target array segment.

The method handles one algorithmic category only: numeric array merge processing. It does not branch by business type, customer type, or transaction category; instead, it branches purely on comparative order between the left and right temporary buffers. Its design pattern is a classic divide-and-conquer merge routine, using two temporary working arrays and a sequential merge cursor to reconstruct the sorted output.

Within the larger system, this method is a reusable utility called by the surrounding sort workflow in `Sortingss.java`. It acts as an internal implementation detail for sorting, not as a screen entry point or a data-access service. Its conditional logic determines which side of the merge contributes the next value, and its trailing loops complete the copy of any remaining values after one side has been exhausted.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["merge(num, l, m, r)"])
    CALC_N1["Compute n1 = m - l + 1"]
    CALC_N2["Compute n2 = r - m"]
    INIT_L["Allocate L[n1]"]
    INIT_R["Allocate R[n2]"]
    COPY_L{"for i from 0 to n1 - 1"}
    COPY_R{"for j from 0 to n2 - 1"}
    PREP["Initialize i = 0, j = 0, k = l"]
    MERGE_LOOP{"while i < n1 and j < n2"}
    COMPARE{"L[i] <= R[j] ?"}
    TAKE_L["Copy L[i] to num[k] and increment i"]
    TAKE_R["Copy R[j] to num[k] and increment j"]
    INC_K["Increment k"]
    LEFT_LOOP{"while i < n1"}
    LEFT_COPY["Copy remaining L[i] to num[k]"]
    RIGHT_LOOP{"while j < n2"}
    RIGHT_COPY["Copy remaining R[j] to num[k]"]
    END_NODE(["Return"])

    START --> CALC_N1 --> CALC_N2 --> INIT_L --> INIT_R --> COPY_L --> COPY_R --> PREP --> MERGE_LOOP --> COMPARE
    COMPARE -->|Yes| TAKE_L --> INC_K --> MERGE_LOOP
    COMPARE -->|No| TAKE_R --> INC_K --> MERGE_LOOP
    MERGE_LOOP -->|Exit| LEFT_LOOP
    LEFT_LOOP -->|Yes| LEFT_COPY --> LEFT_LOOP
    LEFT_LOOP -->|Exit| RIGHT_LOOP
    RIGHT_LOOP -->|Yes| RIGHT_COPY --> RIGHT_LOOP
    RIGHT_LOOP -->|Exit| END_NODE
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `num[]` | `int` | Source-and-target integer array that holds the full sortable dataset. The method reads from this array to build temporary left/right partitions and writes merged results back into the same array segment. |
| 2 | `l` | `int` | Left boundary index of the merge range. It identifies the first element in the left sorted subarray and sets the starting write position for the merged result. |
| 3 | `m` | `int` | Midpoint index that separates the two sorted halves. Values up to `m` belong to the left half; values after `m` belong to the right half. |
| 4 | `r` | `int` | Right boundary index of the merge range. It identifies the last element to be merged and determines the size of the right temporary buffer. |

Instance fields or external state read by the method: none. The method uses only local variables and the supplied array slice.

## 4. CRUD Operations / Called Services

This method does not invoke application services, repositories, or database CRUD operations. All work is local in-memory array manipulation.

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| - | - | - | - | No external CRUD or service call is performed; the method only copies and reorders integer values in memory. |

## 5. Dependency Trace

### Pre-computed evidence from code analysis graph:

No screen/batch entry points found within 8 hops. Direct callers found: 1 methods.
Terminal operations from this method: -

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Utility: `Sortingss.mergeSort` | `Sortingss.mergeSort` -> `merg.merge` | `-` |

## 6. Per-Branch Detail Blocks

**Block 1** — SET/EXEC `(method entry and local sizing)` (L58)

> Initializes working indices and calculates the sizes of the temporary left and right partitions.

| # | Type | Code |
|---|------|------|
| 1 | SET | `int i, j, k;` |
| 2 | SET | `int n1 = m - l + 1;` |
| 3 | SET | `int n2 = r - m;` |
| 4 | SET | `int[] L = new int[n1];` |
| 5 | SET | `int[] R = new int[n2];` |

**Block 1.1** — FOR `(i = 0; i < n1; i++)` (L64)

> Copies the left sorted half into a temporary buffer.

| # | Type | Code |
|---|------|------|
| 1 | SET | `L[i] = num[l + i];` |

**Block 1.2** — FOR `(j = 0; j < n2; j++)` (L66)

> Copies the right sorted half into a temporary buffer.

| # | Type | Code |
|---|------|------|
| 1 | SET | `R[j] = num[m + 1 + j];` |

**Block 2** — SET `(pointer initialization)` (L68-L70)

> Resets the merge cursors and positions the write pointer at the start of the target range.

| # | Type | Code |
|---|------|------|
| 1 | SET | `i = 0;` |
| 2 | SET | `j = 0;` |
| 3 | SET | `k = l;` |

**Block 3** — WHILE `(i < n1 && j < n2)` (L71)

> Repeatedly selects the smaller front value from either temporary buffer and writes it back to the merged range.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `if (L[i] <= R[j])` |

**Block 3.1** — IF `(L[i] <= R[j])` (L73)

> Prefers the left buffer when both candidate values are equal or the left value is smaller.

| # | Type | Code |
|---|------|------|
| 1 | SET | `num[k] = L[i];` |
| 2 | SET | `i++;` |

**Block 3.2** — ELSE `(L[i] > R[j])` (L78)

> Takes the next value from the right buffer when it is smaller than the left candidate.

| # | Type | Code |
|---|------|------|
| 1 | SET | `num[k] = R[j];` |
| 2 | SET | `j++;` |

**Block 3.3** — SET `(after each comparison branch)` (L82)

> Advances the target write position after one value has been copied into the merged output.

| # | Type | Code |
|---|------|------|
| 1 | SET | `k++;` |

**Block 4** — WHILE `(i < n1)` (L85)

> Copies any remaining values from the left buffer after the right buffer has been exhausted.

| # | Type | Code |
|---|------|------|
| 1 | SET | `num[k] = L[i];` |
| 2 | SET | `i++;` |
| 3 | SET | `k++;` |

**Block 5** — WHILE `(j < n2)` (L92)

> Copies any remaining values from the right buffer after the left buffer has been exhausted.

| # | Type | Code |
|---|------|------|
| 1 | SET | `num[k] = R[j];` |
| 2 | SET | `j++;` |
| 3 | SET | `k++;` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `num[]` | Field | Integer working array that stores the full dataset being sorted. |
| `l` | Field | Left index of the merge window. |
| `m` | Field | Midpoint index dividing the left and right sorted partitions. |
| `r` | Field | Right index of the merge window. |
| `n1` | Technical term | Number of elements in the left temporary buffer. |
| `n2` | Technical term | Number of elements in the right temporary buffer. |
| `L` | Technical term | Left temporary array used during merge. |
| `R` | Technical term | Right temporary array used during merge. |
| `i` | Technical term | Cursor for the left temporary array. |
| `j` | Technical term | Cursor for the right temporary array. |
| `k` | Technical term | Cursor for the destination position in the original array. |
| merge sort | Algorithm | Divide-and-conquer sorting method that splits data, sorts each half, and merges results. |
| temporary buffer | Technical term | Short-lived array used to hold partition values during merging. |
| sorted half | Technical term | A subarray already arranged in ascending order before merge. |
| Utility | Layer | Shared helper code that performs local computation without external persistence or orchestration. |
