---

# (DD01) 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-based sorting screen used to demonstrate and execute multiple integer sorting algorithms from a single UI. It constructs the frame, input area, output area, and action buttons, then wires each button to its own business action. The `Submit` action reads a space-separated integer list from the input field, converts it into an integer array, and dispatches the array to one of six sorting branches based on the current `key` value. The supported service categories are Bubble Sort, Selection Sort, Quick Sort, Insertion Sort, Merge Sort, and Heap Sort, each producing a formatted result string and a header label that identifies the selected algorithm. The `Open Code` action is a companion navigation function that launches Notepad with the corresponding algorithm reference file for the currently selected sort. The `Exit` action simply hides the window. Overall, this method acts as the UI composition and dispatch entry point for the algorithm demo screen, coordinating screen rendering, user input parsing, algorithm selection, and output presentation.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START["initialize()"]
    CREATE_FRAME["Create JFrame and configure layout"]
    CREATE_HEADER["Create header text area"]
    CREATE_INPUT["Create input text field and label"]
    CREATE_OUTPUT["Create output text area and label"]
    CREATE_SUBMIT["Create Submit button"]
    SUBMIT_CLICK["Submit actionPerformed"]
    READ_INPUT["Read textField content"]
    SPLIT_INPUT["Split input by spaces"]
    PARSE_LOOP["For each token parse integer into num[]"]
    KEY_DECISION{"key value"}
    B1["key == 1 - Bubble Sort"]
    B2["key == 2 - Selection Sort"]
    B3["key == 3 - Quick Sort"]
    B4["key == 4 - Insertion Sort"]
    B5["key == 5 - Merge Sort"]
    B6["key == 6 - Heap Sort"]
    B1_SORT["Bubble sort loop and swap pass"]
    B2_SORT["Selection sort scan and swap"]
    B3_CALL["new quiks().quickSort(num, 0, size - 1)"]
    B4_SORT["Insertion sort shift loop"]
    B5_CALL["new merg().mergeSort(num, 0, size)"]
    B6_CALL["new heaps().heapSort(num, size)"]
    BUILD_OUTPUT["Convert sorted numbers to string"]
    UPDATE_UI["Set output text and title text"]
    CREATE_EXIT["Create Exit button"]
    EXIT_CLICK["Exit actionPerformed"]
    HIDE_FRAME["frame.setVisible(false)"]
    CREATE_OPEN["Create Open Code button"]
    OPEN_CLICK["Open Code actionPerformed"]
    OPEN_KEY{"key value"}
    O1["key == 1 - open bubble.txt"]
    O2["key == 2 - open selection.txt"]
    O3["key == 3 - open quicksort.txt"]
    O4["key == 4 - open insertion.txt"]
    O5["key == 5 - open merge.txt"]
    O6["key == 6 - open maxheap.txt"]
    END_NODE["Return / Next"]
    START --> CREATE_FRAME --> CREATE_HEADER --> CREATE_INPUT --> CREATE_OUTPUT --> CREATE_SUBMIT --> SUBMIT_CLICK --> READ_INPUT --> SPLIT_INPUT --> PARSE_LOOP --> KEY_DECISION
    KEY_DECISION --> B1 --> B1_SORT --> BUILD_OUTPUT
    KEY_DECISION --> B2 --> B2_SORT --> BUILD_OUTPUT
    KEY_DECISION --> B3 --> B3_CALL --> BUILD_OUTPUT
    KEY_DECISION --> B4 --> B4_SORT --> BUILD_OUTPUT
    KEY_DECISION --> B5 --> B5_CALL --> BUILD_OUTPUT
    KEY_DECISION --> B6 --> B6_CALL --> BUILD_OUTPUT
    BUILD_OUTPUT --> UPDATE_UI --> CREATE_EXIT --> EXIT_CLICK --> HIDE_FRAME
    UPDATE_UI --> CREATE_OPEN --> OPEN_CLICK --> OPEN_KEY
    OPEN_KEY --> O1 --> END_NODE
    OPEN_KEY --> O2 --> END_NODE
    OPEN_KEY --> O3 --> END_NODE
    OPEN_KEY --> O4 --> END_NODE
    OPEN_KEY --> O5 --> END_NODE
    OPEN_KEY --> O6 --> END_NODE
