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

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

## 1. Role

### Sortingss.initialize()

This method builds and wires the complete Swing user interface for the sorting tool screen. It creates the frame, places all input/output controls, and registers event handlers for the Submit, Exit, and Open Code buttons. From a business perspective, it is the screen bootstrapper that turns a raw integer list entered by the user into a selected sorting result and displays both the sorted output and the selected algorithm name.

The method acts as a routing and dispatch point for six sorting service types: Bubble Sort, Selection Sort, Quick Sort, Insertion Sort, Merge Sort, and Heap Sort. The selected sorting mode is driven by the instance field `key`, and each branch dispatches to the corresponding algorithm implementation or inline sorting routine. It also performs post-processing by reformatting the sorted integers back into a space-delimited string for display.

In the larger system, this method is the local entry point for the `Sortingss` screen and is invoked from the class constructor. It follows an event-driven UI pattern: the screen is created once, and the sorting work is deferred until the user clicks Submit. The Open Code action provides supporting documentation or source-code access for the currently selected algorithm by launching a text file in Notepad.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["initialize()"])
    CREATE_FRAME(["Create JFrame and configure size, close behavior, and layout"])
    ADD_HEADER(["Create title text area and add to frame"])
    ADD_INPUT(["Create input text field and 'Enter Input' label"])
    ADD_OUTPUT(["Create output text area and 'Your Output' label"])
    ADD_SUBMIT(["Create Submit button and register ActionListener"])
    SUBMIT_CLICK(["User clicks Submit"])
    READ_INPUT(["Read textField.getText() and split by space"])
    PARSE_NUMS(["Parse tokens into int[] num"])
    KEY_CHECK(["Evaluate key selection"])
    KEY1(["key == 1 - Bubble Sort"])
    BUBBLE(["Perform bubble sort in while/for loops"])
    KEY2(["key == 2 - Selection Sort"])
    SELECTION(["Perform selection sort in nested loops"])
    KEY3(["key == 3 - Quick Sort"])
    QUICK_CALL(["Create quiks and call quickSort(num, 0, size-1)"])
    KEY4(["key == 4 - Insertion Sort"])
    INSERTION(["Perform insertion sort in for/while loops"])
    KEY5(["key == 5 - Merge Sort"])
    MERGE_CALL(["Create merg and call mergeSort(num, 0, size)"])
    KEY6(["key == 6 - Heap Sort"])
    HEAP_CALL(["Create heaps and call heapSort(num, size)"])
    FORMAT(["Convert sorted numbers back to String[] and build fin"])
    UPDATE_VIEW(["Set output textArea and textArea_1"])
    ADD_EXIT(["Create Exit button and register ActionListener"])
    EXIT_CLICK(["User clicks Exit"])
    HIDE_FRAME(["frame.setVisible(false)"])
    ADD_OPEN(["Create Open Code button and register ActionListener"])
    OPEN_CLICK(["User clicks Open Code"])
    OPEN_KEY(["Evaluate key selection for source file"])
    OPEN1(["key == 1 - open bubble.txt"])
    OPEN2(["key == 2 - open selection.txt"])
    OPEN3(["key == 3 - open quicksort.txt"])
    OPEN4(["key == 4 - open insertion.txt"])
    OPEN5(["key == 5 - open merge.txt"])
    OPEN6(["key == 6 - open maxheap.txt"])
    END_NODE(["Return / Next"])

    START --> CREATE_FRAME --> ADD_HEADER --> ADD_INPUT --> ADD_OUTPUT --> ADD_SUBMIT --> ADD_EXIT --> ADD_OPEN --> END_NODE
    SUBMIT_CLICK --> READ_INPUT --> PARSE_NUMS --> KEY_CHECK
    KEY_CHECK --> KEY1 --> BUBBLE --> FORMAT
    KEY_CHECK --> KEY2 --> SELECTION --> FORMAT
    KEY_CHECK --> KEY3 --> QUICK_CALL --> FORMAT
    KEY_CHECK --> KEY4 --> INSERTION --> FORMAT
    KEY_CHECK --> KEY5 --> MERGE_CALL --> FORMAT
    KEY_CHECK --> KEY6 --> HEAP_CALL --> FORMAT
    FORMAT --> UPDATE_VIEW
    EXIT_CLICK --> HIDE_FRAME
    OPEN_CLICK --> OPEN_KEY
    OPEN_KEY --> OPEN1 --> END_NODE
    OPEN_KEY --> OPEN2 --> END_NODE
    OPEN_KEY --> OPEN3 --> END_NODE
    OPEN_KEY --> OPEN4 --> END_NODE
    OPEN_KEY --> OPEN5 --> END_NODE
    OPEN_KEY --> OPEN6 --> END_NODE
