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

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

## Class Overview

`Sortingss` is a Swing-based desktop UI that lets the user enter a space-separated list of integers, choose a sorting mode indirectly through the `key` field, and then run one of several sorting algorithms. The class acts as the presentation and orchestration layer for bubble sort, selection sort, quick sort, insertion sort, merge sort, and heap sort, while also rendering the resulting output back into the window. It mixes GUI setup, input parsing, algorithm dispatch, and output rendering in a single class, so its design is primarily procedural rather than strongly layered.

---

## Method: initialize()

### 1. Role

`initialize()` builds the complete Swing user interface for the sorting window and wires the event handlers that drive all runtime behavior. It prepares the frame, creates the input and output widgets, and attaches the Submit, Exit, and Open Code actions. The method also contains the algorithm selection dispatch logic that decides which sorting routine to run based on the current `key` value.

### 2. Processing Pattern

```mermaid
flowchart TD
A["initialize()"] --> B["Create JFrame and configure bounds, close behavior, and null layout"]
B --> C["Create title text area and input field"]
C --> D["Create output text area and labels"]
D --> E["Create Submit button"]
E --> F["Read text field, split by spaces, parse ints"]
F --> G{"key value"}
G -->|"1"| H["Bubble sort loop"]
G -->|"2"| I["Selection sort loop"]
G -->|"3"| J["quiks.quickSort(num, 0, size - 1)"]
G -->|"4"| K["Insertion sort loop"]
G -->|"5"| L["merg.mergeSort(num, 0, size)"]
G -->|"6"| M["heaps.heapSort(num, size)"]
H --> N["Render sorted values and title"]
I --> N
J --> N
K --> N
L --> N
M --> N
N --> O["Update output fields"]
O --> P["Create Exit button to hide frame"]
P --> Q["Create Open Code button to open algorithm note file by key"]
```

### 3. Parameter Analysis

| Parameter | Type | Direction | Meaning | Notes |
|---|---|---|---|---|
| none | - | - | `initialize()` takes no parameters | It operates on the instance fields `frame`, `textField`, `key`, and `tname` |

### 4. CRUD Operations / Called Services

| Operation Type | Target | Details |
|---|---|---|
| Create | `JFrame`, `JTextArea`, `JTextField`, `JLabel`, `JButton` | Constructs the full UI surface |
| Read | `textField.getText()` | Reads the user-entered integer sequence |
| Update | `textArea.setText()`, `textArea_1.setText()` | Writes the sorted result and algorithm title |
| Call | `quiks.quickSort()`, `merg.mergeSort()`, `heaps.heapSort()` | Delegates sorting to helper classes |
| Call | `ProcessBuilder` | Opens the algorithm text file associated with the selected `key` |
| Call | `frame.setVisible(false)` | Hides the window when Exit is clicked |

### 5. Dependency Trace

| Dependency Type | Element | Purpose |
|---|---|---|
| Field dependency | `frame` | Root Swing container configured and displayed by the method |
| Field dependency | `textField` | Input source for the number list |
| Field dependency | `key` | Selects which algorithm branch executes |
| Field dependency | `tname` | Stores the title shown in the header area |
| Internal call | `quiks` | Provides quick sort behavior for `key == 3` |
| Internal call | `merg` | Provides merge sort behavior for `key == 5` |
| Internal call | `heaps` | Provides heap sort behavior for `key == 6` |
| Caller | `Sortingss()` constructor | Invokes `initialize()` during object creation |
| Caller | `main(String[] args)` | Creates the object whose constructor triggers this method |
| Caller | `vis(int k, String str)` | Relies on the initialized frame already existing so it can be shown |

### 6. Per-Branch Detail Blocks

#### 6.1 Frame setup branch
- Creates a new `JFrame` and sets the application window size to 514 x 329.
- Sets `JFrame.EXIT_ON_CLOSE` so the application exits when the window is closed.
- Uses absolute positioning by applying `null` layout to the content pane.

