# (DD43) Business Logic — quiks.partition() [18 LOC]

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

## 1. Role

### quiks.partition()

This method performs the partitioning step of an in-place quicksort routine over an integer array. It selects the last element of the target range as the pivot value, then reorders the elements between `start` and `end - 1` so that all values smaller than the pivot are moved to the left side of the range. After the scan completes, the pivot is placed into its final sorted position and that position index is returned to the caller.

From a business-logic perspective, this is a reusable sorting utility that supports divide-and-conquer ordering of numeric datasets. The method does not branch by business service type; instead, it implements a single routing/dispatch pattern for array partitioning, which is the core operation needed by the surrounding quicksort algorithm. Its role in the larger system is to normalize one segment of an array into a left-less-than-pivot / right-greater-or-equal-to-pivot structure so the caller can recursively sort the remaining subranges.

The processing is entirely algorithmic and stateful on the supplied array: the input array is mutated in place, the `start` boundary advances as smaller values are encountered, and the method finally returns the pivot index for downstream recursive calls. No external services, database operations, or persistence effects are involved.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START["partition(num, start, end)"]
    INIT["Set pivot = num[end]"]
    LOOP["For i from start to end - 1"]
    CHECK["Is num[i] < pivot?"]
    SWAP1["Swap num[start] and num[i]"]
    INC["Increment start"]
    SWAP2["Swap pivot into num[start]"]
    RETURN_NODE["Return start"]
    END_NODE["End"]

    START --> INIT
    INIT --> LOOP
    LOOP --> CHECK
    CHECK -->|Yes| SWAP1
    CHECK -->|No| LOOP
    SWAP1 --> INC
    INC --> LOOP
    LOOP -->|After loop| SWAP2
    SWAP2 --> RETURN_NODE
    RETURN_NODE --> END_NODE
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `num` | `int[]` | The numeric dataset being sorted. The method mutates this array in place by moving smaller values to the front of the active range and placing the pivot into its final position. |
| 2 | `start` | `int` | The left boundary of the active partition window. It marks the first index in the subarray currently being normalized and is advanced each time a value smaller than the pivot is found. |
| 3 | `end` | `int` | The right boundary of the active partition window and the pivot index. The value at this position is used as the pivot and remains fixed until the final swap places it into its sorted location. |

External state read by the method: the contents of `num` within the `start..end` range. No instance fields are read.

## 4. CRUD Operations / Called Services

This method contains no service calls, database access, or CRUD operations. All operations are local array reads, writes, and swaps.

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|-----------------------|
| - | - | - | - | No external CRUD activity; the method only rearranges elements within the provided array. |

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

This method is called by the quicksort driver in the same class and returns a pivot index for the next recursive sort step.

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Utility: `quiks` | `quiks.quickSort` -> `quiks.partition` | `-` |

## 6. Per-Branch Detail Blocks

**Block 1** — [SEQUENTIAL] `(method entry)` (L32)

> Initializes the partition routine for the active array segment.

| # | Type | Code |
|---|------|------|
| 1 | SET | `int pivot = num[end];` |

**Block 2** — [FOR] `(i = start; i < end)` (L34)

> Scans the active range from left to right, excluding the pivot position.

| # | Type | Code |
|---|------|------|
| 1 | LOOP | `for(int i=start; i<end; i++)` |
| 2 | IF | `if(num[i] < pivot)` |

**Block 2.1** — [IF] `(num[i] < pivot)` (L35)

> Moves elements smaller than the pivot toward the front of the range.

| # | Type | Code |
|---|------|------|
| 1 | SET | `int temp = num[start];` |
| 2 | EXEC | `num[start] = num[i];` |
| 3 | EXEC | `num[i] = temp;` |
| 4 | SET | `start++;` |

**Block 2.2** — [ELSE] `(num[i] >= pivot)` (L35)

> Leaves the element in place and advances to the next index.

| # | Type | Code |
|---|------|------|
| 1 | CONTINUE | No array swap is executed. |

**Block 3** — [SEQUENTIAL] `(after loop completion)` (L41)

> Places the pivot into the first position where greater-or-equal elements begin.

| # | Type | Code |
|---|------|------|
| 1 | SET | `int temp = num[start];` |
| 2 | EXEC | `num[start] = pivot;` |
| 3 | EXEC | `num[end] = temp;` |

**Block 4** — [RETURN] `(final result)` (L44)

> Returns the final pivot index to the caller for recursive quicksort processing.

| # | Type | Code |
|---|------|------|
| 1 | RETURN | `return start;` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `num` | Field | Input number array being reordered in place during sorting. |
| `start` | Field | Left boundary of the active sorting segment. |
| `end` | Field | Right boundary of the active sorting segment and pivot index. |
| `pivot` | Algorithm term | Reference value used to split the array into lower and higher partitions. |
| `partition` | Algorithm term | Sorting step that rearranges values around a pivot and returns the pivot's final index. |
| quicksort | Algorithm term | Divide-and-conquer sorting method that repeatedly partitions a range and sorts the subranges. |
| in-place | Technical term | Processing style where the original array is modified directly instead of copying data to another structure. |
| Utility | Layer | Reusable helper logic with no persistence or screen-specific responsibility. |