```

## 3. Parameter Analysis

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

External state read by the method:
- `key` — determines which sorting algorithm branch runs and which source file can be opened.
- `textField` — source of the user-entered space-separated integer list.
- `textArea` — target for the sorted result string.
- `textArea_1` — target for the selected algorithm name.
- `frame` — the Swing window container that receives all components and is later hidden by Exit.
- `tname` — selected algorithm label written to the header area.

## 4. CRUD Operations / Called Services

### Pre-computed evidence from code analysis graph:

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| - | `quiks.quickSort` | quiks | - | Calls `quickSort` in `quiks` |
| U | `merg.mergeSort` | merg | - | Calls `mergeSort` in `merg` |
| - | `heaps.heapSort` | heaps | - | Calls `heapSort` 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 | `textField.getText` | - | Swing input field | Reads the user-entered integer list from the input control |
| - | `str.split` | - | In-memory string | Splits the input string into integer tokens |
| - | `Integer.parseInt` | - | In-memory conversion | Converts each token into an integer for sorting |
| - | `quiks.quickSort` | quiks | In-memory array `num` | Sorts the integer list using quick sort |
| U | `merg.mergeSort` | merg | In-memory array `num` | Reorders the integer list using merge sort |
| - | `heaps.heapSort` | heaps | In-memory array `num` | Reorders the integer list using heap sort |
| R | `Integer.toString` | - | In-memory conversion | Converts sorted integers back to displayable strings |
| U | `textArea.setText` | - | Swing output field | Updates the output area with the sorted result |
| U | `textArea_1.setText` | - | Swing header field | Updates the displayed algorithm name |
| D | `frame.setVisible(false)` | - | Swing frame | Hides the window when Exit is clicked |
| R | `new ProcessBuilder(...).start()` | - | Operating system / local file | Launches Notepad to open the algorithm text file for reference |

## 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 | `Sortingss` constructor | `Sortingss.<init>() -> Sortingss.initialize()` | `quiks.quickSort [R] in-memory array` |
| 1 | `Sortingss` constructor | `Sortingss.<init>() -> Sortingss.initialize()` | `merg.mergeSort [U] in-memory array` |
| 1 | `Sortingss` constructor | `Sortingss.<init>() -> Sortingss.initialize()` | `heaps.heapSort [U] in-memory array` |
| 1 | `Sortingss` constructor | `Sortingss.<init>() -> Sortingss.initialize()` | `frame.setVisible(false) [D] Swing frame` |
| 1 | `Sortingss` constructor | `Sortingss.<init>() -> Sortingss.initialize()` | `new ProcessBuilder(...).start() [R] local file` |
| 2 | Screen: `Frame` | `Frame.<init>() -> Frame.initialize() -> Sortingss.initialize()` | `quiks.quickSort [R] in-memory array` |
| 2 | Screen: `Frame` | `Frame.<init>() -> Frame.initialize() -> Sortingss.initialize()` | `merg.mergeSort [U] in-memory array` |
| 2 | Screen: `Frame` | `Frame.<init>() -> Frame.initialize() -> Sortingss.initialize()` | `heaps.heapSort [U] in-memory array` |
| 2 | Screen: `Frame` | `Frame.<init>() -> Frame.initialize() -> Sortingss.initialize()` | `frame.setVisible(false) [D] Swing frame` |
| 2 | Screen: `Frame` | `Frame.<init>() -> Frame.initialize() -> Sortingss.initialize()` | `new ProcessBuilder(...).start() [R] local file` |

## 6. Per-Branch Detail Blocks

**Block 1** — `initialize()` (L204)

> Builds the sorting UI shell and attaches the interactive controls.

| # | 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);` |