#### 6.2 Title area branch
- Creates `textArea_1` for the algorithm title display.
- Applies Arial Bold 26-point styling.
- Uses a dark gray background and white foreground to make the header visually distinct.

#### 6.3 Input branch
- Creates `textField` for user input.
- Places the `Enter Input` label next to it.
- The UI instruction implies the input must be space-separated integers.

#### 6.4 Output branch
- Creates a read-only output `JTextArea` with line wrapping enabled.
- Adds the `Your Output` label to identify the results area.
- After sorting, the method concatenates the sorted integers into a display string.

#### 6.5 Submit action branch
- Reads the current text field value.
- Splits input by spaces and converts every token to `int`.
- Dispatches to one of six algorithm branches based on the current `key` value.
- After sorting, it reconstructs the result string and updates both the output area and the title area.

#### 6.6 Bubble sort branch (`key == 1`)
- Uses a `flag`-driven loop that repeats until no swap occurs.
- Compares adjacent elements and swaps them when out of order.
- Sets the title text to indicate Bubble Sort.
- Because the code uses a classic pass-until-stable strategy, the worst-case behavior is quadratic.

#### 6.7 Selection sort branch (`key == 2`)
- Scans the unsorted suffix to find the smallest remaining value.
- Swaps the minimum into the current position.
- Updates `tname` to `SELECTION SORT` and the output prefix to `Selection Sort =`.
- This branch also runs in quadratic time and performs in-place sorting.

#### 6.8 Quick sort branch (`key == 3`)
- Instantiates `quiks` and sorts the array with `quickSort(num, 0, size - 1)`.
- The helper class performs partition-based recursive quick sort.
- Updates the title to `QUICK SORT` and the output prefix to `Quick Sort =`.
- This branch delegates all sorting logic to the helper class.

#### 6.9 Insertion sort branch (`key == 4`)
- Iterates through the array from left to right.
- Inserts each value into its correct position in the already-sorted prefix.
- Updates `tname` to `INSERTION SORT` and the output prefix to `Insertion Sort =`.
- This branch is stable and in-place.

#### 6.10 Merge sort branch (`key == 5`)
- Instantiates `merg` and calls `mergeSort(num, 0, size)` where `size = num.length - 1`.
- The helper recursively splits the array and merges sorted halves.
- Updates the title to `MERGE SORT` and the output prefix to `Merge Sort =`.
- This branch delegates all recursion and merge logic to the helper class.

#### 6.11 Heap sort branch (`key == 6`)
- Instantiates `heaps` and calls `heapSort(num, size)` where `size = tok.length`.
- The helper builds a max heap and repeatedly extracts the largest element.
- Updates the title to `HEAP SORT` and the output prefix to `Heap Sort =`.
- Sorting is done in place through the helper class.

#### 6.12 Result rendering branch
- Converts each sorted integer back to `String`.
- Appends values to the output prefix one by one.
- Writes the final result to the output text area and the selected algorithm name to the header area.

#### 6.13 Exit button branch
- Adds an Exit button to the frame.
- The action handler simply hides the frame by calling `frame.setVisible(false)`.
- The application process remains alive if other non-daemon threads exist, but the UI is no longer visible.

#### 6.14 Open Code button branch
- Adds an Open Code button to the frame.
- Uses the selected `key` to decide which text file to open.
- Launches `Notepad.exe` with one of `bubble.txt`, `selection.txt`, `quicksort.txt`, `insertion.txt`, `merge.txt`, or `maxheap.txt`.
- This is a user assistance feature that exposes the source explanation for the chosen algorithm.

---

## Method: main(String[] args)

### 1. Role

`main(String[] args)` is the application entry point. It schedules the creation and display of the `Sortingss` window on the AWT event dispatch thread so that Swing UI initialization runs in the correct thread context. The method also guards startup with a broad exception handler that prints failures to standard error output.

### 2. Processing Pattern

