# Algo_calc

## Overview

`algo_Calc` appears to be a small Swing-based sorting demonstrator. It provides a menu window for choosing one of several sorting algorithms, then opens a second window where the user can enter a space-separated list of integers, run the selected sort, and view the sorted result. The module also exposes a couple of entry points and helper classes that contain the actual sorting implementations.

This module exists to let a user compare common sorting algorithms in a simple desktop UI. The UI logic and the algorithm logic are tightly coupled: the `Frame` class acts as a launcher, while `Sortingss` owns the interactive sorting screen and delegates to helper classes for quick sort, merge sort, and heap sort.

## Key Classes and Interfaces

### [Frame](src/algo_Calc/Frame.java:9)

`Frame` is the first window a user sees. Its role is to present the algorithm menu and forward the user's choice into the main sorting screen.

#### Responsibilities

- Create the initial Swing frame.
- Show six algorithm choices as buttons.
- Pass the chosen algorithm identifier into `Sortingss.vis(...)`.
- Hide the menu window after a selection is made.

#### Important methods

- [Frame.main(String[] args)](src/algo_Calc/Frame.java:16) launches the UI on the AWT event dispatch thread. It constructs a `Frame` instance and makes its internal `JFrame` visible.
- [Frame.Frame()](src/algo_Calc/Frame.java:32) is the constructor and simply delegates to `initialize()`.
- [Frame.initialize()](src/algo_Calc/Frame.java:39) builds the menu window and wires each button to a specific algorithm choice.

#### Design role

`Frame` is effectively a selector screen. It does not sort anything itself; instead, it encodes a small integer key and a title string for the target algorithm and hands those values off to the main application window.

#### Menu mapping

- Bubble Sort → `obj.vis(1, "BUBBLE SORT")`
- Selection Sort → `obj.vis(2, "SELECTION SORT")`
- Quick Sort → `obj.vis(3, "QUICK SORT")`
- Insertion Sort → `obj.vis(4, "INSERTION SORT")`
- Merge Sort → `obj.vis(5, "MERGE SORT")`
- Max Heap Sort → `obj.vis(6, "HEAP SORT")`

### [Sortingss](src/algo_Calc/Sortingss.java:162)

`Sortingss` is the main application window and the module's central coordinator. It owns the text input field, the result display, the algorithm title banner, and the control buttons that run the sort or open example code files.

#### Responsibilities

- Create the main sorting UI.
- Store the selected algorithm in `key` and `tname`.
- Parse user input into integers.
- Execute one of six sorting flows based on the selected key.
- Render the sorted output back into the UI.
- Open algorithm-specific text files in Notepad through `ProcessBuilder`.

#### Important methods

- [Sortingss.main(String[] args)](src/algo_Calc/Sortingss.java:171) starts the application window on the event dispatch thread.
- [Sortingss.Sortingss()](src/algo_Calc/Sortingss.java:187) constructs the window by calling `initialize()`.
- [Sortingss.vis(int k, String str)](src/algo_Calc/Sortingss.java:190) stores the chosen algorithm key and title, then shows the main frame.
- [Sortingss.initialize()](src/algo_Calc/Sortingss.java:204) creates the full UI, including the input field, labels, submit button, exit button, and open-code button.

#### Design role

`Sortingss` acts as both controller and view. It owns the UI state and also dispatches to the actual sorting routines. The class is intentionally stateful: the selected algorithm is remembered in the `key` field so that the submit button can decide which algorithm to apply later.

### [quiks](src/algo_Calc/Sortingss.java:17)

`quiks` contains quick-sort logic, but the class name is misleading because it also serves as the home for the quick sort partition helper.

#### Responsibilities

- Sort an integer array using quick sort.
- Partition the array around a pivot element.

#### Important methods

- [quiks.quickSort(int[] num, int start, int end)](src/algo_Calc/Sortingss.java:19) recursively sorts the subarray between `start` and `end`.
- [quiks.partition(int[] num, int start, int end)](src/algo_Calc/Sortingss.java:32) partitions the array using the last element as pivot and returns the final pivot index.

#### Design role

This helper class isolates the recursive sorting logic from the UI. `Sortingss` creates `quiks` only when the user selects quick sort, then calls `quickSort(...)` on the parsed integer array.

### [merg](src/algo_Calc/Sortingss.java:55)

`merg` contains a merge-sort implementation.

#### Responsibilities

- Merge two sorted halves of an array.
- Recursively divide and sort an array segment.

#### Important methods

- [quiks.merge(int num[], int l, int m, int r)](src/algo_Calc/Sortingss.java:58) combines two sorted halves into one sorted range.
- [quiks.mergeSort(int num[], int l, int r)](src/algo_Calc/Sortingss.java:105) recursively splits the array and calls `merge(...)`.

#### Design role

This class separates the divide-and-conquer merge sort implementation from the UI layer. It uses temporary arrays for the left and right halves, then copies the merged result back into the original array.

### [heaps](src/algo_Calc/Sortingss.java:120)

`heaps` contains a max-heap based heap sort implementation.

#### Responsibilities

- Restore the heap property for a subtree.
- Build a max heap and repeatedly extract the maximum element.

#### Important methods

- [quiks.heapify(int num[], int size, int i)](src/algo_Calc/Sortingss.java:121) ensures the subtree rooted at `i` satisfies the max-heap property.
- [quiks.heapSort(int num[], int size)](src/algo_Calc/Sortingss.java:144) builds the heap and sorts the array in place.

#### Design role

Like the other helper classes, `heaps` keeps the algorithm implementation separate from the UI. The sort is done in place and uses the standard heap-sort pattern of heap construction followed by repeated swap-and-heapify steps.