**Block 2** — UI header and input/output component setup (L208-L226)

| # | Type | Code |
|---|------|------|
| 1 | SET | `JTextArea textArea_1 = new JTextArea();` |
| 2 | EXEC | `textArea_1.setFont(new Font("Arial", Font.BOLD, 26));` |
| 3 | EXEC | `textArea_1.setBackground(Color.DARK_GRAY);` |
| 4 | EXEC | `textArea_1.setForeground(Color.WHITE);` |
| 5 | EXEC | `textArea_1.setBounds(102, 0, 304, 41);` |
| 6 | EXEC | `frame.getContentPane().add(textArea_1);` |
| 7 | SET | `textField = new JTextField();` |
| 8 | EXEC | `textField.setBounds(102, 102, 304, 22);` |
| 9 | EXEC | `frame.getContentPane().add(textField);` |
| 10 | EXEC | `textField.setColumns(10);` |
| 11 | SET | `JLabel lblEnterInput = new JLabel("Enter Input");` |
| 12 | EXEC | `lblEnterInput.setBounds(30, 105, 85, 16);` |
| 13 | EXEC | `frame.getContentPane().add(lblEnterInput);` |
| 14 | SET | `JTextArea textArea = new JTextArea();` |
| 15 | EXEC | `textArea.setLineWrap(true);` |
| 16 | EXEC | `textArea.setEditable(false);` |
| 17 | EXEC | `textArea.setBounds(102, 175, 304, 56);` |
| 18 | EXEC | `frame.getContentPane().add(textArea);` |

**Block 3** — Submit button listener registration (L228-L231)

| # | Type | Code |
|---|------|------|
| 1 | SET | `JButton btnSubmit = new JButton("Submit");` |
| 2 | EXEC | `btnSubmit.addActionListener(new ActionListener() { ... });` |
| 3 | EXEC | `frame.getContentPane().add(btnSubmit);` |

**Block 3.1** — `actionPerformed(ActionEvent e)` for Submit (L229-L318)

> Reads the user input, routes the selected sort algorithm, and refreshes the output labels.

