# Algo_calc

## Overview

`algo_Calc` appears to be a small Swing-based sorting visualizer and calculator. It provides a menu for choosing one of several classic sorting algorithms, then opens a second window where the user can enter space-separated integers, run the selected sort, and view the sorted result. The module also exposes an `Open Code` action that launches algorithm-specific text files in Notepad, suggesting it was designed as both an interactive demo and a teaching aid.

The module is self-contained: the menu window lives in [`Frame`](src/algo_Calc/Frame.java:9), and the main sorting UI plus the algorithm implementations live in [`Sortingss`](src/algo_Calc/Sortingss.java:162) and its helper classes.

## Key Classes and Interfaces

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

`Frame` is the entry-point menu window. Its job is to present the user with a set of algorithm choices and then hand off control to `Sortingss` when a button is clicked.

Key responsibilities:

- Creates the top-level Swing `JFrame`.
- Displays one button per algorithm: Bubble Sort, Selection Sort, Quick Sort, Insertion Sort, Merge Sort, and Max Heap Sort.
- Calls [`Sortingss.vis(int, String)`](src/algo_Calc/Sortingss.java:190) with a numeric algorithm key and a display title when a user chooses an option.
- Hides the menu window after a selection is made.

Important methods:

- [`Frame.main(String[] args)`](src/algo_Calc/Frame.java:16) — starts the GUI on the AWT event dispatch thread.
- [`Frame.Frame()`](src/algo_Calc/Frame.java:32) — constructor that initializes the UI.
- [`Frame.initialize()`](src/algo_Calc/Frame.java:39) — creates the menu frame and wires button listeners.

Design note: `Frame` owns no sorting logic. It is strictly a launcher and navigation surface for the sorter window.

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

`Sortingss` is the main application window and the central coordinator for user input, sorting, and output. It stores the currently selected algorithm in `key` and the current title in `tname`, then performs the actual sort when the user submits input.

Key responsibilities:

- Builds the main input/output window.
- Reads space-separated integers from a text field.
- Dispatches to one of six sorting implementations based on `key`.
- Renders the chosen algorithm name in the header area and the sorted result in the output area.
- Opens a supporting text file for the selected algorithm through Notepad.

Important methods:

- [`Sortingss.main(String[] args)`](src/algo_Calc/Sortingss.java:171) — starts the application.
- [`Sortingss.Sortingss()`](src/algo_Calc/Sortingss.java:187) — constructor that initializes the frame.
- [`Sortingss.vis(int k, String str)`](src/algo_Calc/Sortingss.java:190) — sets the selected algorithm and shows the window.
- [`Sortingss.initialize()`](src/algo_Calc/Sortingss.java:204) — builds the form, submit handler, exit button, and Open Code button.

Design note: despite the class name, `Sortingss` is not a sorter implementation itself; it is the UI controller that invokes the sorting helpers and also contains some inline bubble, selection, and insertion sort code.

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

`quiks` is a helper class that contains quick sort logic. It is used only when the user selects Quick Sort.

Key responsibilities:

- Partitions an integer array around a pivot.
- Recursively sorts the left and right partitions.

Important methods:

- [`quiks.quickSort(int[] num, int start, int end)`](src/algo_Calc/Sortingss.java:19) — recursive quick sort driver.
- [`quiks.partition(int[] num, int start, int end)`](src/algo_Calc/Sortingss.java:32) — partitions the array and returns the pivot index.

Implementation note: `partition` uses the last element as the pivot and swaps smaller elements forward in place.

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

`merg` is the merge sort helper class. It exists to keep the merge logic separate from the UI class.

Key responsibilities:

- Merge two sorted subarrays back into the source array.
- Recursively split the array and merge the halves.

Important methods:

- [`merg.merge(int num[], int l, int m, int r)`](src/algo_Calc/Sortingss.java:58) — merges two sorted slices.
- [`merg.mergeSort(int num[], int l, int r)`](src/algo_Calc/Sortingss.java:105) — recursively sorts the array.

Implementation note: this is a standard top-down merge sort with temporary left and right buffers.

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

`heaps` contains the heap sort implementation used by the Heap Sort option.

Key responsibilities:

- Maintain the max-heap property for a subtree.
- Repeatedly move the maximum element to the end of the array.

Important methods:

- [`heaps.heapify(int num[], int size, int i)`](src/algo_Calc/Sortingss.java:121) — restores max-heap order from a node downward.
- [`heaps.heapSort(int num[], int size)`](src/algo_Calc/Sortingss.java:144) — builds the heap and sorts the full array.

Implementation note: the algorithm is in-place and uses array indices to compute child positions.

## How It Works

### Application flow

1. The user launches [`Frame.main`](src/algo_Calc/Frame.java:16).
2. `Frame` opens the algorithm selection window.
3. The user clicks one of the sort buttons.
4. The corresponding listener calls [`Sortingss.vis`](src/algo_Calc/Sortingss.java:190) with:
   - a numeric algorithm key (`1` through `6`)
   - a title string such as `BUBBLE SORT` or `MERGE SORT`
5. `Sortingss` shows the main input window.
6. The user enters integers separated by spaces and clicks Submit.
7. The submit handler parses the input into an `int[]`, runs the selected algorithm, and writes the formatted result to the output area.
8. The header area is updated with the active algorithm name.

### Algorithm dispatch

The submit handler in [`Sortingss.initialize()`](src/algo_Calc/Sortingss.java:204) uses `key` to choose a sorting path:

- `1` — inline bubble sort
- `2` — inline selection sort
- `3` — [`quiks.quickSort`](src/algo_Calc/Sortingss.java:19)
- `4` — inline insertion sort
- `5` — [`merg.mergeSort`](src/algo_Calc/Sortingss.java:105)
- `6` — [`heaps.heapSort`](src/algo_Calc/Sortingss.java:144)

After sorting, the code converts the numbers back to strings and appends them to a label such as `Bubble Sort =` or `Merge Sort =`.

### Output and supporting actions

The window includes two extra actions:

- **Exit** — hides the sorter window.
- **Open Code** — launches a Notepad process for an algorithm-specific text file such as `bubble.txt`, `merge.txt`, or `maxheap.txt`.

This appears to be a learning-oriented feature that pairs the runtime demo with readable source notes or walkthroughs outside the Java application.

## Data Model

There are no domain entities or persistent models in this module. The module works with simple in-memory data structures:

- `int[] num` — parsed input values and the array being sorted.
- `String[] tok` — tokenized text input from the UI.
- `String tname` — the currently selected algorithm label displayed in the header.
- `int key` — an algorithm selector used by the dispatch logic.

The module's state is mostly UI state plus the current input array. There is no external storage, database, or object graph beyond the Swing components.

## Dependencies and Integration

### Java platform APIs used

The source imports only standard Java desktop APIs:

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

### External integration behavior

The `Open Code` button uses `ProcessBuilder` to launch `Notepad.exe` with a text file name. That means this feature is Windows-specific and depends on those text files being present in the working directory or reachable from the current launch context.

### Intra-module relationships

```mermaid
flowchart TD
FrameClass["Frame"] --> SortingssClass["Sortingss"]
SortingssClass --> QuiksClass["quiks"]
SortingssClass --> MergClass["merg"]
SortingssClass --> HeapsClass["heaps"]
FrameClass --> MainMenu["Algorithm selection window"]
SortingssClass --> SortWindow["Input and output window"]
```

## Notes for Developers

- **Input format matters.** The submit logic expects integers separated by single spaces. Any non-integer token will cause `Integer.parseInt` to fail.
- **The selected algorithm is stored as mutable UI state.** `key` determines which algorithm runs, so any future code path that opens the sorter window must set it first via [`vis`](src/algo_Calc/Sortingss.java:190).
- **Some sorting logic is inline, some is delegated.** Bubble sort, selection sort, and insertion sort are coded directly in the submit handler, while quick sort, merge sort, and heap sort live in helper classes. If you extend the module, decide whether to keep that split or consolidate it.
- **The `Open Code` feature is environment-specific.** It assumes Notepad is available and that the referenced text files exist. That behavior will not be portable to non-Windows systems without changes.
- **UI layout is absolute-position based.** Both windows use `null` layouts and `setBounds(...)`, so resizing behavior is limited and future UI changes will likely require manual coordinate updates.
- **The `vis` method currently returns `0`.** It appears to act as a command rather than a query. If the code is refactored later, it may be a candidate for a `void` return type.
- **Algorithm titles are presentation-only.** The strings passed into `vis` are used to update the UI header; they do not affect the actual sorting algorithm chosen.

## Method Reference

### [`Frame.main(String[] args)`](src/algo_Calc/Frame.java:16)
Launches the application on the event dispatch thread and makes the menu frame visible.

### [`Frame.Frame()`](src/algo_Calc/Frame.java:32)
Constructs the menu window by calling `initialize()`.

### [`Frame.initialize()`](src/algo_Calc/Frame.java:39)
Builds the menu frame and attaches button listeners for each sorting algorithm.

### [`quiks.quickSort(int[] num, int start, int end)`](src/algo_Calc/Sortingss.java:19)
Recursively sorts the subarray between `start` and `end` using quick sort. It partitions the array first, then sorts the left and right sides if they contain more than one element.

### [`quiks.partition(int[] num, int start, int end)`](src/algo_Calc/Sortingss.java:32)
Chooses the last element as pivot, moves smaller elements to the left side, places the pivot in its final position, and returns that final index.

### [`merg.merge(int num[], int l, int m, int r)`](src/algo_Calc/Sortingss.java:58)
Merges two sorted halves of `num` back into a single sorted range from `l` to `r`.

### [`merg.mergeSort(int num[], int l, int r)`](src/algo_Calc/Sortingss.java:105)
Recursively divides the array into halves and merges them in sorted order.

### [`heaps.heapify(int num[], int size, int i)`](src/algo_Calc/Sortingss.java:121)
Restores the max-heap property for the subtree rooted at index `i`.

### [`heaps.heapSort(int num[], int size)`](src/algo_Calc/Sortingss.java:144)
Builds a max heap and then repeatedly extracts the maximum value to sort the array in place.

### [`quiks.main(String[] args)`](src/algo_Calc/Sortingss.java:171)
Launches the `Sortingss` application window.

### [`quiks.Sortingss()`](src/algo_Calc/Sortingss.java:187)
Constructs the sorter window by calling `initialize()`.

### [`quiks.vis(int k, String str)`](src/algo_Calc/Sortingss.java:190)
Stores the selected algorithm key and title, makes the sorter window visible, and returns `0`.

### [`quiks.initialize()`](src/algo_Calc/Sortingss.java:204)
Builds the sorter UI, parses the user input, dispatches to the chosen sorting implementation, and renders the output.
