# (DD02) Business Logic — Sortingss.initialize() [229 LOC]

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

## 1. Role

### Sortingss.initialize()

This method initializes the Swing frame used for the sorting demonstration screen and wires together all user interface controls, event handlers, and output fields. It builds the page layout, defines the title and guidance labels, and prepares the input/output areas that allow a user to enter a space-separated integer list and see the sorted result.

From a business perspective, the method acts as the presentation-layer bootstrap for an algorithm selection and execution screen. It is responsible for launching the correct sorting flow based on the current `key` value, which is set elsewhere by the companion navigation method and represents the chosen sorting category. The implementation follows a routing/dispatch pattern inside the submit action handler: it parses user input, converts it to numeric data, executes one of six sorting strategies, and then renders both the sorted list and the selected algorithm name back to the screen.

The method also provides two auxiliary screen actions: one to close the window and another to open the reference source text file for the selected algorithm. In the larger system, it is the main UI orchestration method for the sorting module and serves as the entry point for the interactive screen behavior.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["initialize()"])
    FRAME(["Create JFrame and configure window"])
    TITLE(["Create title text area and style it"])
    INPUT(["Create input text field and input label"])
    OUTPUT(["Create output text area and output label"])
    SUBMIT(["Create Submit button and attach action listener"])
    PARSE(["Read input text and split by spaces"])
    CONVERT(["Convert tokens to integer array"])
    KEY1{"key == 1"}
    BUBBLE(["Run bubble sort and set fin and tname"])
    KEY2{"key == 2"}
    SELECT(["Run selection sort and set fin and tname"])
    KEY3{"key == 3"}
    QUICK(["Instantiate quiks and call quickSort()"])
    KEY4{"key == 4"}
    INSERT(["Run insertion sort and set fin and tname"])
    KEY5{"key == 5"}
    MERGE(["Instantiate merg and call mergeSort()"])
    KEY6{"key == 6"}
    HEAP(["Instantiate heaps and call heapSort()"])
    FORMAT(["Convert sorted numbers to output string"])
    UPDATE(["Set output text area and title area"])
    EXITBTN(["Create Exit button and attach action listener"])
    OPENBTN(["Create Open Code button and attach action listener"])
    OPEN1{"key == 1"}
    OPEN2{"key == 2"}
    OPEN3{"key == 3"}
    OPEN4{"key == 4"}
    OPEN5{"key == 5"}
    OPEN6{"key == 6"}
    END(["Return"])

    START --> FRAME --> TITLE --> INPUT --> OUTPUT --> SUBMIT --> PARSE --> CONVERT --> KEY1
    KEY1 -->|yes| BUBBLE --> FORMAT
    KEY1 -->|no| KEY2
    KEY2 -->|yes| SELECT --> FORMAT
    KEY2 -->|no| KEY3
    KEY3 -->|yes| QUICK --> FORMAT
    KEY3 -->|no| KEY4
    KEY4 -->|yes| INSERT --> FORMAT
    KEY4 -->|no| KEY5
    KEY5 -->|yes| MERGE --> FORMAT
    KEY5 -->|no| KEY6
    KEY6 -->|yes| HEAP --> FORMAT
    KEY6 -->|no| FORMAT
    FORMAT --> UPDATE --> EXITBTN --> OPENBTN --> OPEN1
    OPEN1 -->|yes| END
    OPEN1 -->|no| OPEN2
    OPEN2 -->|yes| END
    OPEN2 -->|no| OPEN3
    OPEN3 -->|yes| END
    OPEN3 -->|no| OPEN4
    OPEN4 -->|yes| END
    OPEN4 -->|no| OPEN5
    OPEN5 -->|yes| END
    OPEN5 -->|no| OPEN6
    OPEN6 -->|yes| END
    OPEN6 -->|no| END
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| - | (none) | - | - |

**Instance fields / external state read by this method:**
- `frame` — the Swing window instance created and configured by this method.
- `textField` — the input field whose contents are parsed into integers.
- `key` — algorithm selector used to choose the sort implementation.
- `tname` — title text written to the header area after sorting.

## 4. CRUD Operations / Called Services

This method does not perform database CRUD operations. It operates entirely in the UI layer and invokes local sorting routines plus desktop process launchers.

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `quickSort()` via `quiks` | - | In-memory integer array | Sorts the user-provided numbers using the quick sort algorithm. |
| R | `mergeSort()` via `merg` | - | In-memory integer array | Sorts the user-provided numbers using the merge sort algorithm. |
| R | `heapSort()` via `heaps` | - | In-memory integer array | Sorts the user-provided numbers using the heap sort algorithm. |

