# Algo_calc

## Overview

`algo_Calc` appears to be a small Swing-based sorting visualizer and demo application. It provides a menu window for choosing a sorting algorithm, then opens a second window where the user can enter space-separated integers, run the selected sort, and view both the sorted result and a label naming the algorithm.

The module exists to package several classic sorting implementations together with a simple GUI wrapper. One class (`Frame`) acts as the menu screen, while `Sortingss` holds the main input/output UI and the algorithm dispatch logic. Supporting classes in the same file implement quick sort, merge sort, and heap sort.

## Key Classes and Interfaces

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

`Frame` is the entry-point menu window. It is intentionally small: it creates a frame containing six buttons, one for each supported sort, and forwards the user's choice into the main `Sortingss` window.

**Role in the design**

- Serves as the first screen the user sees.
- Encodes the algorithm choice as an integer key and a display label.
- Hides itself after a selection is made, so the app behaves like a two-step flow rather than a single permanent window.

**Key methods**

- [Frame.main(String[] args)](src/algo_Calc/Frame.java:16) launches the UI on the AWT event thread and shows the menu frame.
- [Frame.Frame()](src/algo_Calc/Frame.java:32) constructs the window by delegating to `initialize()`.
- [Frame.initialize()](src/algo_Calc/Frame.java:39) builds the menu and wires each button to a specific algorithm choice.

**Behavior worth noting**

Each button calls `obj.vis(...)` on a shared `Sortingss` instance and then hides the menu frame. The numeric keys map to algorithms as follows:

- `1` - Bubble Sort
- `2` - Selection Sort
- `3` - Quick Sort
- `4` - Insertion Sort
- `5` - Merge Sort
- `6` - Max Heap Sort

This mapping is the bridge between the menu and the sorter window.

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

`Sortingss` is the main application window and the central coordination point for the module. It owns the input field, output area, title banner, and buttons for submitting input, exiting, and opening algorithm-specific code files in Notepad.

**Role in the design**

- Receives the chosen algorithm from `Frame`.
- Parses user input into integers.
- Runs the selected sorting algorithm.
- Renders the sorted result back into the UI.
- Optionally opens a text file associated with the chosen algorithm.

**Key fields**

- `frame` - the Swing window.
- `textField` - user input for space-separated integers.
- `key` - the selected algorithm identifier.
- `tname` - the heading shown at the top of the result window.

**Key methods**

