# (DD25) Business Logic — heaps.heapSort() [14 LOC]

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

## 1. Role

### heaps.heapSort()

`heapSort()` is the sorting entry point used by the calculator/sorting utility screen flow to reorder a numeric array in ascending order using the heap sort algorithm. In business terms, it takes a user-entered list of numbers and converts it into a ranked, ordered sequence that can be displayed back to the user as the selected sorting result. The method implements a classic two-phase transformation pattern: first it builds a max heap from the full input range, then it repeatedly extracts the largest value and places it at the end of the array. Its role in the larger system is purely computational and reusable; it does not access persistence or external services, and it supports the UI branch that handles the “Heap Sort” option. Because the method is invoked only when the user selects key `6`, it acts as a dedicated algorithm dispatcher for that one sort category.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["heapSort(num, size)"])
    INIT(["Initialize loop index i"])
    BUILD_HEAP{"i = size / 2 - 1; i >= 0?"}
    HEAPIFY_BUILD(["Call heapify(num, size, i)"])
    EXTRACT_INIT(["Set i = size - 1"])
    EXTRACT_CHECK{"i >= 0?"}
    SWAP(["Swap num[0] and num[i]"])
    HEAPIFY_REDUCED(["Call heapify(num, i, 0)"])
    END_NODE(["Return"])
    START --> INIT
    INIT --> BUILD_HEAP
    BUILD_HEAP -->|Yes| HEAPIFY_BUILD
    HEAPIFY_BUILD --> BUILD_HEAP
    BUILD_HEAP -->|No| EXTRACT_INIT
    EXTRACT_INIT --> EXTRACT_CHECK
    EXTRACT_CHECK -->|Yes| SWAP
    SWAP --> HEAPIFY_REDUCED
    HEAPIFY_REDUCED --> EXTRACT_CHECK
    EXTRACT_CHECK -->|No| END_NODE
```

The method first prepares the heap-building phase by iterating from the last non-leaf node down to the root and calling `heapify()` on each position. After the heap is built, it repeatedly swaps the root value with the current tail element, shrinking the active heap boundary on each pass. The second loop restores heap order on the reduced range by calling `heapify()` again with the shortened size. The overall result is an in-place ascending sort of the input array.

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `num[]` | `int` | Numeric input list to be sorted. It represents the customer-entered sequence of values shown in the application’s sort result area. The method rearranges this array in place, so every element may change position during heap construction and extraction. |
| 2 | `size` | `int` | Effective number of numeric elements to include in heap sorting. It controls both the heap construction range and the extraction loop boundary, so a larger value expands the sortable range while a smaller value limits processing to the leading portion of the array. |

**External state read by the method:** none. The method uses only its parameters and the local variable `i`; it does not read instance fields, constants, database values, or shared mutable state.

## 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.

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `heapify` | heaps | - | Reads and reorders the active heap region to maintain max-heap structure during heap construction and extraction. |

## 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: Sortingss.java | `Sortingss` heap sort branch (`key==6`) -> `heaps obj4 = new heaps()` -> `obj4.heapSort(num, size)` | `heapify [R] -` |

`heapSort()` is reached from the UI branch that handles the heap sort option. The caller initializes `heaps`, passes the current numeric array, and uses the method result to populate the displayed output string. Internally, the only downstream business method is `heapify()`, which is invoked repeatedly during both heap building and heap extraction.

## 6. Per-Branch Detail Blocks

**Block 1** — **FOR** `(i = size / 2 - 1; i >= 0; i--)` (L145)

> Builds the initial max heap from the last non-leaf node up to the root.

| # | Type | Code |
|---|------|------|
| 1 | SET | `i = size / 2 - 1;` |
| 2 | CALL | `heapify(num, size, i);` |

**Block 2** — **FOR** `(i = size - 1; i >= 0; i--)` (L147)

> Extracts the largest element one by one and places it at the end of the array.

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

### Block 2.1 — nested processing inside extraction loop

| # | Type | Code |
|---|------|------|
| 1 | SET | `temp = num[0];` |
| 2 | SET | `num[0] = num[i];` |
| 3 | SET | `num[i] = temp;` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `heapSort` | Method | In-place heap sorting routine that orders numeric input into ascending sequence. |
| `heapify` | Method | Heap maintenance routine that restores max-heap order for a given subtree. |
| `num` | Field | Input number array supplied from the sort screen for ordering. |
| `size` | Field | Number of elements in the sortable portion of the array. |
| `temp` | Variable | Temporary swap holder used while exchanging the heap root with the tail element. |
| Heap Sort | Business term | Comparison-based sorting approach that uses a heap structure to produce ordered output. |
| Max heap | Business term | Heap structure where the parent value is greater than or equal to its children, enabling largest-value extraction first. |
| `key` | Field | UI selection code that determines which sorting algorithm branch is executed. |
| `key==6` | Branch condition | Heap sort menu choice; activates the heap sorting flow. |
| `Sortingss` | Class | Sorting screen or utility class containing multiple algorithm branches. |
