# (DD01) Sortingss — Class Detailed Design [1743 LOC]

| Field | Value |
|-------|-------|
| Fully Qualified Name | `algo_Calc.Sortingss` |
| Layer | Controller / Swing UI |
| Module | `algo_Calc` |

## Class Overview

`Sortingss` is a Swing-based desktop controller that lets a user enter a space-separated list of integers, choose one of several sorting algorithms, and view the sorted result on screen. It also opens a corresponding text file containing the algorithm source when the user requests it. The class acts as a presentation-layer orchestrator around algorithm helper classes (`quiks`, `merg`, and `heaps`) and inline implementations for bubble, selection, and insertion sort.

---

## Method: initialize()

### 1. Role
Builds the entire application window, wires all UI controls, and attaches the button actions that drive sorting and code viewing. This method is the core composition point for the class because it defines the input model, output rendering, algorithm selection behavior, and file-opening behavior.

### 2. Processing Pattern
```mermaid
flowchart TD
    A["initialize"] --> B["Create JFrame and configure window bounds"]
    B --> C["Create title, input, output, and instruction components"]
    C --> D["Attach Submit handler"]
    C --> E["Attach Exit handler"]
    C --> F["Attach Open Code handler"]
    D --> G["Read text field content"]
    G --> H["Split input by spaces"]
    H --> I["Parse integers into array"]
    I --> J["Branch on key value"]
    J --> K["Bubble sort path when key equals 1"]
    J --> L["Selection sort path when key equals 2"]
    J --> M["Quick sort path when key equals 3"]
    J --> N["Insertion sort path when key equals 4"]
    J --> O["Merge sort path when key equals 5"]
    J --> P["Heap sort path when key equals 6"]
    K --> Q["Build result string"]
    L --> Q
    M --> Q
    N --> Q
    O --> Q
    P --> Q
    Q --> R["Update output area and title label"]
    E --> S["Hide frame"]
    F --> T["Launch Notepad for algorithm source file"]
```

### 3. Parameter Analysis

| Parameter | Type | Source | Meaning | Constraints | Usage |
|-----------|------|--------|---------|-------------|-------|
| None | - | - | The method has no parameters | N/A | Initializes the UI directly from instance state |

### 4. CRUD Operations / Called Services

| Operation Type | Target | Details |
|----------------|--------|---------|
| Create | `JFrame`, `JTextArea`, `JTextField`, `JLabel`, `JButton` | Instantiates and configures all screen components |
| Read | `textField.getText()` | Reads the user-entered integer sequence |
| Update | `textArea.setText(...)`, `textArea_1.setText(...)` | Displays the sorted output and selected algorithm name |
| Action / Service Call | `quiks.quickSort(...)`, `merg.mergeSort(...)`, `heaps.heapSort(...)` | Delegates to helper sorting implementations |
| Action / Service Call | `ProcessBuilder(...).start()` | Opens the algorithm source file in Notepad |

### 5. Dependency Trace

| Dependency | Kind | Evidence / Usage |
|------------|------|------------------|
| `Sortingss.main(String[] args)` | Internal caller | Constructor flow is triggered from application startup |
| `Sortingss.Sortingss()` | Internal caller | Constructor calls `initialize()` immediately |
| `quiks` | Helper class | Used for quick sort when `key == 3` |
| `merg` | Helper class | Used for merge sort when `key == 5` |
| `heaps` | Helper class | Used for heap sort when `key == 6` |
| `JFrame` / Swing controls | UI framework | Defines the interactive desktop shell |

### 6. Per-Branch Detail Blocks

#### 6.1 Window construction branch
- Allocates the main frame.
- Sets location and size to `100,100,514,329`.
- Uses absolute positioning via `null` layout.
- Adds the title/output areas, input field, labels, and buttons.

#### 6.2 Submit button branch
- Reads the input string from the text field.
- Splits the input on spaces into tokens.
- Converts each token to an integer.
- Selects one of six sorting behaviors using the instance field `key`.

#### 6.3 Bubble sort branch (`key == 1`)
- Uses a `flag`-based outer loop to keep scanning until no swaps occur.
- Compares adjacent values and swaps out-of-order pairs.
- Sets the output prefix to `Bubble Sort =`.