- [Sortingss.main(String[] args)](src/algo_Calc/Sortingss.java:171) launches the sorter window directly.
- [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 selected algorithm, updates the title text, and makes the window visible.
- [Sortingss.initialize()](src/algo_Calc/Sortingss.java:204) builds the full UI and attaches all button handlers.

**Behavior worth noting**

`vis()` does not sort anything itself. It only records the selected mode and shows the window. The actual sort happens later when the user clicks **Submit**.

The class also contains the UI event handler that parses the input string, dispatches to the chosen algorithm, and formats the output. The `Open Code` button launches Notepad with a hard-coded filename such as `bubble.txt`, `selection.txt`, or `quicksort.txt` depending on the chosen algorithm.

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

`quiks` contains the quick sort implementation used by the sorter UI. Despite the name, this class only implements quick sort-related behavior: recursive sorting plus partitioning.

**Role in the design**

- Encapsulates the quick sort algorithm separately from the GUI.
- Provides a straightforward in-place sort over an `int[]` array.

**Key methods**

- [quiks.quickSort(int[] num, int start, int end)](src/algo_Calc/Sortingss.java:19) recursively sorts a subrange of the array.
- [quiks.partition(int[] num, int start, int end)](src/algo_Calc/Sortingss.java:32) partitions the array around the last element as pivot and returns the pivot's final index.

**Algorithm notes**

The implementation uses the last element as pivot. Elements smaller than the pivot are moved to the left side of the active partition, and the recursion continues on the left and right partitions until the range is sorted.

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

`merg` contains the merge sort implementation. Like `quiks`, it is a small algorithm-only helper class with no GUI responsibilities.

**Role in the design**

- Implements merge sort in a reusable form for the UI.
- Keeps the divide-and-conquer logic isolated from the event handler.

**Key methods**

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

**Algorithm notes**

The code copies the left and right halves into temporary arrays, then merges them back into the original array in ascending order. This is the standard stable merge-sort pattern.

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

`heaps` contains the heap sort implementation and the supporting heapify routine.

**Role in the design**

- Builds a max heap from the input array.
- Repeatedly moves the largest element to the end of the array and restores the heap property.

**Key methods**

- [quiks.heapify(int num[], int size, int i)](src/algo_Calc/Sortingss.java:121) enforces the max-heap property for the subtree rooted at `i`.
- [quiks.heapSort(int num[], int size)](src/algo_Calc/Sortingss.java:144) converts the whole array into a heap and sorts it in place.

**Algorithm notes**

The implementation uses array indices to represent the binary heap. The left child is `2*i + 1` and the right child is `2*i + 2`. After heap construction, the largest element is swapped to the end one by one.

## How It Works

### End-to-end flow

1. The user starts the app and sees the [Frame](src/algo_Calc/Frame.java:9) menu.
2. A button click chooses an algorithm and calls [Sortingss.vis(int k, String str)](src/algo_Calc/Sortingss.java:190).
3. `vis()` stores the selected algorithm key and title text, then shows the sorter window.
4. In the sorter window, the user enters integers separated by spaces.
5. Clicking **Submit** parses the input into an `int[]`.
6. The event handler dispatches to the matching algorithm implementation based on `key`.
7. The code converts the sorted numbers back into text and displays them in the output area.
8. Clicking **Open Code** launches Notepad with a file name tied to the chosen algorithm.

### Dispatch logic

The `Submit` handler inside [Sortingss.initialize()](src/algo_Calc/Sortingss.java:204) is the central switch point. It selects behavior based on `key`:

- `1` - inline bubble sort implementation
- `2` - inline selection sort implementation
- `3` - `quiks.quickSort(...)`
- `4` - inline insertion sort implementation
- `5` - `merg.mergeSort(...)`
- `6` - `heaps.heapSort(...)`

This design keeps the UI code in one place, but it also means the event handler contains the sorting logic for three algorithms directly and delegates the other three to helper classes.

### Output formatting

After sorting, the handler builds a string that begins with a label such as `Bubble Sort =` or `Quick Sort =`, then appends the sorted integers separated by spaces. That string is shown in the result text area, and the title banner at the top is updated to the selected algorithm name.

## Mermaid relationship diagram

```mermaid
flowchart LR
  FrameClass["Frame"] --> SortingssClass["Sortingss"]
  SortingssClass --> QuiksClass["quiks"]
  SortingssClass --> MergClass["merg"]
  SortingssClass --> HeapsClass["heaps"]
  FrameClass --> MenuButtons["Sort selection buttons"]
  MenuButtons --> SortingssClass
  SortingssClass --> InputParsing["Parse space separated integers"]
  InputParsing --> Algorithms["Selected sorting algorithm"]
```

## Data Model

The module does not define domain entities, DTOs, or persistent models. Its working data structures are simple in-memory arrays and UI state:

- `int[] num` holds the integers being sorted.
- `String[] tok` stores the raw input tokens after splitting on spaces.
- `String tname` tracks the title shown in the window.
- `int key` tracks which algorithm the user selected.

The module is therefore stateless across runs, aside from the current window state and the last algorithm choice.

## Dependencies and Integration

### Swing and AWT

Both classes depend on standard Java desktop libraries:

- `java.awt.EventQueue` for launching UI work on the event thread.
- `javax.swing.JFrame`, `JButton`, `JLabel`, `JTextField`, and `JTextArea` for the interface.
- `java.awt.Font` and `java.awt.Color` for styling.

There are no external package dependencies and no cross-module dependencies recorded in the index.

### Operating system integration

The `Open Code` button uses `ProcessBuilder` to start `Notepad.exe` with a text file name. That makes the feature Windows-specific and dependent on Notepad being available on the machine.

## Notes for Developers

- Input must be space-separated integers. The parser uses `String.split(" ")`, so non-numeric tokens or irregular spacing can lead to parsing issues.
- The quick sort, merge sort, and heap sort algorithms are implemented in helper classes, but bubble, selection, and insertion sort live inline inside the submit handler. If you extend the module, consider moving all algorithms into dedicated helpers for consistency.
- `vis()` only sets state and shows the sorter frame. Any future code that relies on `key` or `tname` should preserve that two-step flow.
- The `Open Code` feature assumes local text files such as `bubble.txt` and `merge.txt` exist alongside the application or in the working directory.
- The class names `quiks`, `merg`, and `heaps` are unconventional. If you refactor, be careful to preserve any references from the UI event handler.
- `Sortingss.main()` declares `throws Exception`, but the body catches exceptions inside the `Runnable`. That declaration currently appears unnecessary.

## Reference Map

- Menu window: [Frame.initialize()](src/algo_Calc/Frame.java:39)
- Main sorter UI: [Sortingss.initialize()](src/algo_Calc/Sortingss.java:204)
- Algorithm selection handoff: [Sortingss.vis(int k, String str)](src/algo_Calc/Sortingss.java:190)
- Quick sort helper: [quiks.quickSort(int[] num, int start, int end)](src/algo_Calc/Sortingss.java:19)
- Merge sort helper: [quiks.mergeSort(int num[], int l, int r)](src/algo_Calc/Sortingss.java:105)
- Heap sort helper: [quiks.heapSort(int num[], int size)](src/algo_Calc/Sortingss.java:144)