## How It Works

### Typical flow through the application

1. The program starts in [Frame.main(String[] args)](src/algo_Calc/Frame.java:16).
2. The menu window appears with six algorithm buttons.
3. The user clicks one algorithm, which calls `Sortingss.vis(key, title)`.
4. `Sortingss.vis(...)` stores the algorithm key, updates the title string, and shows the main sorting window.
5. The user enters integers separated by spaces and clicks Submit.
6. The submit handler parses the input string into an `int[]`.
7. The handler chooses a sorting branch based on `key`:
   - `1` → bubble sort inline in the UI handler
   - `2` → selection sort inline in the UI handler
   - `3` → quick sort via `quiks.quickSort(...)`
   - `4` → insertion sort inline in the UI handler
   - `5` → merge sort via `merg.mergeSort(...)`
   - `6` → heap sort via `heaps.heapSort(...)`
8. The resulting array is converted back to strings and displayed in the output area.
9. The title banner is updated with the selected algorithm name.

### Sorting implementation notes

- **Bubble sort** is implemented directly in the submit button handler. It uses a `flag` loop and swaps adjacent values until no swaps occur.
- **Selection sort** is also inline. For each position, it finds the smallest remaining value and swaps it into place.
- **Insertion sort** is inline and uses the standard shift-right approach to insert the current key into the sorted prefix.
- **Quick sort** uses a pivot-at-end partition strategy. The partition method swaps smaller values toward the front and then places the pivot into its final position.
- **Merge sort** recursively splits the array into halves, sorts both halves, then merges them using temporary left and right buffers.
- **Heap sort** first heapifies the array into a max heap, then swaps the root with the end element and heapifies the reduced heap.

### Meridiated relationship diagram

```mermaid
flowchart TD
  FrameClass["Frame"] --> SortingssClass["Sortingss"]
  SortingssClass --> QuiksClass["quiks"]
  SortingssClass --> MergClass["merg"]
  SortingssClass --> HeapsClass["heaps"]
  FrameClass --> MenuButtons["Algorithm selection buttons"]
  MenuButtons --> SortingssClass
```

## Data Model

There are no dedicated entity or DTO classes in this module. The main data model is a user-entered list of integers represented as an `int[]`.

### Key data handled by the module

- **`key`** in `Sortingss` stores the selected algorithm identifier.
- **`tname`** in `Sortingss` stores the label shown in the window header.
- **`textField`** captures the raw input string from the user.
- **`num` arrays** hold the parsed integers and are mutated in place by the selected algorithm.

### Input format

The UI expects integers separated by spaces. The submit handler splits the string on a single space and parses each token with `Integer.parseInt(...)`.

## Dependencies and Integration

### Swing UI dependencies

The module is built on standard Java Swing and AWT classes:

- `JFrame`
- `JButton`
- `JLabel`
- `JTextField`
- `JTextArea`
- `EventQueue`
- `ActionListener`
- `ActionEvent`
- `Font`
- `Color`

### External process integration

The **Open Code** button uses `ProcessBuilder` to launch Notepad with an algorithm-specific text file. The file name depends on the selected algorithm key:

- `bubble.txt`
- `selection.txt`
- `quicksort.txt`
- `insertion.txt`
- `merge.txt`
- `maxheap.txt`

This suggests the module is meant not only to demonstrate sorting behavior, but also to expose corresponding code samples or explanations stored in text files outside the Java sources.

### Module relationships

No package dependencies or cross-module links were resolved in the index, so `algo_Calc` appears to be self-contained aside from its reliance on the Java standard library and the external `.txt` files opened through Notepad.

## Notes for Developers

### Things to watch out for

- **The module uses hard-coded keys** to select algorithms. If you add a new algorithm, you must update both `Frame.initialize()` and the submit logic in `Sortingss.initialize()`.
- **Input parsing is strict.** The current implementation assumes every token is a valid integer and does not handle empty input or malformed values gracefully.
- **Several algorithms are implemented inline in the UI handler.** Bubble, selection, and insertion sort logic are not factored into helper classes, so the submit handler is doing both UI work and algorithm work.
- **The UI state is shared through instance fields.** `key` and `tname` are updated by `vis(...)` and then consumed later by the submit and open-code handlers.
- **The file-opening behavior is Windows-specific.** `ProcessBuilder("Notepad.exe", ...)` will only work where Notepad is available.

### Extension points

- Add a new sorting algorithm by:
  1. creating a new helper class or inline branch,
  2. adding a button in [Frame.initialize()](src/algo_Calc/Frame.java:39), and
  3. adding another `key` branch in [Sortingss.initialize()](src/algo_Calc/Sortingss.java:204).
- If you want to support non-space-separated input, the parsing logic in the Submit handler is the place to change.
- If you want better separation of concerns, the inline bubble/selection/insertion implementations could be moved into helper classes alongside `quiks`, `merg`, and `heaps`.

### Code quality observations

- The helper class names are abbreviated and do not reflect their responsibilities very clearly.
- The `vis(...)` method assigns `tname` several times before setting the final value, which looks like leftover experimentation rather than required logic.
- `vis(...)` returns `int` but always returns `0`; it appears to be used for its side effects rather than its return value.
- The UI is created with absolute positioning (`setLayout(null)` and explicit bounds), which makes it simple but rigid.

## Related Source Files

- [src/algo_Calc/Frame.java](src/algo_Calc/Frame.java)
- [src/algo_Calc/Sortingss.java](src/algo_Calc/Sortingss.java)