#### 6.4 Selection sort branch (`key == 2`)
- Iterates the array from left to right.
- Finds the minimum element in the unsorted suffix.
- Swaps it into the current position.
- Sets the algorithm label to `SELECTION SORT`.

#### 6.5 Quick sort branch (`key == 3`)
- Creates a `quiks` helper instance.
- Sorts the full array from index `0` to `size - 1`.
- Sets the output prefix to `Quick Sort =`.
- Sets the title label to `QUICK SORT`.

#### 6.6 Insertion sort branch (`key == 4`)
- Walks through the array from the second element onward.
- Inserts each value into its correct position among earlier elements.
- Sets the output prefix to `Insertion Sort =`.
- Sets the title label to `INSERTION SORT`.

#### 6.7 Merge sort branch (`key == 5`)
- Creates a `merg` helper instance.
- Sorts the array recursively from `0` to `num.length - 1`.
- Sets the output prefix to `Merge Sort =`.
- Sets the title label to `MERGE SORT`.

#### 6.8 Heap sort branch (`key == 6`)
- Creates a `heaps` helper instance.
- Sorts the array using heap sort with the token count as the heap size.
- Sets the output prefix to `Heap Sort =`.
- Sets the title label to `HEAP SORT`.

#### 6.9 Result rendering branch
- Converts each sorted integer back to a string.
- Appends the values to the result prefix.
- Renders the final string in the output area.
- Refreshes the title label with the selected algorithm name.

#### 6.10 Exit button branch
- Hides the frame by setting visibility to false.
- Does not terminate the JVM directly.

#### 6.11 Open Code button branch
- Checks the active `key` value.
- Opens the matching text file in Notepad.
- Uses one file per algorithm: `bubble.txt`, `selection.txt`, `quicksort.txt`, `insertion.txt`, `merge.txt`, or `maxheap.txt`.
- Catches and prints any launch exception.

---

## Method: main(String[] args)

### 1. Role
Provides the entry point for the desktop application and schedules the UI creation on the AWT event-dispatch thread. The method ensures the Swing frame is created and shown asynchronously rather than on the calling thread.

### 2. Processing Pattern
```mermaid
flowchart TD
    A["main"] --> B["Receive command line arguments"]
    B --> C["Schedule Runnable with EventQueue.invokeLater"]
    C --> D["Instantiate Sortingss"]
    D --> E["Constructor calls initialize"]
    E --> F["Show frame"]
    D --> G["Catch and print any exception"]
```

### 3. Parameter Analysis

| Parameter | Type | Source | Meaning | Constraints | Usage |
|-----------|------|--------|---------|-------------|-------|
| `args` | `String[]` | JVM startup | Command line arguments | Not referenced inside the body | Present to satisfy Java entry-point signature |

### 4. CRUD Operations / Called Services

| Operation Type | Target | Details |
|----------------|--------|---------|
| Action / Service Call | `EventQueue.invokeLater(...)` | Defers UI creation onto the Swing event thread |
| Create | `new Sortingss()` | Creates the main application window controller |
| Update | `window.frame.setVisible(true)` | Displays the window |
| Error Handling | `e.printStackTrace()` | Writes startup failures to the console |

### 5. Dependency Trace

| Dependency | Kind | Evidence / Usage |
|------------|------|------------------|
| `EventQueue` | JDK GUI service | Used to safely start Swing UI code |
| `Sortingss` constructor | Internal dependency | Instantiated inside the runnable |
| `initialize()` | Internal dependency | Called indirectly through the constructor |

### 6. Per-Branch Detail Blocks

#### 6.1 EventQueue scheduling branch
- Wraps UI startup in a `Runnable`.
- Avoids direct frame creation on the main thread.
- Conforms to Swing threading requirements.

#### 6.2 Successful startup branch
- Creates the application object.
- Initializes the frame hierarchy.
- Makes the frame visible.

#### 6.3 Failure branch
- Catches any exception during construction or display.
- Prints the stack trace for debugging.

---

## Method: vis(int k, String str)

### 1. Role
Captures the selected sorting key and display label from external callers, then makes the frame visible again. The method appears to act as a simple setter-and-show helper for switching the active sorting mode.

