# (DD22) Business Logic — heaps.heapify() [21 LOC]

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

## 1. Role

### heaps.heapify()

`heapify()` is the core heap-maintenance routine used by the heap sort implementation in `Sortingss.java`. Its business role is to restore the max-heap property for a subtree rooted at index `i` within the numeric array `num[]`, ensuring that the largest value in that subtree is positioned at the root of the subtree before the sort continues. In practical terms, this method supports the array reordering step that allows the larger elements to be repeatedly moved toward the end of the array during heap sort.

The method applies a recursive routing/adjustment pattern: it checks the left child, checks the right child, swaps the current root with the larger child when needed, and then recursively re-heapifies the affected subtree. There are no business-category branches beyond index comparison and swap control, so the method handles a single service type: in-memory numeric heap maintenance. In the larger system, this is a reusable algorithmic utility rather than a screen-driven business workflow, and it is invoked internally by `heapSort()` and by recursive self-calls to maintain ordering correctness after each swap.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START["heapify(num, size, i)"]
    INIT["Initialize largest, left, and right indices"]
    COND_LEFT{"left < size and num[left] > num[largest]"}
    SET_LEFT["largest = left"]
    COND_RIGHT{"right < size and num[right] > num[largest]"}
    SET_RIGHT["largest = right"]
    COND_SWAP{"largest != i"}
    SWAP_TEMP["Store num[i] in temp"]
    SWAP_I["Set num[i] = num[largest]"]
    SWAP_LARGEST["Set num[largest] = temp"]
    RECURSE["heapify(num, size, largest)"]
    END_NODE["Return to caller"]

    START --> INIT
    INIT --> COND_LEFT
    COND_LEFT -->|Yes| SET_LEFT
    COND_LEFT -->|No| COND_RIGHT
    SET_LEFT --> COND_RIGHT
    COND_RIGHT -->|Yes| SET_RIGHT
    COND_RIGHT -->|No| COND_SWAP
    SET_RIGHT --> COND_SWAP
    COND_SWAP -->|Yes| SWAP_TEMP
    COND_SWAP -->|No| END_NODE
    SWAP_TEMP --> SWAP_I
    SWAP_I --> SWAP_LARGEST
    SWAP_LARGEST --> RECURSE
    RECURSE --> END_NODE
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `num[]` | `int` | The working numeric dataset being reorganized into heap order. Each element is compared and potentially swapped to maintain the max-heap property. |
| 2 | `size` | `int` | The effective heap boundary within `num[]`. Values at or beyond this boundary are excluded from heap maintenance and are treated as already fixed by the sort process. |
| 3 | `i` | `int` | The root index of the subtree currently being heapified. This is the anchor position that may be replaced by a larger child value if either child violates heap order. |

External state read by the method: none. The method uses only its parameters and local variables (`largest`, `left`, `right`, and `temp`).

## 4. CRUD Operations / Called Services

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

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| - | `heaps.heapify` | heaps | - | Calls `heapify` in `heaps` |

Analyze all method calls within this method and classify each as a CRUD operation.
Use the pre-computed evidence above. If SC Code or Entity/DB is missing, try to infer from:
- The **SC Code** (Service Component code, e.g., `EKK0361A010SC`, `EKK1081D010CBS`) — look at the class name of the called method or its containing class.
- The **Entity/DB tables** — search for table name constants (often `KK_T_*` pattern), SQL references, or entity names in the called method's source code.

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `heapify` | `heaps` | - | Reads child values and the current root value from the in-memory array to evaluate heap order. |
| U | `heapify` | `heaps` | - | Updates array positions by swapping the current root with the larger child when heap order is violated. |
| R | `heapify` | `heaps` | - | Recursively re-reads the affected subtree after a swap to ensure the heap property is restored. |

## 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: `heapify` [-], `heapify` [-], `heapify` [-], `heapify` [-], `heapify` [-], `heapify` [-]

Trace who calls this method and what this method ultimately calls.
Use the pre-computed evidence and caller search results from Step 2 above.

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Utility: `heaps.heapSort()` | `heaps.heapSort()` -> `heaps.heapify()` | `heapify [R/U] in-memory array` |

## 6. Per-Branch Detail Blocks

Analyze the method's control flow block by block. Analyze ALL nesting levels — no depth limit.

> Each branch of the control flow is displayed as a hierarchical block structure.

**Block 1** — SET / INIT (L121)

> Initialize subtree comparison state for heap maintenance.

| # | Type | Code |
|---|------|------|
| 1 | SET | `int largest = i;` |
| 2 | SET | `int left = 2*i + 1;` |
| 3 | SET | `int right = 2*i + 2;` |

**Block 2** — IF `(left < size && num[left] > num[largest])` (L126)

> Checks whether the left child is within the active heap and larger than the current root candidate.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `num[left]` and `num[largest]` are read for comparison. |
| 2 | SET | `largest = left;` |

**Block 3** — IF `(right < size && num[right] > num[largest])` (L129)

> Checks whether the right child is within the active heap and larger than the current largest candidate, including any left-child promotion.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `num[right]` and `num[largest]` are read for comparison. |
| 2 | SET | `largest = right;` |

**Block 4** — IF `(largest != i)` (L132)

> If a child outranks the current root, the method restores heap order by swapping the values and recursing into the affected branch.

| # | Type | Code |
|---|------|------|
| 1 | SET | `int temp;` |
| 2 | SET | `temp = num[i];` |
| 3 | SET | `num[i] = num[largest];` |
| 4 | SET | `num[largest] = temp;` |
| 5 | CALL | `heapify(num, size, largest);` |

**Block 4.1** — CALL `heapify(num, size, largest)` (L138)

> Recursively re-evaluates the subtree rooted at the child position that received the previous root value.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `heapify(num, size, largest);` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `heapify` | Method | Heap restoration routine that reorders a subtree so the largest value is at the root of that subtree. |
| `heapSort` | Method | Sorting routine that repeatedly builds and shrinks a max-heap to produce ascending order. |
| `num[]` | Field / Parameter | Working integer array being sorted or heap-adjusted. |
| `size` | Parameter | Active heap size boundary used to exclude already-sorted tail elements. |
| `i` | Parameter | Root index of the subtree currently under heap maintenance. |
| `largest` | Local variable | Index of the current maximum candidate among the root, left child, and right child. |
| `left` | Local variable | Index of the left child in the implicit binary heap representation. |
| `right` | Local variable | Index of the right child in the implicit binary heap representation. |
| `temp` | Local variable | Temporary storage used during element swapping. |
| Heap | Business term | Tree-based ordering structure where parent nodes dominate child nodes in a max-heap. |
| Max-heap | Business term | Heap variant where each parent value is greater than or equal to its children. |
| Recursive call | Technical term | A method invocation that re-enters the same routine to continue heap restoration after a swap. |
| Utility | Layer | Shared algorithmic helper that performs in-memory data manipulation rather than business transaction processing. |
