# Algo_calc

## Overview

`algo_Calc` appears to be a small Swing-based sorting visualizer. It provides a menu window for choosing an algorithm, then opens a second window where the user can enter a space-separated list of integers, run the selected sort, and view the sorted output. The module also includes “Open Code” actions that launch a text file associated with each algorithm, suggesting the app is meant both as a demo and as a teaching aid.

The module is self-contained: the UI, algorithm implementations, and selection logic all live in the same package. That makes it easy to understand end-to-end, but it also means the GUI class currently owns a lot of behavior that might eventually be split out if the module grows.

## Key Classes and Interfaces

### `Frame`
**Source:** [src/algo_Calc/Frame.java](src/algo_Calc/Frame.java)

`Frame` is the entry screen for the application. It creates a simple menu with one button per supported sorting algorithm:

- Bubble Sort
- Selection Sort
- Quick Sort
- Insertion Sort
- Merge Sort
- Max Heap Sort

Each button delegates to `Sortingss.vis(int k, String str)` with an algorithm code and a display title, then hides the menu window. The class does not perform any sorting itself; its job is to choose the algorithm and hand control to the interactive sorter window.

**Key methods**

- `main(String[] args)` — Starts the Swing UI on the event dispatch thread and shows the menu window.
- `Frame()` — Constructor that immediately calls `initialize()`.
- `initialize()` — Builds the menu frame, creates the buttons, and wires each button to the appropriate algorithm selection.

**Role in the module**

`Frame` is essentially the navigation layer. It is the first point of contact for the user and the only place where the algorithm choice is made before the sorting screen opens.

### `quiks`
**Source:** [src/algo_Calc/Sortingss.java](src/algo_Calc/Sortingss.java)

`quiks` is a helper class that implements quick sort. Despite the name, it is not a general utility class; it only contains the quick sort algorithm and its partitioning routine.

**Key methods**

- `quickSort(int[] num, int start, int end)` — Recursively sorts the subarray from `start` to `end` using quick sort.
- `partition(int[] num, int start, int end)` — Reorders the subarray around the pivot at `end` and returns the pivot’s final index.

**Design role**

This class isolates the quick sort implementation from the GUI event handler, but it is still tightly coupled to the current module because it is package-private and used directly by `Sortingss`.

### `merg`
**Source:** [src/algo_Calc/Sortingss.java](src/algo_Calc/Sortingss.java)

`merg` implements merge sort using the standard divide-and-conquer pattern. It contains the merge step and the recursive sort step.

**Key methods**

- `merge(int[] num, int l, int m, int r)` — Merges two sorted halves back into the original array.
- `mergeSort(int[] num, int l, int r)` — Recursively divides the input until single-element ranges are reached, then merges them.

**Design role**

The class is a straightforward implementation of merge sort and serves as the algorithm backend for the merge-sort button in the UI.

### `heaps`
**Source:** [src/algo_Calc/Sortingss.java](src/algo_Calc/Sortingss.java)

`heaps` implements max-heap sort. It builds a max heap and repeatedly moves the largest element to the end of the array.

**Key methods**

- `heapify(int[] num, int size, int i)` — Restores the max-heap property for a subtree rooted at `i`.
- `heapSort(int[] num, int size)` — Builds the heap and extracts elements into sorted order.

**Design role**

Like `quiks` and `merg`, this class keeps the algorithm implementation separate from the event-handler logic, but only within the package boundary.

### `Sortingss`
**Source:** [src/algo_Calc/Sortingss.java](src/algo_Calc/Sortingss.java)

`Sortingss` is the main working window of the application. It is responsible for:

- presenting the text input field,
- parsing the user’s space-separated integers,
- dispatching to the selected sorting algorithm,
- showing the sorted result,
- and opening the corresponding algorithm text file.

The class stores two pieces of state that drive the UI:

- `key` — an integer identifying the selected algorithm.
- `tname` — the title displayed in the header area.

`Sortingss` is the module’s integration point. It ties the algorithm helpers and the GUI together and contains the user-facing workflow.

**Key methods**

- `main(String[] args)` — Starts the sorting window on the Swing event dispatch thread.
- `Sortingss()` — Constructor that calls `initialize()`.
- `vis(int k, String str)` — Sets the active algorithm key and title, then makes the window visible.
- `initialize()` — Builds the sorting window, wires up the Submit/Exit/Open Code buttons, and contains the core sorting dispatch logic.

## How It Works

### Overall flow