### 2. Processing Pattern
```mermaid
flowchart TD
    A["vis"] --> B["Store integer key in instance field"]
    B --> C["Set tname to Hello then pop then incoming text"]
    C --> D["Show frame"]
    D --> E["Return 0"]
```

### 3. Parameter Analysis

| Parameter | Type | Meaning | Constraints | Usage |
|-----------|------|---------|-------------|-------|
| `k` | `int` | Sorting mode selector | Expected to match the button key values used elsewhere | Stored in instance field `key` |
| `str` | `String` | Display label for the active algorithm | May be any non-null string | Assigned to `tname` after intermediate test values |

### 4. CRUD Operations / Called Services

| Operation Type | Target | Details |
|----------------|--------|---------|
| Update | `key` | Stores the active mode identifier |
| Update | `tname` | Overwrites the label value, ending with the supplied string |
| Update | `frame.setVisible(true)` | Re-displays the UI |
| Return | `0` | Always returns zero despite the `int` signature |

### 5. Dependency Trace

| Dependency | Kind | Evidence / Usage |
|------------|------|------------------|
| `frame` | Instance UI state | Required before visibility can be toggled |
| `initialize()` | Initialization dependency | Must run before this method for `frame` to exist |
| `initialize()` submit logic | Behavioral dependency | Reads `key` and `tname` set here |

### 6. Per-Branch Detail Blocks

#### 6.1 Key assignment step
- Copies the supplied mode into the shared `key` field.
- This value determines which sort branch the Submit button will execute.

#### 6.2 Label assignment step
- Assigns two temporary strings before the final value.
- The final state of `tname` is always the `str` argument.
- The intermediate literals `Hello` and `pop` do not affect the final UI output.

#### 6.3 Visibility branch
- Makes the frame visible.
- This is useful if the UI was previously hidden through the Exit action.

#### 6.4 Return branch
- Returns `0` unconditionally.
- The return value is not used by the rest of the class.

## Method: Sortingss()

### 1. Role
Constructs the controller instance and immediately delegates to the UI initialization routine. This is the minimal object lifecycle entry point for the class.

### 2. Processing Pattern
```mermaid
flowchart TD
    A["constructor"] --> B["Call initialize"]
    B --> C["Build UI and wire actions"]
```

### 3. Parameter Analysis

| Parameter | Type | Meaning | Constraints | Usage |
|-----------|------|---------|-------------|-------|
| None | - | Constructor has no parameters | N/A | Immediately initializes the frame |

### 4. CRUD Operations / Called Services

| Operation Type | Target | Details |
|----------------|--------|---------|
| Action / Service Call | `initialize()` | Builds the complete UI state |

### 5. Dependency Trace

| Dependency | Kind | Evidence / Usage |
|------------|------|------------------|
| `initialize()` | Internal dependency | Direct constructor call |
| `main(String[] args)` | External caller | Instantiates the class during startup |

### 6. Per-Branch Detail Blocks

#### 6.1 Construction branch
- No conditional logic exists.
- The constructor always calls `initialize()`.
- All functional behavior is deferred to the initialization method.

## Method: initialize()

### 1. Role
Builds the entire application window, wires all UI controls, and attaches the button actions that drive sorting and code viewing. This method is the core composition point for the class because it defines the input model, output rendering, algorithm selection behavior, and file-opening behavior.

### 2. Processing Pattern
```mermaid
flowchart TD
    A["initialize"] --> B["Create JFrame and configure window bounds"]
    B --> C["Create title, input, output, and instruction components"]
    C --> D["Attach Submit handler"]
    C --> E["Attach Exit handler"]
    C --> F["Attach Open Code handler"]
    D --> G["Read text field content"]
    G --> H["Split input by spaces"]
    H --> I["Parse integers into array"]
    I --> J["Branch on key value"]
    J --> K["Bubble sort path when key equals 1"]
    J --> L["Selection sort path when key equals 2"]
    J --> M["Quick sort path when key equals 3"]
    J --> N["Insertion sort path when key equals 4"]
    J --> O["Merge sort path when key equals 5"]
    J --> P["Heap sort path when key equals 6"]
    K --> Q["Build result string"]
    L --> Q
    M --> Q
    N --> Q
    O --> Q
    P --> Q
    Q --> R["Update output area and title label"]
    E --> S["Hide frame"]
    F --> T["Launch Notepad for algorithm source file"]
```