Notes:
- Bubble sort, selection sort, and insertion sort are implemented inline inside the event handler rather than delegated to external services.
- The `ProcessBuilder` calls in the Open Code handler are operating-system process launches, not CRUD operations.

## 5. Dependency Trace

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Screen: `Frame` | `Frame.initialize()` -> `new Sortingss()` -> `Sortingss.initialize()` | `quiks.quickSort() [R] In-memory integer array` |
| 2 | Screen: `Frame` | `Frame.initialize()` -> `obj.vis(1, "BUBBLE SORT")` -> `Sortingss.initialize()` | `merg.mergeSort() [R] In-memory integer array` |
| 3 | Screen: `Frame` | `Frame.initialize()` -> `obj.vis(4, "INSERTION SORT")` -> `Sortingss.initialize()` | `heaps.heapSort() [R] In-memory integer array` |
| 4 | Screen: `Frame` | `Frame.initialize()` -> `obj.vis(2, "SELECTION SORT")` -> `Sortingss.initialize()` | `Bubble sort inline [R] In-memory integer array` |
| 5 | Screen: `Frame` | `Frame.initialize()` -> `obj.vis(5, "MERGE SORT")` -> `Sortingss.initialize()` | `Selection sort inline [R] In-memory integer array` |
| 6 | Screen: `Frame` | `Frame.initialize()` -> `obj.vis(3, "QUICK SORT")` -> `Sortingss.initialize()` | `Insertion sort inline [R] In-memory integer array` |
| 7 | Screen: `Frame` | `Frame.initialize()` -> `obj.vis(6, "HEAP SORT")` -> `Sortingss.initialize()` | `Open Code ProcessBuilder [R] desktop file launch` |

## 6. Per-Branch Detail Blocks

**Block 1** — SEQUENCE `(screen and window setup)` (L204-L226)

> Builds the shared sorting UI and applies layout, styling, and field placement.

| # | Type | Code |
|---|------|------|
| 1 | SET | `frame = new JFrame();` |
| 2 | EXEC | `frame.setBounds(100, 100, 514, 329);` |
| 3 | EXEC | `frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);` |
| 4 | EXEC | `frame.getContentPane().setLayout(null);` |
| 5 | SET | `JTextArea textArea_1 = new JTextArea();` |
| 6 | EXEC | `textArea_1.setFont(new Font("Arial", Font.BOLD, 26));` |
| 7 | EXEC | `textArea_1.setBackground(Color.DARK_GRAY);` |
| 8 | EXEC | `textArea_1.setForeground(Color.WHITE);` |
| 9 | EXEC | `frame.getContentPane().add(textArea_1);` |
| 10 | SET | `textField = new JTextField();` |
| 11 | EXEC | `textField.setColumns(10);` |
| 12 | EXEC | `frame.getContentPane().add(textField);` |
| 13 | SET | `JLabel lblEnterInput = new JLabel("Enter Input");` |
| 14 | EXEC | `frame.getContentPane().add(lblEnterInput);` |
| 15 | SET | `JTextArea textArea = new JTextArea();` |
| 16 | EXEC | `textArea.setLineWrap(true);` |
| 17 | EXEC | `textArea.setEditable(false);` |
| 18 | EXEC | `frame.getContentPane().add(textArea);` |

**Block 2** — ACTION LISTENER `(Submit button action)` (L227-L303)

> Reads the entered numbers, chooses one sorting strategy, and prepares the final result string.

| # | Type | Code |
|---|------|------|
| 1 | SET | `JButton btnSubmit = new JButton("Submit");` |
| 2 | EXEC | `btnSubmit.addActionListener(new ActionListener() { ... });` |
| 3 | SET | `String fin = new String();` |
| 4 | SET | `String str = new String();` |
| 5 | EXEC | `str = textField.getText();` |
| 6 | SET | `String[] tok = str.split(" ");` |
| 7 | SET | `int[] num = new int[tok.length];` |
| 8 | FOR | `for (int i = 0; i < tok.length; i++)` |
| 9 | EXEC | `num[i] = Integer.parseInt(tok[i]);` |

**Block 2.1** — IF `(key == 1)` (L235)

> Executes the bubble sort path selected by the navigation screen.

