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

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

## 1. Role

### Sortingss.initialize()

`initialize()` is the UI composition method for the sorting demonstration window. It creates and configures the main frame, input field, output areas, descriptive labels, and action buttons that let the user enter a space-separated integer list and execute one of several sorting demonstrations. The method supports six business branches, each tied to a sort mode selected through the surrounding screen state: bubble sort, selection sort, quick sort, insertion sort, merge sort, and heap sort. For the algorithm branches that are delegated, it acts as a routing and orchestration point; for the in-method algorithms, it performs the full sort inline inside the button listener. It also includes a separate “Open Code” action that opens the corresponding text file for the selected algorithm, which positions the screen as both an executable demo and a learning aid. In the larger system, this method is the UI entry point for the `Sortingss` screen and is invoked automatically from the class constructor so the window is ready as soon as the object is created.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START["initialize()"]
    FRAME["Create JFrame and configure window properties"]
    TITLE["Create title area JTextArea"]
    INPUT["Create input JTextField and prompt label"]
    OUTPUT["Create output JTextArea and label"]
    SUBMIT["Submit button action"]
    READ["Read textField.getText()"]
    SPLIT["Split input by spaces"]
    PARSE["Parse each token to int"]
    KEY1{"key == 1"}
    BUBBLE["Bubble sort loop and swap pass"]
    KEY2{"key == 2"}
    SELECT["Selection sort loop and swap minimum"]
    KEY3{"key == 3"}
    QUICK["Instantiate quiks and call quickSort(num, 0, size-1)"]
    KEY4{"key == 4"}
    INSERT["Insertion sort loop"]
    KEY5{"key == 5"}
    MERGE["Instantiate merg and call mergeSort(num, 0, size)"]
    KEY6{"key == 6"}
    HEAP["Instantiate heaps and call heapSort(num, size)"]
    FORMAT["Build result string and update output areas"]
    EXITBTN["Exit button action hides frame"]
    OPENCODE["Open Code button action"]
    END["Return / frame ready"]
    START --> FRAME
    FRAME --> TITLE
    TITLE --> INPUT
    INPUT --> OUTPUT
    OUTPUT --> SUBMIT
    SUBMIT --> READ
    READ --> SPLIT
    SPLIT --> PARSE
    PARSE --> KEY1
    KEY1 -->|Yes| BUBBLE
    KEY1 -->|No| KEY2
    KEY2 -->|Yes| SELECT
    KEY2 -->|No| KEY3
    KEY3 -->|Yes| QUICK
    KEY3 -->|No| KEY4
    KEY4 -->|Yes| INSERT
    KEY4 -->|No| KEY5
    KEY5 -->|Yes| MERGE
    KEY5 -->|No| KEY6
    KEY6 -->|Yes| HEAP
    KEY6 -->|No| FORMAT
    BUBBLE --> FORMAT
    SELECT --> FORMAT
    QUICK --> FORMAT
    INSERT --> FORMAT
    MERGE --> FORMAT
    HEAP --> FORMAT
    FORMAT --> EXITBTN
    EXITBTN --> OPENCODE
    OPENCODE --> END
```

**CRITICAL — Constant Resolution:**
The method does not reference project constant classes or named constant fields. The only branching values are the runtime sort selector integers `1` through `6`, which are read from the screen state and used as inline literals.

## 3. Parameter Analysis

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

`initialize()` does not accept parameters. It depends on instance fields and local UI state instead: `frame` is created and configured here, `textField` is created and later read by the Submit action, and the surrounding instance field `key` determines which sorting branch and which code sample are used. The method also indirectly uses `tname` as shared screen output state when the Submit action assigns the selected algorithm name.

## 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` | `Sortingss` | UI field state | Reads the user-entered integer sequence from the input box |
| - | `String.split` | `java.lang.String` | - | Splits the input string into individual tokens by spaces |
| - | `Integer.parseInt` | `java.lang.Integer` | - | Converts each token into an integer value for sorting |
| - | `quiks.quickSort` | `quiks` | - | Delegates quick sort execution for the selected dataset |
| U | `merg.mergeSort` | `merg` | - | Performs merge sort on the selected dataset |
| - | `heaps.heapSort` | `heaps` | - | Performs heap sort on the selected dataset |
| U | `textArea.setText` | `Sortingss` | UI field state | Updates the result area with the formatted sorted output |
| U | `textArea_1.setText` | `Sortingss` | UI field state | Updates the title area with the selected algorithm name |
| U | `frame.setVisible(false)` | `Sortingss` | UI frame state | Hides the window when Exit is pressed |
| - | `ProcessBuilder.start` | `java.lang.ProcessBuilder` | OS process | Launches Notepad for the selected algorithm text file |