### 3. Parameter Analysis

| Parameter | Type | Source | Meaning | Constraints | Usage |
|-----------|------|--------|---------|-------------|-------|
| None | - | - | The method has no parameters | N/A | Initializes the UI directly from instance state |

### 4. CRUD Operations / Called Services

| Operation Type | Target | Details |
|----------------|--------|---------|
| Create | `JFrame`, `JTextArea`, `JTextField`, `JLabel`, `JButton` | Instantiates and configures all screen components |
| Read | `textField.getText()` | Reads the user-entered integer sequence |
| Update | `textArea.setText(...)`, `textArea_1.setText(...)` | Displays the sorted output and selected algorithm name |
| Action / Service Call | `quiks.quickSort(...)`, `merg.mergeSort(...)`, `heaps.heapSort(...)` | Delegates to helper sorting implementations |
| Action / Service Call | `ProcessBuilder(...).start()` | Opens the algorithm source file in Notepad |

### 5. Dependency Trace

| Dependency | Kind | Evidence / Usage |
|------------|------|------------------|
| `Sortingss.main(String[] args)` | Internal caller | Constructor flow is triggered from application startup |
| `Sortingss.Sortingss()` | Internal caller | Constructor calls `initialize()` immediately |
| `quiks` | Helper class | Used for quick sort when `key == 3` |
| `merg` | Helper class | Used for merge sort when `key == 5` |
| `heaps` | Helper class | Used for heap sort when `key == 6` |
| `JFrame` / Swing controls | UI framework | Defines the interactive desktop shell |

### 6. Per-Branch Detail Blocks

#### 6.1 Window construction branch
- Allocates the main frame.
- Sets location and size to `100,100,514,329`.
- Uses absolute positioning via `null` layout.
- Adds the title/output areas, input field, labels, and buttons.

#### 6.2 Submit button branch
- Reads the input string from the text field.
- Splits the input on spaces into tokens.
- Converts each token to an integer.
- Selects one of six sorting behaviors using the instance field `key`.

#### 6.3 Bubble sort branch (`key == 1`)
- Uses a `flag`-based outer loop to keep scanning until no swaps occur.
- Compares adjacent values and swaps out-of-order pairs.
- Sets the output prefix to `Bubble Sort =`.

#### 6.4 Selection sort branch (`key == 2`)
- Iterates the array from left to right.
- Finds the minimum element in the unsorted suffix.
- Swaps it into the current position.
- Sets the algorithm label to `SELECTION SORT`.

#### 6.5 Quick sort branch (`key == 3`)
- Creates a `quiks` helper instance.
- Sorts the full array from index `0` to `size - 1`.
- Sets the output prefix to `Quick Sort =`.
- Sets the title label to `QUICK SORT`.

#### 6.6 Insertion sort branch (`key == 4`)
- Walks through the array from the second element onward.
- Inserts each value into its correct position among earlier elements.
- Sets the output prefix to `Insertion Sort =`.
- Sets the title label to `INSERTION SORT`.

#### 6.7 Merge sort branch (`key == 5`)
- Creates a `merg` helper instance.
- Sorts the array recursively from `0` to `num.length - 1`.
- Sets the output prefix to `Merge Sort =`.
- Sets the title label to `MERGE SORT`.

#### 6.8 Heap sort branch (`key == 6`)
- Creates a `heaps` helper instance.
- Sorts the array using heap sort with the token count as the heap size.
- Sets the output prefix to `Heap Sort =`.
- Sets the title label to `HEAP SORT`.

#### 6.9 Result rendering branch
- Converts each sorted integer back to a string.
- Appends the values to the result prefix.
- Renders the final string in the output area.
- Refreshes the title label with the selected algorithm name.

#### 6.10 Exit button branch
- Hides the frame by setting visibility to false.
- Does not terminate the JVM directly.

#### 6.11 Open Code button branch
- Checks the active `key` value.
- Opens the matching text file in Notepad.
- Uses one file per algorithm: `bubble.txt`, `selection.txt`, `quicksort.txt`, `insertion.txt`, `merge.txt`, or `maxheap.txt`.
- Catches and prints any launch exception.
