# Algo_calc

## Overview

`algo_Calc` appears to be a small Swing-based sorting visualizer. The package exposes a menu window where a user chooses a sorting algorithm, then a second window where they enter a space-separated list of integers and see the sorted result. It also includes an "Open Code" action that tries to open a text file associated with the selected algorithm.

The module exists to bundle the UI shell and the algorithm implementations together in one package. The code is written as a self-contained desktop app rather than as a reusable library, so most of the logic lives in UI event handlers and helper classes in the same source file.

## Key Classes and Interfaces

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

`Frame` is the entry window for the application. It creates a small launcher with six buttons, one for each supported sorting mode:

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

Each button calls `Sortingss.vis(...)` with a numeric key and a display title, then hides the menu window. In other words, `Frame` does not perform any sorting itself; it acts as a navigation layer that chooses which algorithm the user wants to run.

Key methods:

- `main(String[] args)` — starts the Swing UI on the event dispatch thread and shows the menu frame.
- `Frame()` — constructor that immediately delegates to `initialize()`.
- `initialize()` — builds the `JFrame`, creates the buttons, and wires each button to the `Sortingss` window.

Design notes:

- The frame is positioned manually with absolute bounds rather than a layout manager.
- A single `Sortingss` instance is created up front and reused for all algorithm selections.
- The numeric key passed to `vis(...)` determines which algorithm runs later in `Sortingss`.

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

`Sortingss` is the main working window and the orchestrator for the sorting demo. It owns the input field, output area, title label, submit button, exit button, and the "Open Code" button. It also contains the runtime state that remembers which algorithm the user selected.

The class stores two key pieces of state:

- `key` — identifies the selected algorithm
- `tname` — the title text shown at the top of the window

When the user submits input, `Sortingss` parses the text field into an integer array, dispatches to the chosen algorithm, and renders the sorted result.

Key methods:

- `main(String[] args)` — starts the application and shows the sorting window.
- `Sortingss()` — constructor that builds the UI by calling `initialize()`.
- `vis(int k, String str)` — sets the current algorithm key and title, then makes the frame visible.
- `initialize()` — builds the UI, installs event handlers, and defines the submit/open-code behavior.

Important behavior:

- `vis(...)` is the bridge between the menu window and the sorter window.
- The submit handler contains the bubble sort and insertion sort implementations inline, while quick sort, merge sort, and heap sort are delegated to helper classes.
- The output text area shows both the algorithm name and the sorted list.
- The "Open Code" button launches Notepad with a `.txt` file named for the selected algorithm.

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

`quiks` is a helper class that implements quick sort. Despite the name, it only handles quick sort and partitioning.

Key methods:

- `quickSort(int[] num, int start, int end)` — recursively sorts a subrange of the array using partitioning.
- `partition(int[] num, int start, int end)` — chooses the last element as pivot, partitions the range, and returns the pivot position.

Design notes:

- The quick sort implementation sorts in place.
- The partition logic swaps values directly inside the provided array.
- The class has no UI responsibilities; it is only called from `Sortingss` when the user selects Quick Sort.

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

`merg` is the merge sort helper class. It provides a standard recursive merge sort implementation with a merge step that combines two sorted halves.

Key methods:

- `merge(int num[], int l, int m, int r)` — merges two sorted partitions back into the original array.
- `mergeSort(int num[], int l, int r)` — recursively divides the array and calls `merge(...)`.

Design notes:

- The code allocates temporary left and right arrays for each merge.
- The algorithm is in-place with respect to the caller’s array, but it uses extra temporary storage during merging.
- `Sortingss` invokes this class when the user selects Merge Sort.

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

`heaps` is the helper class for heap sort. It builds a max heap and then repeatedly extracts the largest element to sort the array in ascending order.

Key methods:

- `heapify(int num[], int size, int i)` — enforces the max-heap property on a subtree rooted at index `i`.
- `heapSort(int num[], int size)` — builds the heap, then swaps the root with the end of the active range until sorted.

Design notes:

- `heapify(...)` is recursive and re-applies itself after a swap.
- The algorithm works in place over the caller’s array.
- The UI labels this option as "Max Heap Sort," which matches the max-heap implementation.

## How It Works

### Startup flow

1. `Frame.main(...)` launches the menu window on the Swing event thread.
2. The user clicks one of the algorithm buttons.
3. That button calls `Sortingss.vis(key, title)` with the chosen algorithm identifier.
4. `Sortingss` stores the key/title and makes its window visible.
5. The user enters integers separated by spaces and clicks Submit.
6. The submit handler parses the input into an `int[]`, routes to the correct sorting implementation, and writes the sorted output back to the UI.

### Algorithm dispatch

The submit handler in `Sortingss.initialize()` uses the numeric `key` to select behavior:

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

This makes `Sortingss` the central dispatcher even though the actual algorithms are split across multiple classes.

### Input/output handling

- Input is read from a `JTextField` as a single string.
- The string is split on spaces with `split(" ")`.
- Each token is parsed with `Integer.parseInt(...)`.
- After sorting, the numbers are converted back to strings and appended to a label prefix such as `Bubble Sort =` or `Heap Sort =`.
- The result is written into a non-editable `JTextArea`.

### Open Code action

The "Open Code" button checks the current `key` and launches Notepad with a corresponding text file name:

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

This appears to be a companion feature for showing algorithm source or notes alongside the live sort demo.

## Data Model

This module does not define domain entities, DTOs, or persistent models. Its working data is limited to:

- a string title (`tname`)
- an integer algorithm selector (`key`)
- user-entered input text
- an `int[]` array of numbers to sort
- temporary arrays used by merge sort

The data flow is simple: text input is parsed into an integer array, sorted in memory, and rendered back into the output area.

## Mermaid Diagram

```mermaid
flowchart TD
Frame["Frame"] --> Sortingss["Sortingss"]
Sortingss --> Quiks["quiks"]
Sortingss --> Merg["merg"]
Sortingss --> Heaps["heaps"]
Frame --> MenuButtons["Algorithm selection buttons"]
Sortingss --> InputField["Space-separated integer input"]
Sortingss --> OutputArea["Sorted result display"]
```

## Dependencies and Integration

### Swing UI

The module depends on standard Java Swing and AWT classes for the desktop interface:

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

### External process launch

`Sortingss` uses `ProcessBuilder` in the "Open Code" button handler to launch `Notepad.exe` with a file name. This is a Windows-specific behavior and is the only non-Swing integration visible in the code.

### Package coupling

There are no resolved package dependencies beyond standard libraries. The source files in `algo_Calc` are tightly coupled to each other:

- `Frame` instantiates `Sortingss`
- `Sortingss` instantiates `quiks`, `merg`, and `heaps`
- the helper classes live in the same source file as `Sortingss`

## Notes for Developers

- The UI uses absolute positioning (`setLayout(null)` plus manual bounds). Any new component must be positioned manually unless the layout strategy changes.
- Input parsing assumes values are separated by single spaces. Extra whitespace or invalid tokens will likely cause parsing issues.
- Sorting is performed on integer arrays only. There is no validation for empty input or malformed numbers.
- `vis(int k, String str)` is the selection entry point. If you add another algorithm, you need to update both the menu window and the `key` dispatch logic in `Sortingss`.
- Bubble sort, selection sort, and insertion sort are implemented inline inside the submit handler, while quick/merge/heap sort are delegated. If you want consistency or easier testing, this would be a natural place to refactor.
- The "Open Code" feature is hard-coded to Windows Notepad and file names in the working directory. On other platforms it will not behave as intended.
- `quiks`, `merg`, and `heaps` are package-private helper classes with short, unconventional names. Their purpose is clear from usage, but their naming is not descriptive.

## Class Relationship Summary

- `Frame` is the launcher and algorithm chooser.
- `Sortingss` is the main interactive sorter window.
- `quiks`, `merg`, and `heaps` implement the recursive/utility sorting algorithms used by `Sortingss`.

The overall design is a simple demo application: one window chooses the algorithm, another window accepts input and shows the sorted output.