## 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` | `Sortingss.<init>() -> Sortingss.initialize()` | `quiks.quickSort [R] -` |
| 2 | `Frame` | `Frame.initialize() -> new Sortingss() -> Sortingss.initialize()` | `merg.mergeSort [U] -` |
| 3 | `Frame` | `Frame.initialize() -> new Sortingss() -> Sortingss.initialize()` | `heaps.heapSort [ - ] -` |

The method is not directly invoked by external screens; instead, it is reached through object construction. `Frame.initialize()` creates a `Sortingss` instance, and the `Sortingss` constructor immediately calls `initialize()`, which makes the method a screen bootstrap routine rather than a user-triggered endpoint.

## 6. Per-Branch Detail Blocks

**Block 1** — [SET / EXEC] `(screen bootstrap and component creation)` (L204)

> Initializes the sorting window and all visible 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);` |
| 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 | `textArea_1.setBounds(102, 0, 304, 41);` |
| 10 | EXEC | `frame.getContentPane().add(textArea_1);` |
| 11 | SET | `textField = new JTextField();` |
| 12 | EXEC | `textField.setBounds(102, 102, 304, 22);` |
| 13 | EXEC | `frame.getContentPane().add(textField);` |
| 14 | EXEC | `textField.setColumns(10);` |
| 15 | SET | `JLabel lblEnterInput = new JLabel("Enter Input");` |
| 16 | EXEC | `lblEnterInput.setBounds(30, 105, 85, 16);` |
| 17 | EXEC | `frame.getContentPane().add(lblEnterInput);` |
| 18 | SET | `JTextArea textArea = new JTextArea();` |
| 19 | EXEC | `textArea.setLineWrap(true);` |
| 20 | EXEC | `textArea.setEditable(false);` |
| 21 | EXEC | `textArea.setBounds(102, 175, 304, 56);` |
| 22 | EXEC | `frame.getContentPane().add(textArea);` |

**Block 2** — [EXEC] `(Submit button action listener registration)` (L233)

> Registers the primary action that performs parsing, sorting, and output formatting.

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

**Block 3** — [EXEC] `(Submit action execution)` (L235)

> Reads the entered sequence and converts it to integer form for algorithm execution.

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

**Block 4** — [IF] `(key == 1)` (L243)

> Executes bubble sort in place and labels the result accordingly.

| # | Type | Code |
|---|------|------|
| 1 | SET | `int j;` |
| 2 | SET | `boolean flag = true;` |
| 3 | SET | `int temp;` |
| 4 | EXEC | `while (flag) { ... }` |
| 5 | SET | `flag = false;` |
| 6 | EXEC | `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 5** — [IF] `(key == 2)` (L261)

> Executes selection sort in place and labels the selected algorithm.

| # | Type | Code |
|---|------|------|
| 1 | SET | `int n = num.length;` |
| 2 | EXEC | `for (int i = 0; i < n-1; i++) { ... }` |
| 3 | SET | `int min_idx = i;` |
| 4 | EXEC | `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 6** — [IF] `(key == 3)` (L277)

> Delegates the sort to the quick sort helper class.

| # | 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 7** — [IF] `(key == 4)` (L287)

> Executes insertion sort in place and labels the selected algorithm.

| # | Type | Code |
|---|------|------|
| 1 | SET | `int i, ke, j;` |
| 2 | EXEC | `for (i = 1; i < tok.length; i++)` |
| 3 | SET | `ke = num[i];` |
| 4 | SET | `j = i - 1;` |
| 5 | EXEC | `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 8** — [IF] `(key == 5)` (L302)

> Delegates the sort to the merge sort helper class.

| # | 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 9** — [IF] `(key == 6)` (L309)

> Delegates the sort to the heap sort helper class.

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

**Block 10** — [EXEC] `(format and render output)` (L317)