```mermaid
flowchart TD
FrameMain["Frame main window"] --> SortingssVis["Sortingss.vis()"]
FrameButtons["Algorithm buttons"] --> SortingssVis
SortingssVis --> SortingssFrame["Sorting window"]
SortingssFrame --> ParseInput["Parse space-separated integers"]
ParseInput --> Bubble["Bubble sort"]
ParseInput --> Selection["Selection sort"]
ParseInput --> Quick["Quick sort"]
ParseInput --> Insertion["Insertion sort"]
ParseInput --> Merge["Merge sort"]
ParseInput --> Heap["Heap sort"]
Bubble --> Output["Rendered sorted output"]
Selection --> Output
Quick --> Output
Insertion --> Output
Merge --> Output
Heap --> Output
```

### Typical user interaction

1. The user launches `Frame`, which shows the algorithm menu.
2. The user picks an algorithm.
3. The selected button calls `Sortingss.vis(...)` with an algorithm key and label.
4. `Sortingss` becomes visible and shows the input form.
5. The user enters integers separated by spaces and clicks Submit.
6. The Submit handler:
   - reads the input text,
   - splits it on spaces,
   - converts each token to an `int`,
   - runs the algorithm matching `key`,
   - converts the sorted numbers back to text,
   - updates the output area and header label.
7. The user can optionally open the associated algorithm text file with the Open Code button.

### Algorithm dispatch

The `Submit` button contains the central dispatch logic. The selected algorithm is determined by `key`:

- `1` — bubble sort, implemented inline in the event handler
- `2` — selection sort, implemented inline in the event handler
- `3` — quick sort, delegated to `quiks.quickSort(...)`
- `4` — insertion sort, implemented inline in the event handler
- `5` — merge sort, delegated to `merg.mergeSort(...)`
- `6` — heap sort, delegated to `heaps.heapSort(...)`

The result is written to the output text area and the algorithm title is shown in the header area.

### Open Code behavior

The Open Code button launches a Windows Notepad process for a text file associated with the selected algorithm. The filenames are hard-coded:

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

This appears to be intended as a companion explanation or source reference for each algorithm.

## Data Model

There is no persistent domain model in this module. The module operates on transient arrays of integers created from the user’s input text.

### Runtime data flow

- **Input text** → split by spaces into `String[] tok`
- **Parsed numbers** → converted into `int[] num`
- **Sorted output** → converted back into strings and concatenated for display

### UI state

`Sortingss` keeps a small amount of state in fields:

- `key` — the currently selected sort algorithm
- `tname` — the current header title
- `textField` — the input control used by the user
- `frame` — the active Swing window

## Dependencies and Integration

### Internal dependencies

- `Frame` depends on `Sortingss` to show the algorithm input window.
- `Sortingss` depends on the helper classes `quiks`, `merg`, and `heaps` for three of the six algorithms.

### External libraries

The module uses only standard Java/Swing classes:

- `java.awt.EventQueue`
- `javax.swing.JFrame`
- `javax.swing.JButton`
- `javax.swing.JTextField`
- `javax.swing.JTextArea`
- `javax.swing.JLabel`
- `java.awt.event.ActionListener`
- `java.awt.event.ActionEvent`
- `java.awt.Font`
- `java.awt.Color`

No third-party dependencies are visible in the source.

## Notes for Developers

- **Input format is strict.** The parser expects integers separated by single spaces. Extra whitespace or non-numeric tokens will cause parsing issues when `Integer.parseInt(...)` runs.
- **The sorting logic is split between inline code and helper classes.** Bubble, selection, and insertion sort are implemented directly in the Submit handler, while quick, merge, and heap sort are delegated to separate helper classes.
- **The `vis()` method is the main integration point.** If you add a new sorting algorithm, you will need to:
  1. add a new menu button in `Frame`,
  2. assign it a new key,
  3. add a matching branch in `Sortingss`’s Submit handler,
  4. optionally add a new Open Code filename.
- **Open Code is Windows-specific.** The use of `ProcessBuilder("Notepad.exe", ...)` means this path only works in environments where Notepad is available.
- **The UI layout is absolute-position based.** Both frames use `null` layouts and explicit bounds. That keeps the code simple but makes resizing and cross-platform presentation less flexible.
- **Package-private algorithm classes are intentionally lightweight.** `quiks`, `merg`, and `heaps` are not public APIs; they are internal helpers for the module’s UI flow.

## Class Relationship Summary

- `Frame` selects an algorithm.
- `Sortingss` receives the selection and handles user input and output.
- `quiks`, `merg`, and `heaps` provide reusable algorithm implementations for the corresponding branches.

## Reference

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