```

**Constant resolution note:** No `*Const*` or `*CC*.java` files exist in this repository scan, so the method relies on hardcoded literals for branch selection and UI labels. The resolved business values are: `key == 1` Bubble Sort, `key == 2` Selection Sort, `key == 3` Quick Sort, `key == 4` Insertion Sort, `key == 5` Merge Sort, and `key == 6` Heap Sort. The `Open Code` branch opens `bubble.txt`, `selection.txt`, `quicksort.txt`, `insertion.txt`, `merge.txt`, and `maxheap.txt` respectively.

## 3. Parameter Analysis

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

**Instance fields / external state read by this method:** `frame`, `textField`, `key`, and `tname` are read or updated during the UI lifecycle. The method also relies on runtime user input from the text field and external Windows availability of `Notepad.exe` when opening reference files.

## 4. CRUD Operations / Called Services

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

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| - | `heaps.heapSort` | heaps | - | Calls `heapSort` in `heaps` |
| U | `merg.mergeSort` | merg | - | Calls `mergeSort` in `merg` |
| - | `quiks.quickSort` | quiks | - | Calls `quickSort` in `quiks` |

This method does not invoke database persistence, so CRUD is interpreted as algorithm/service dispatch and UI state handling rather than data-store mutation. The following called services are observed inside the method body:

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| R | `textField.getText` | N/A | UI State | Reads the current input string entered by the user |
| U | `Integer.parseInt` loop into `num[]` | N/A | In-memory array | Converts the submitted text tokens into numeric values |
| U | Bubble sort branch | N/A | In-memory array | Sorts the array in ascending order by repeated swaps |
| U | Selection sort branch | N/A | In-memory array | Sorts the array in ascending order by selecting the minimum element |
| R | `quiks.quickSort` | quiks | In-memory array | Recursively sorts the array using quick sort |
| U | Insertion sort branch | N/A | In-memory array | Sorts the array in ascending order by insertion logic |
| U | `merg.mergeSort` | merg | In-memory array | Recursively sorts the array using merge sort |
| U | `heaps.heapSort` | heaps | In-memory array | Sorts the array using heap sort |
| U | Output formatting loop | N/A | UI State | Converts sorted numbers to a printable output string |
| U | `textArea.setText` / `textArea_1.setText` | N/A | UI State | Updates the output panel and title banner |
| D | `frame.setVisible(false)` | N/A | UI State | Hides the window when Exit is selected |
| R | `ProcessBuilder(...).start()` | N/A | External file | Opens the selected algorithm reference text file in Notepad |

## 5. Dependency Trace

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Screen: `Frame` | `algo_Calc.Frame.<init>()` -> `Frame.initialize()` -> `new Sortingss()` -> `Sortingss.initialize()` | `heaps.heapSort [U] in-memory array` |
| 2 | Screen: `Sortingss` | `algo_Calc.Sortingss.<init>()` -> `Sortingss.initialize()` | `quiks.quickSort [R] in-memory array` |

The first caller is the application entry screen that instantiates the sorting window. The second caller is the sorting class constructor itself, which initializes its own frame when the object is created. Inside this method, the terminal algorithm endpoints reached from the `Submit` action are the bubble sort, selection sort, quick sort, insertion sort, merge sort, and heap sort branches; the externally callable terminal observed in the source is `quiks.quickSort`, `merg.mergeSort`, and `heaps.heapSort`.

## 6. Per-Branch Detail Blocks

**Block 1** — IF / initialization sequence `(method entry)` (L204)

> Builds the sorting screen container and prepares the UI shell.

| # | 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** — SEQUENCE `(header and input/output control creation)` (L211-L232)

> Creates the title banner, input field, input label, output area, and output label.

| # | 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 | `frame.getContentPane().add(textArea_1);` |
| 6 | SET | `textField = new JTextField();` |
| 7 | EXEC | `textField.setColumns(10);` |
| 8 | SET | `JLabel lblEnterInput = new JLabel("Enter Input");` |
| 9 | EXEC | `frame.getContentPane().add(lblEnterInput);` |
| 10 | SET | `JTextArea textArea = new JTextArea();` |
| 11 | EXEC | `textArea.setLineWrap(true);` |
| 12 | EXEC | `textArea.setEditable(false);` |
| 13 | EXEC | `frame.getContentPane().add(textArea);` |

**Block 3** — EVENT HANDLER `(Submit button actionPerformed)` (L234-L346)

> Parses the entered numbers, selects the sorting algorithm by key, and writes the sorted result back to the UI.

| # | Type | Code |
|---|------|------|
| 1 | SET | `JButton btnSubmit = new JButton("Submit");` |
| 2 | CALL | `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 3.1** — IF `(key == 1)` `[key="1"]` (L248)

> Executes bubble sort and labels the output accordingly.

| # | 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 3.2** — IF `(key == 2)` `[key="2"]` (L270)