> Converts the sorted integers back into text and refreshes the display areas.

| # | Type | Code |
|---|------|------|
| 1 | SET | `String str1[] = new String[tok.length];` |
| 2 | EXEC | `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 11** — [EXEC] `(Exit button action listener registration)` (L328)

> Registers the UI close action.

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

**Block 12** — [EXEC] `(Exit action execution)` (L330)

> Hides the sorting window.

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

**Block 13** — [EXEC] `(Open Code button action listener registration)` (L337)

> Registers the code-view action for the selected algorithm.

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

**Block 14** — [IF] `(key == 1)` (L340)

> Opens the bubble sort source text file in Notepad.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "bubble.txt");` |
| 2 | EXEC | `pb.start();` |

**Block 15** — [IF] `(key == 2)` (L351)

> Opens the selection sort source text file in Notepad.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "selection.txt");` |
| 2 | EXEC | `pb.start();` |

**Block 16** — [IF] `(key == 3)` (L361)

> Opens the quick sort source text file in Notepad.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "quicksort.txt");` |
| 2 | EXEC | `pb.start();` |

**Block 17** — [IF] `(key == 4)` (L372)

> Opens the insertion sort source text file in Notepad.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "insertion.txt");` |
| 2 | EXEC | `pb.start();` |

**Block 18** — [IF] `(key == 5)` (L383)

> Opens the merge sort source text file in Notepad.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "merge.txt");` |
| 2 | EXEC | `pb.start();` |

**Block 19** — [IF] `(key == 6)` (L393)

> Opens the heap sort source text file in Notepad.

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "maxheap.txt");` |
| 2 | EXEC | `pb.start();` |

**Block 20** — [EXEC] `(method completion)` (L403)

> Finishes window setup after all controls and event handlers are registered.

| # | Type | Code |
|---|------|------|
| 1 | RETURN | `}` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `frame` | Field | Main application window for the sorting screen |
| `textField` | Field | User input box where the integer list is entered |
| `textArea` | Field | Output area that shows the sorted numbers |
| `textArea_1` | Field | Title/status area that shows the selected sort name |
| `key` | Field | Sort selector value that determines which algorithm branch executes |
| `tname` | Field | Display name of the selected sorting algorithm |
| `fin` | Local variable | Formatted output string that begins with the algorithm label and ends with the sorted numbers |
| `str` | Local variable | Raw text entered by the user |
| `tok` | Local variable | Tokenized list of input numbers split by spaces |
| `num` | Local variable | Integer array used as the working sort dataset |
| `flag` | Local variable | Bubble sort control flag indicating whether another pass is required |
| `min_idx` | Local variable | Index of the current minimum element during selection sort |
| `ke` | Local variable | Insertion sort key value being inserted into the sorted prefix |
| `temp` | Local variable | Temporary swap holder used by bubble sort and selection sort |
| `quiks` | Class | Quick sort helper class used to sort the dataset |
| `merg` | Class | Merge sort helper class used to sort the dataset |
| `heaps` | Class | Heap sort helper class used to sort the dataset |
| `Bubble Sort` | Business term | Bubble sort algorithm demonstration mode |
| `Selection Sort` | Business term | Selection sort algorithm demonstration mode |
| `Quick Sort` | Business term | Quick sort algorithm demonstration mode |
| `Insertion Sort` | Business term | Insertion sort algorithm demonstration mode |
| `Merge Sort` | Business term | Merge sort algorithm demonstration mode |
| `Heap Sort` | Business term | Heap sort algorithm demonstration mode |
| `Notepad.exe` | Technical term | Windows text editor launched to show the corresponding algorithm source file |
| `bubble.txt` | File | Bubble sort reference code opened from the UI |
| `selection.txt` | File | Selection sort reference code opened from the UI |
| `quicksort.txt` | File | Quick sort reference code opened from the UI |
| `insertion.txt` | File | Insertion sort reference code opened from the UI |
| `merge.txt` | File | Merge sort reference code opened from the UI |
| `maxheap.txt` | File | Heap sort reference code opened from the UI |
| `Space-separated input` | Business term | Input formatting rule requiring each integer to be separated by a single space |
| `Exit` | UI action | Closes the sorting window by hiding the frame |
| `Open Code` | UI action | Opens the text file for the currently selected sort algorithm |