| # | Type | Code |
|---|------|------|
| 1 | SET | `int j;` |
| 2 | SET | `boolean flag = true;` |
| 3 | SET | `int temp;` |
| 4 | WHILE | `while (flag)` |
| 5 | SET | `flag = false;` |
| 6 | FOR | `for (j = 0; j < num.length - 1; j++)` |
| 7 | IF | `if (num[j] > num[j + 1])` |
| 8 | SET | `temp = num[j];` |
| 9 | SET | `num[j] = num[j + 1];` |
| 10 | SET | `num[j + 1] = temp;` |
| 11 | SET | `flag = true;` |
| 12 | SET | `fin = "Bubble Sort =";` |

**Block 2.2** — IF `(key == 2)` (L248)

> Executes the selection sort path and labels the result for the selection sort screen.

| # | Type | Code |
|---|------|------|
| 1 | SET | `int n = num.length;` |
| 2 | FOR | `for (int i = 0; i < n - 1; i++)` |
| 3 | SET | `int min_idx = i;` |
| 4 | FOR | `for (int j = i + 1; j < n; j++)` |
| 5 | IF | `if (num[j] < num[min_idx])` |
| 6 | SET | `min_idx = j;` |
| 7 | SET | `int temp = num[min_idx];` |
| 8 | SET | `num[min_idx] = num[i];` |
| 9 | SET | `num[i] = temp;` |
| 10 | SET | `tname = "SELECTION SORT";` |
| 11 | SET | `fin = "Selection Sort =";` |

**Block 2.3** — IF `(key == 3)` (L260)

> Delegates sorting to the quick sort helper.

| # | Type | Code |
|---|------|------|
| 1 | SET | `quiks obj2 = new quiks();` |
| 2 | SET | `int size = num.length;` |
| 3 | CALL | `obj2.quickSort(num, 0, size - 1);` |
| 4 | SET | `fin = "Quick Sort =";` |
| 5 | SET | `tname = "QUICK SORT";` |

**Block 2.4** — IF `(key == 4)` (L271)

> Executes the insertion sort path.

| # | Type | Code |
|---|------|------|
| 1 | SET | `int i, ke, j;` |
| 2 | FOR | `for (i = 1; i < tok.length; i++)` |
| 3 | SET | `ke = num[i];` |
| 4 | SET | `j = i - 1;` |
| 5 | WHILE | `while (j >= 0 && num[j] > ke)` |
| 6 | SET | `num[j + 1] = num[j];` |
| 7 | SET | `j = j - 1;` |
| 8 | SET | `num[j + 1] = ke;` |
| 9 | SET | `tname = "INSERTION SORT";` |
| 10 | SET | `fin = "Insertion Sort =";` |

**Block 2.5** — IF `(key == 5)` (L283)

> Delegates sorting to the merge sort helper.

| # | Type | Code |
|---|------|------|
| 1 | SET | `merg obj3 = new merg();` |
| 2 | SET | `int size = num.length - 1;` |
| 3 | CALL | `obj3.mergeSort(num, 0, size);` |
| 4 | SET | `fin = "Merge Sort =";` |
| 5 | SET | `tname = "MERGE SORT";` |

**Block 2.6** — IF `(key == 6)` (L291)

> Delegates sorting to the heap sort helper.

| # | Type | Code |
|---|------|------|
| 1 | SET | `heaps obj4 = new heaps();` |
| 2 | SET | `int size;` |
| 3 | SET | `size = tok.length;` |
| 4 | CALL | `obj4.heapSort(num, size);` |
| 5 | SET | `fin = "Heap Sort =";` |
| 6 | SET | `tname = "HEAP SORT";` |

**Block 2.7** — FOR `(format sorted output)` (L299-L302)

> Converts the sorted integer array back into displayable text.

| # | Type | Code |
|---|------|------|
| 1 | SET | `String str1[] = new String[tok.length];` |
| 2 | FOR | `for (int i = 0; i < tok.length; i++)` |
| 3 | SET | `str1[i] = Integer.toString(num[i]);` |
| 4 | SET | `fin = fin + " " + str1[i];` |
| 5 | EXEC | `textArea.setText(fin);` |
| 6 | EXEC | `textArea_1.setText(tname);` |

**Block 3** — SEQUENCE `(footer actions)` (L304-L327)

> Adds the guidance labels, exit control, and code viewer control.