```mermaid
flowchart TD
A["main(String[] args)"] --> B["Schedule GUI creation on EventQueue"]
B --> C["Instantiate Sortingss"]
C --> D["Call initialize() via constructor"]
D --> E["Show frame"]
E --> F{"Exception during startup?"}
F -->|"yes"| G["Print stack trace"]
F -->|"no"| H["Application remains running"]
```

### 3. Parameter Analysis

| Parameter | Type | Direction | Meaning | Notes |
|---|---|---|---|---|
| args | `String[]` | In | Standard JVM command-line arguments | The method does not inspect the contents |

### 4. CRUD Operations / Called Services

| Operation Type | Target | Details |
|---|---|---|
| Call | `EventQueue.invokeLater()` | Defers UI startup to the Swing event thread |
| Call | `new Sortingss()` | Creates the main window instance |
| Call | `window.frame.setVisible(true)` | Displays the initialized frame |
| Call | `e.printStackTrace()` | Logs startup exceptions |

### 5. Dependency Trace

| Dependency Type | Element | Purpose |
|---|---|---|
| Called class | `Sortingss` constructor | Builds the UI and triggers `initialize()` |
| Runtime dependency | `EventQueue` | Ensures Swing thread safety |
| Caller | JVM launcher | Invokes `main()` as the application entry point |

### 6. Per-Branch Detail Blocks

#### 6.1 Normal startup branch
- Wraps GUI creation inside `EventQueue.invokeLater(...)`.
- Constructs a new `Sortingss` instance.
- Makes the frame visible after construction completes.
- This is the standard Swing startup pattern.

#### 6.2 Failure branch
- Catches any exception thrown during window creation or display.
- Prints the stack trace for diagnosis.
- The method does not attempt recovery or retry.

---

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

### 1. Role

`vis(int k, String str)` stores the currently selected sorting key and a display string, then makes the frame visible. In practice, this method looks like a small UI state setter used to prime the window before display. It returns a constant integer value of `0` and does not otherwise transform the input.

### 2. Processing Pattern

```mermaid
flowchart TD
A["vis(int k, String str)"] --> B["Store k in key"]
B --> C["Assign tname to placeholder values"]
C --> D["Assign tname = str"]
D --> E["Show frame"]
E --> F["Return 0"]
```

### 3. Parameter Analysis

| Parameter | Type | Direction | Meaning | Notes |
|---|---|---|---|---|
| k | `int` | In | Selected algorithm key | Determines which sorting branch `initialize()` will use when Submit is pressed |
| str | `String` | In | Text to store in `tname` | The method ultimately assigns this to the title field |

### 4. CRUD Operations / Called Services

| Operation Type | Target | Details |
|---|---|---|
| Update | `key` field | Stores the selected algorithm identifier |
| Update | `tname` field | Temporarily assigns placeholder values, then sets it to `str` |
| Call | `frame.setVisible(true)` | Displays the frame |
| Return | `0` | Returns a fixed success-like value |

### 5. Dependency Trace

| Dependency Type | Element | Purpose |
|---|---|---|
| Field dependency | `key` | Controls which algorithm branch the UI will execute later |
| Field dependency | `tname` | Holds the visible title value |
| Field dependency | `frame` | Must already exist for visibility to be changed |
| Caller | External UI flow | Likely invoked after a menu or navigation selection to bring the window forward |

### 6. Per-Branch Detail Blocks

#### 6.1 State assignment branch
- Sets the instance field `key` to the provided value `k`.
- Writes placeholder strings to `tname` before assigning the final input string.
- The intermediate placeholder assignments do not affect the returned value or frame behavior.

#### 6.2 Visibility branch
- Calls `frame.setVisible(true)` to show the existing window.
- This assumes the frame has already been created by `initialize()`.
- The method does not configure layout or components.

#### 6.3 Return branch
- Returns `0` unconditionally.
- No computation is performed on the returned value.
- The return type is therefore effectively used as a fixed status code.