> Executes selection sort and tags the result as Selection Sort.

| # | 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 3.3** — IF `(key == 3)` `[key="3"]` (L291)

> Invokes the quick sort helper and sets the corresponding title.

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

> Executes insertion sort and labels the output accordingly.

| # | 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 3.5** — IF `(key == 5)` `[key="5"]` (L321)

> Invokes merge sort and sets the corresponding title.

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

> Invokes heap sort and sets the corresponding title.

| # | 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 3.7** — FOR `(format sorted output)` (L338)

> Converts the sorted array back into a displayable string.

| # | 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 4** — EVENT HANDLER `(Exit button actionPerformed)` (L359-L364)

> Closes the current screen by hiding the frame.

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

**Block 5** — EVENT HANDLER `(Open Code button actionPerformed)` (L368-L426)

> Opens the algorithm reference file that corresponds to the current sort selection.

| # | Type | Code |
|---|------|------|
| 1 | SET | `JButton btnOpenCode = new JButton("Open Code");` |
| 2 | CALL | `btnOpenCode.addActionListener(new ActionListener() { ... });` |
| 3 | IF | `if (key == 1)` `[key="1"]` |
| 4 | SET | `ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "bubble.txt");` |
| 5 | CALL | `pb.start();` |
| 6 | IF | `if (key == 2)` `[key="2"]` |
| 7 | SET | `ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "selection.txt");` |
| 8 | CALL | `pb.start();` |
| 9 | IF | `if (key == 3)` `[key="3"]` |
| 10 | SET | `ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "quicksort.txt");` |
| 11 | CALL | `pb.start();` |
| 12 | IF | `if (key == 4)` `[key="4"]` |
| 13 | SET | `ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "insertion.txt");` |
| 14 | CALL | `pb.start();` |
| 15 | IF | `if (key == 5)` `[key="5"]` |
| 16 | SET | `ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "merge.txt");` |
| 17 | CALL | `pb.start();` |
| 18 | IF | `if (key == 6)` `[key="6"]` |
| 19 | SET | `ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "maxheap.txt");` |
| 20 | CALL | `pb.start();` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `initialize` | Method | Builds and wires the sorting screen UI and event handlers |
| `frame` | Field | Main application window for the sorting demo screen |
| `textField` | Field | Input box where the user enters numbers separated by spaces |
| `textArea` | Field / Local UI | Output area that displays the sorted result string |
| `textArea_1` | Field / Local UI | Header area that displays the selected algorithm name |
| `key` | Field | Sort selection code that determines which algorithm branch runs |
| `tname` | Field | Title name shown in the screen header for the chosen algorithm |
| `tok` | Local variable | Tokenized user input values split by spaces |
| `num` | Local variable | Integer array built from the user-entered numbers |
| `fin` | Local variable | Final output string rendered in the result area |
| Bubble Sort | Business term | Comparison-based ascending sort that repeatedly swaps adjacent values |
| Selection Sort | Business term | Ascending sort that repeatedly selects the minimum remaining value |
| Quick Sort | Business term | Divide-and-conquer sort that partitions values around a pivot |
| Insertion Sort | Business term | Ascending sort that inserts each item into its correct position |
| Merge Sort | Business term | Divide-and-conquer sort that merges sorted halves |
| Heap Sort | Business term | Sort that uses a heap structure to repeatedly extract the maximum value |
| `quiks` | Class | Helper class that implements quick sort |
| `merg` | Class | Helper class that implements merge sort |
| `heaps` | Class | Helper class that implements heap sort |
| `ProcessBuilder` | Java API | Launches an external process to open the reference text file |
| `Notepad.exe` | External application | Windows text editor used to display algorithm reference files |
| `bubble.txt` | File | Reference text for Bubble Sort |
| `selection.txt` | File | Reference text for Selection Sort |
| `quicksort.txt` | File | Reference text for Quick Sort |
| `insertion.txt` | File | Reference text for Insertion Sort |
| `merge.txt` | File | Reference text for Merge Sort |
| `maxheap.txt` | File | Reference text for Heap Sort |
| `JFrame` | UI term | Swing top-level window container |
| `JTextArea` | UI term | Multi-line text display field |
| `JTextField` | UI term | Single-line user input field |
| `ActionListener` | Technical term | Callback interface that handles button click events |
| `JOptionPane` | Technical term | Swing dialog utility; not used in this method body but part of the same UI codebase context |
| `sort` | Business term | Operation of ordering numbers from smallest to largest |
| `screen` | Business term | Interactive UI used by end users to choose and run a sort algorithm |