| # | Type | Code |
|---|------|------|
| 1 | SET | `JLabel lblIntegerMustBe = new JLabel("Integer must be space separated in your input");` |
| 2 | EXEC | `frame.getContentPane().add(lblIntegerMustBe);` |
| 3 | SET | `JLabel lblYourOutput = new JLabel("Your Output");` |
| 4 | EXEC | `frame.getContentPane().add(lblYourOutput);` |
| 5 | SET | `JButton btnExit = new JButton("Exit");` |
| 6 | EXEC | `btnExit.addActionListener(new ActionListener() { ... });` |
| 7 | EXEC | `frame.setVisible(false);` |
| 8 | SET | `JButton btnOpenCode = new JButton("Open Code");` |
| 9 | EXEC | `btnOpenCode.addActionListener(new ActionListener() { ... });` |

**Block 3.1** — IF / ELSE-IF chain `(key == 1 .. key == 6)` (L334-L376)

> Opens the source reference text file that matches the currently selected algorithm.

| # | Type | Code |
|---|------|------|
| 1 | IF | `if (key == 1)` |
| 2 | EXEC | `ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "bubble.txt");` |
| 3 | EXEC | `pb.start();` |
| 4 | IF | `if (key == 2)` |
| 5 | EXEC | `ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "selection.txt");` |
| 6 | EXEC | `pb.start();` |
| 7 | IF | `if (key == 3)` |
| 8 | EXEC | `ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "quicksort.txt");` |
| 9 | EXEC | `pb.start();` |
| 10 | IF | `if (key == 4)` |
| 11 | EXEC | `ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "insertion.txt");` |
| 12 | EXEC | `pb.start();` |
| 13 | IF | `if (key == 5)` |
| 14 | EXEC | `ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "merge.txt");` |
| 15 | EXEC | `pb.start();` |
| 16 | IF | `if (key == 6)` |
| 17 | EXEC | `ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "maxheap.txt");` |
| 18 | EXEC | `pb.start();` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `key` | Field | Algorithm selector used to choose which sorting implementation runs on submit. |
| `tname` | Field | Title label value shown at the top of the result area, representing the selected sorting method. |
| `fin` | Local variable | Final formatted output string shown to the user after sorting. |
| `str` | Local variable | Raw user-entered text from the input field. |
| `tok` | Local variable | Tokenized input values split by spaces. |
| `num` | Local variable | Integer array built from the entered values and then sorted. |
| `flag` | Local variable | Bubble sort repeat flag indicating whether another pass is needed. |
| `min_idx` | Local variable | Selection sort index of the current minimum value. |
| `ke` | Local variable | Insertion sort key value being inserted into the sorted prefix. |
| `temp` | Local variable | Temporary swap holder used in sorting logic. |
| `quiks` | Class | Quick sort helper class used to sort the numeric array. |
| `merg` | Class | Merge sort helper class used to sort the numeric array. |
| `heaps` | Class | Heap sort helper class used to sort the numeric array. |
| `Bubble Sort` | Business term | Sorting mode that repeatedly swaps adjacent out-of-order values. |
| `Selection Sort` | Business term | Sorting mode that repeatedly selects the minimum value from the unsorted portion. |
| `Quick Sort` | Business term | Divide-and-conquer sorting mode using partitioning. |
| `Insertion Sort` | Business term | Sorting mode that inserts each new item into the correct position in the growing sorted prefix. |
| `Merge Sort` | Business term | Divide-and-conquer sorting mode that merges sorted halves. |
| `Heap Sort` | Business term | Sorting mode that uses a heap structure to order values. |
| `ProcessBuilder` | Technical term | Java API used to launch an external desktop program. |
| `Notepad.exe` | Technical term | Windows editor launched to show the reference text file for the selected algorithm. |
| `bubble.txt` | File | Reference text file for the bubble sort implementation. |
| `selection.txt` | File | Reference text file for the selection sort implementation. |
| `quicksort.txt` | File | Reference text file for the quick sort implementation. |
| `insertion.txt` | File | Reference text file for the insertion sort implementation. |
| `merge.txt` | File | Reference text file for the merge sort implementation. |
| `maxheap.txt` | File | Reference text file for the heap sort implementation. |
| `frame` | Field | Main application window that hosts the sorting UI. |
| `JFrame` | Technical term | Swing top-level window component. |
| `JTextArea` | Technical term | Swing multi-line text display/input component. |
| `JTextField` | Technical term | Swing single-line text input component. |
| `JLabel` | Technical term | Swing static text component. |
| `JButton` | Technical term | Swing clickable action component. |
| `FTTH` | Acronym | Not used in this method. |
| `SOD` | Acronym | Not used in this method. |
| `JOKEN` | Acronym | Not used in this method. |
| `SC` | Acronym | Service Component; no SC endpoint is invoked by this method. |
| `CRUD` | Acronym | Create, Read, Update, Delete; no database CRUD is performed by this method. |