| # | Type | Code |
|---|------|------|
| 1 | SET | `String fin = new String();` |
| 2 | SET | `String str = new String();` |
| 3 | EXEC | `str = textField.getText();` |
| 4 | EXEC | `String[] tok = str.split(" ");` |
| 5 | SET | `int[] num = new int[tok.length];` |
| 6 | FOR | `for (int i = 0; i < tok.length; i++)` |
| 7 | EXEC | `num[i] = Integer.parseInt(tok[i]);` |
| 8 | IF | `if (key == 1)` |
| 9 | SET | `int j;` |
| 10 | SET | `boolean flag = true;` |
| 11 | SET | `int temp;` |
| 12 | WHILE | `while (flag)` |
| 13 | SET | `flag = false;` |
| 14 | FOR | `for (j = 0; j < num.length - 1; j++)` |
| 15 | IF | `if (num[j] > num[j + 1])` |
| 16 | SET | `temp = num[j];` |
| 17 | SET | `num[j] = num[j + 1];` |
| 18 | SET | `num[j + 1] = temp;` |
| 19 | SET | `flag = true;` |
| 20 | SET | `fin = "Bubble Sort =";` |
| 21 | IF | `if (key == 2)` |
| 22 | SET | `int n = num.length;` |
| 23 | FOR | `for (int i = 0; i < n - 1; i++)` |
| 24 | SET | `int min_idx = i;` |
| 25 | FOR | `for (int j = i + 1; j < n; j++)` |
| 26 | IF | `if (num[j] < num[min_idx])` |
| 27 | SET | `min_idx = j;` |
| 28 | SET | `int temp = num[min_idx];` |
| 29 | SET | `num[min_idx] = num[i];` |
| 30 | SET | `num[i] = temp;` |
| 31 | SET | `tname = "SELECTION SORT";` |
| 32 | SET | `fin = "Selection Sort =";` |
| 33 | IF | `if (key == 3)` |
| 34 | SET | `quiks obj2 = new quiks();` |
| 35 | SET | `int size = num.length;` |
| 36 | CALL | `obj2.quickSort(num, 0, size - 1);` |
| 37 | SET | `fin = "Quick Sort =";` |
| 38 | SET | `tname = "QUICK SORT";` |
| 39 | IF | `if (key == 4)` |
| 40 | SET | `int i, ke, j;` |
| 41 | FOR | `for (i = 1; i < tok.length; i++)` |
| 42 | SET | `ke = num[i];` |
| 43 | SET | `j = i - 1;` |
| 44 | WHILE | `while (j >= 0 && num[j] > ke)` |
| 45 | SET | `num[j + 1] = num[j];` |
| 46 | SET | `j = j - 1;` |
| 47 | SET | `num[j + 1] = ke;` |
| 48 | SET | `tname = "INSERTION SORT";` |
| 49 | SET | `fin = "Insertion Sort =";` |
| 50 | IF | `if (key == 5)` |
| 51 | SET | `merg obj3 = new merg();` |
| 52 | SET | `int size = num.length - 1;` |
| 53 | CALL | `obj3.mergeSort(num, 0, size);` |
| 54 | SET | `fin = "Merge Sort =";` |
| 55 | SET | `tname = "MERGE SORT";` |
| 56 | IF | `if (key == 6)` |
| 57 | SET | `heaps obj4 = new heaps();` |
| 58 | SET | `int size;` |
| 59 | SET | `size = tok.length;` |
| 60 | CALL | `obj4.heapSort(num, size);` |
| 61 | SET | `fin = "Heap Sort =";` |
| 62 | SET | `tname = "HEAP SORT";` |
| 63 | SET | `String str1[] = new String[tok.length];` |
| 64 | FOR | `for (int i = 0; i < tok.length; i++)` |
| 65 | SET | `str1[i] = Integer.toString(num[i]);` |
| 66 | SET | `fin = fin + " " + str1[i];` |
| 67 | EXEC | `textArea.setText(fin);` |
| 68 | EXEC | `textArea_1.setText(tname);` |

**Block 4** — Exit button listener registration (L320-L328)

| # | Type | Code |
|---|------|------|
| 1 | SET | `JButton btnExit = new JButton("Exit");` |
| 2 | EXEC | `btnExit.addActionListener(new ActionListener() { ... });` |
| 3 | EXEC | `frame.getContentPane().add(btnExit);` |

**Block 4.1** — `actionPerformed(ActionEvent e)` for Exit (L321-L326)

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `frame.setVisible(false);` |

**Block 5** — Open Code button listener registration (L330-L373)

| # | Type | Code |
|---|------|------|
| 1 | SET | `JButton btnOpenCode = new JButton("Open Code");` |
| 2 | EXEC | `btnOpenCode.addActionListener(new ActionListener() { ... });` |
| 3 | EXEC | `frame.getContentPane().add(btnOpenCode);` |

**Block 5.1** — `actionPerformed(ActionEvent e)` for Open Code (L331-L372)

> Launches the source/reference text file that corresponds to the selected sorting algorithm.

