# (DD39) Business Logic — merg.mergeSort() [11 LOC]

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

## 1. Role

### merg.mergeSort()

`mergeSort()` performs the recursive divide-and-conquer phase of a merge sort implementation over an integer array segment. Business-wise, it is a generic ordering utility that takes an unsorted slice of numeric records and repeatedly splits the slice into smaller partitions until each partition contains one or zero elements, which are already ordered by definition. It then delegates to `merge()` to consolidate the sorted partitions back into a single ordered range. The method therefore acts as a sorting orchestration point rather than a data transformation with domain-specific rules.

The method supports only one processing path: the recursive sort path when the left index is less than the right index, and the implicit base-case path when the segment has already been reduced to a minimal size. This is a classic routing/delegation pattern in which `mergeSort()` controls recursion depth and partition boundaries while `merge()` performs the actual combination step. Within the larger system, it appears to be a shared internal algorithm used by the same class to sort a sample integer array, and it is not tied to any business entity, database table, or transactional CRUD operation.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["mergeSort(num, l, r)"])
    C1{"l < r?"}
    SPLIT["Calculate midpoint m = l + (r-l)/2"]
    LEFT["Recursive call mergeSort(num, l, m)"]
    RIGHT["Recursive call mergeSort(num, m+1, r)"]
    MERGE["Call merge(num, l, m, r)"]
    BASE["No action when subarray has 0 or 1 element"]
    END_NODE(["Return / Next"])

    START --> C1
    C1 -->|Yes| SPLIT
    SPLIT --> LEFT
    LEFT --> RIGHT
    RIGHT --> MERGE
    MERGE --> END_NODE
    C1 -->|No| BASE
    BASE --> END_NODE
```

**CRITICAL — Constant Resolution:**
No constant-based branching is present in this method. The only decision condition is `l < r`, which is a direct comparison of indices.

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `num[]` | `int` | Numeric dataset to be sorted; this is the full collection whose ordering is being normalized within the requested index range. |
| 2 | `l` | `int` | Left boundary of the current sortable segment, representing the first element index included in the recursive sort scope. |
| 3 | `r` | `int` | Right boundary of the current sortable segment, representing the last element index included in the recursive sort scope. |

**Instance fields / external state read by the method:** None. The method uses only its parameters and local variables.

## 4. CRUD Operations / Called Services

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

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| U | `merg.merge` | merg | - | Calls `merge` in `merg` |
| U | `merg.mergeSort` | merg | - | Calls `mergeSort` in `merg` |

Analyze all method calls within this method and classify each as a CRUD operation.

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| U | `mergeSort` | merg | - | Recursively processes the left half of the same array segment. |
| U | `mergeSort` | merg | - | Recursively processes the right half of the same array segment. |
| U | `merge` | merg | - | Merges the two sorted halves into one ordered segment. |

## 5. Dependency Trace

Trace who calls this method and what this method ultimately calls.

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Screen:unknown | `obj3.mergeSort(num, 0, size)` -> `merg.mergeSort` | `merge [U] -` |

## 6. Per-Branch Detail Blocks

**Block 1** — IF `(l < r)` (L105)

> Sorts the current subarray only when it contains at least two elements.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `if (l < r)` // checks whether the current partition still needs splitting |
| 2 | SET | `int m = l+(r-l)/2;` // calculates the midpoint for divide-and-conquer splitting |
| 3 | CALL | `mergeSort(num, l, m);` // recursively sorts the left half |
| 4 | CALL | `mergeSort(num, m+1, r);` // recursively sorts the right half |
| 5 | CALL | `merge(num, l, m, r);` // merges the two sorted halves |

**Block 1.1** — THEN `(recursive left partition)` (L111)

| # | Type | Code |
|---|------|------|
| 1 | CALL | `mergeSort(num, l, m);` |

**Block 1.2** — THEN `(recursive right partition)` (L112)

| # | Type | Code |
|---|------|------|
| 1 | CALL | `mergeSort(num, m+1, r);` |

**Block 1.3** — THEN `(merge sorted partitions)` (L113)

| # | Type | Code |
|---|------|------|
| 1 | CALL | `merge(num, l, m, r);` |

**Block 2** — ELSE `(l >= r)` (L105)

> The segment is already sorted because it has one or zero elements, so the method exits without additional work.

| # | Type | Code |
|---|------|------|
| 1 | RETURN | implicit return at method end |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|-------------------|
| `num` | Field | Integer array holding the values to be sorted. |
| `l` | Field | Left index of the active sort window. |
| `r` | Field | Right index of the active sort window. |
| `m` | Field | Midpoint index used to split the current range into two halves. |
| merge sort | Technical term | Divide-and-conquer sorting algorithm that recursively sorts two halves and merges them. |
| recursion | Technical term | A method calling itself to solve smaller instances of the same problem. |
| divide-and-conquer | Technical term | Strategy of splitting a problem into smaller pieces, solving them, and combining the results. |
| Utility | Layer | Shared algorithmic helper code with no direct screen or database responsibility. |
| CRUD | Acronym | Create, Read, Update, Delete — standard operation categories used here to classify method calls. |