| # | Type | Code |
|---|------|------|
| 1 | IF | `if (key == 1)` |
| 2 | TRY | `try` |
| 3 | SET | `ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "bubble.txt");` |
| 4 | CALL | `pb.start();` |
| 5 | CATCH | `catch (Exception ew)` |
| 6 | EXEC | `System.out.println(ew.toString());` |
| 7 | IF | `if (key == 2)` |
| 8 | TRY | `try` |
| 9 | SET | `ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "selection.txt");` |
| 10 | CALL | `pb.start();` |
| 11 | CATCH | `catch (Exception ew)` |
| 12 | EXEC | `System.out.println(ew.toString());` |
| 13 | IF | `if (key == 3)` |
| 14 | TRY | `try` |
| 15 | SET | `ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "quicksort.txt");` |
| 16 | CALL | `pb.start();` |
| 17 | CATCH | `catch (Exception ew)` |
| 18 | EXEC | `System.out.println(ew.toString());` |
| 19 | IF | `if (key == 4)` |
| 20 | TRY | `try` |
| 21 | SET | `ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "insertion.txt");` |
| 22 | CALL | `pb.start();` |
| 23 | CATCH | `catch (Exception ew)` |
| 24 | EXEC | `System.out.println(ew.toString());` |
| 25 | IF | `if (key == 5)` |
| 26 | TRY | `try` |
| 27 | SET | `ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "merge.txt");` |
| 28 | CALL | `pb.start();` |
| 29 | CATCH | `catch (Exception ew)` |
| 30 | EXEC | `System.out.println(ew.toString());` |
| 31 | IF | `if (key == 6)` |
| 32 | TRY | `try` |
| 33 | SET | `ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "maxheap.txt");` |
| 34 | CALL | `pb.start();` |
| 35 | CATCH | `catch (Exception ew)` |
| 36 | EXEC | `System.out.println(ew.toString());` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `key` | Field | Algorithm selector that determines which sorting routine and reference file are used |
| `textField` | Field | User input field for the space-separated integer list |
| `textArea` | Field | Output field that displays the sorted result string |
| `textArea_1` | Field | Header field that displays the selected algorithm name |
| `frame` | Field | Main Swing window container for the sorting screen |
| `fin` | Variable | Final output label plus sorted number list shown in the output area |
| `str` | Variable | Raw text entered by the user before tokenization |
| `tok` | Variable | Token array produced by splitting the input string by spaces |
| `num` | Variable | Integer array that is sorted in memory |
| `tname` | Variable | Title name shown in the header area for the chosen algorithm |
| Bubble Sort | Business term | Comparison-based sorting method that repeatedly swaps adjacent out-of-order elements |
| Selection Sort | Business term | Sorting method that repeatedly selects the minimum remaining element |
| Quick Sort | Business term | Partition-based recursive sorting method |
| Insertion Sort | Business term | Sorting method that builds the final order one item at a time |
| Merge Sort | Business term | Divide-and-conquer sorting method that merges sorted subarrays |
| Heap Sort | Business term | Sorting method that uses a heap structure to order elements |
| `quiks` | Class | Quick sort implementation class used by the screen |
| `merg` | Class | Merge sort implementation class used by the screen |
| `heaps` | Class | Heap sort implementation class used by the screen |
| `ActionListener` | Technical term | Swing event handler interface invoked when a button is clicked |
| `ActionEvent` | Technical term | Swing event object representing the user action that triggered a listener |
| `ProcessBuilder` | Technical term | Java API used here to launch Notepad with the selected text file |
| `Notepad.exe` | External application | Windows text editor used to open algorithm reference content |
| `bubble.txt` | File | Reference file for bubble sort content |
| `selection.txt` | File | Reference file for selection sort content |
| `quicksort.txt` | File | Reference file for quick sort content |
| `insertion.txt` | File | Reference file for insertion sort content |
| `merge.txt` | File | Reference file for merge sort content |
| `maxheap.txt` | File | Reference file for heap sort content |
| `EXIT_ON_CLOSE` | Swing constant | Closes the application when the frame is closed |
| `DARK_GRAY` | UI constant | Dark background color used for the title area |
| `WHITE` | UI constant | White foreground color used for the title area |
| `Arial` | Font family | Typeface used for the title and instruction labels |
| `Arial Black` | Font family | Typeface used for the instruction banner |
