# Algo_calc

## Overview

`algo_Calc` is a small Swing-based sorting visualizer. It appears to provide a menu of sorting algorithms, accept a space-separated list of integers, run the selected algorithm, and display the sorted result in the UI. The module also includes an `Open Code` action that launches algorithm-specific text files in Notepad, which suggests it is meant both as a demonstrator and as a teaching aid.

The module is self-contained: there are no resolved package dependencies and no cross-module links in the index. Most of the behavior lives in two files: `Frame.java`, which presents the algorithm selection screen, and `Sortingss.java`, which contains the main input/output window plus the sorting implementations.

## Key Classes and Interfaces

### `Frame`

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

`Frame` is the entry window for the application. Its responsibility is narrow: show a menu of sorting algorithms and hand control to `Sortingss` with the selected algorithm identifier.

#### What it does

- Creates a `JFrame` with six buttons:
  - Bubble Sort
  - Selection Sort
  - Quick Sort
  - Insertion Sort
  - Merge Sort
  - Max Heap Sort
- On button click, it calls `Sortingss.vis(int k, String str)` with:
  - a numeric key that identifies the algorithm
  - a display label such as `"BUBBLE SORT"`
- After delegating, it hides the menu window with `frame.setVisible(false)`.

#### Key methods

- `main(String[] args)` — starts the Swing UI on the AWT event thread and shows the `Frame` window.
- `Frame()` — constructs the object and immediately calls `initialize()`.
- `initialize()` — builds the menu UI and wires each button to a `Sortingss` instance.

#### Why it exists

This class separates algorithm choice from algorithm execution. That keeps the first screen simple and makes the user flow explicit: choose a sort first, then enter input in the dedicated sorting window.

### `Sortingss`

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

`Sortingss` is the main functional window and the center of the module. It stores the currently selected algorithm, accepts user input, performs the chosen sort, and shows both the algorithm name and the sorted output.

#### What it does

- Builds the main window with:
  - a title area
  - a text field for space-separated integers
  - an output area for the sorted result
  - `Submit`, `Exit`, and `Open Code` buttons
- Uses a `key` field to decide which algorithm should run.
- Parses the input text into an `int[]`.
- Sorts the array using one of six algorithm paths.
- Formats the result and writes it back to the UI.
- Opens an external `.txt` file in Notepad for the selected algorithm when `Open Code` is pressed.

#### Key methods

- `main(String[] args)` — starts the app and shows the `Sortingss` window.
- `Sortingss()` — constructs the object and initializes the frame.
- `vis(int k, String str)` — sets the selected algorithm key and label, then makes the frame visible.
- `initialize()` — creates the UI, registers listeners, and implements the submit/open-code behavior.

#### Related helper classes inside the file

`Sortingss.java` also defines three helper classes that implement the sorting algorithms:

- `quiks` — quick sort implementation
- `merg` — merge sort implementation
- `heaps` — heap sort implementation

These are not separate files, but they are important design units because `Sortingss.initialize()` delegates to them for algorithm execution.

### `quiks`

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

`quiks` contains the quick sort implementation. It is a utility class with no visible state.

#### Methods

- `quickSort(int[] num, int start, int end)` — recursive quick sort driver.
- `partition(int[] num, int start, int end)` — partitions the array around the last element as pivot and returns the pivot’s final index.

#### Role in the module

`Sortingss` uses this class when `key == 3`. The algorithm is implemented directly with in-place array mutation.

### `merg`

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

`merg` contains the merge sort implementation.

#### Methods

- `merge(int num[], int l, int m, int r)` — merges two sorted subarrays back into the source array.
- `mergeSort(int num[], int l, int r)` — recursive divide-and-conquer sort driver.

#### Role in the module

`Sortingss` uses this class when `key == 5`. The implementation uses temporary left and right buffers and then copies the merged result back into the original array.

### `heaps`

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

`heaps` contains the heap sort implementation.

#### Methods

- `heapify(int num[], int size, int i)` — restores the max-heap property for a subtree.
- `heapSort(int num[], int size)` — builds the heap and repeatedly extracts the maximum element to sort the array.

#### Role in the module

`Sortingss` uses this class when `key == 6`. The algorithm mutates the input array in place.

## How It Works

### User flow

1. The application starts in `Frame.main()`.
2. `Frame.initialize()` shows the algorithm menu.
3. The user selects a sort, which calls `Sortingss.vis(k, str)`.
4. `vis()` stores the selected algorithm in `key`, updates the title label text, and makes the main sorting window visible.
5. The user enters integers separated by spaces and clicks `Submit`.
6. `Sortingss.initialize()` parses the input, chooses the algorithm branch based on `key`, sorts the array, and renders the output.
7. Optionally, `Open Code` launches a Notepad file for the selected algorithm.

### Processing the submitted input

The submit handler in `Sortingss.initialize()` follows the same high-level pattern for every algorithm:

1. Read the raw text from the input field.
2. Split the string on spaces.
3. Parse each token into an integer.
4. Sort the `int[]` according to `key`.
5. Convert the sorted integers back to strings.
6. Concatenate them into a single output string.
7. Write the algorithm name into the title area and the sorted list into the output area.

### Algorithm dispatch

The `key` field determines which algorithm runs:

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

That means `Sortingss` acts as both controller and view, while the helper classes provide algorithm-specific logic for the recursive or more structured implementations.

### Open Code behavior

The `Open Code` button uses `ProcessBuilder` to start `Notepad.exe` with an algorithm-specific text file:

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

This appears to be intended as a companion feature for learning or demoing the algorithm alongside the live sort result.

## Data Model

There are no domain entities or persistent models in this module. The core data structures are transient and UI-driven:

- `int[] num` — the working array built from user input
- `String[] tok` — input tokens from splitting the text field value
- `String tname` — the algorithm title shown in the header
- `int key` — algorithm selector shared between the menu and main window

The data flows are simple and ephemeral: text input becomes an integer array, the array is sorted in place, and the result is converted back into display text.

## Dependencies and Integration

### Internal relationships

The module has one internal integration path:

- `Frame` creates a `Sortingss` instance and uses `vis()` to open the main window for the selected algorithm.
- `Sortingss` uses helper classes `quiks`, `merg`, and `heaps` for the algorithm implementations that are not written inline.

### External libraries

The code relies on standard Java Swing and AWT classes only:

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

### External process integration

`Open Code` shells out to `Notepad.exe`. That means the feature is platform-specific and assumes a Windows environment with Notepad available in the expected path.

## Mermaid Diagram

```mermaid
flowchart TD
    FrameClass["Frame"] --> SortingssClass["Sortingss"]
    FrameClass --> SortChoiceButtons["Sort selection buttons"]
    SortChoiceButtons --> VisMethod["Sortingss.vis(k, str)"]
    VisMethod --> SortWindow["Sorting window state"]
    SortWindow --> SubmitAction["Submit button"]
    SubmitAction --> BubbleSort["Bubble sort"]
    SubmitAction --> SelectionSort["Selection sort"]
    SubmitAction --> QuickSort["Quick sort"]
    SubmitAction --> InsertionSort["Insertion sort"]
    SubmitAction --> MergeSort["Merge sort"]
    SubmitAction --> HeapSort["Heap sort"]
    SubmitAction --> OutputArea["Result text area"]
    SubmitAction --> TitleArea["Algorithm title area"]
    SubmitAction --> OpenCode["Open Code button"]
```

## Class Relationships

```mermaid
classDiagram
    class Frame {
        -JFrame frame
        +main(String[] args) void
        +Frame()
        -initialize() void
    }
    class Sortingss {
        -JFrame frame
        -JTextField textField
        -int key
        -String tname
        +main(String[] args) void
        +Sortingss()
        +vis(int k, String str) int
        -initialize() void
    }
    class quiks {
        +quickSort(int[] num, int start, int end) void
        +partition(int[] num, int start, int end) int
    }
    class merg {
        +merge(int[] num, int l, int m, int r) void
        +mergeSort(int[] num, int l, int r) void
    }
    class heaps {
        +heapify(int[] num, int size, int i) void
        +heapSort(int[] num, int size) void
    }
    Frame --> Sortingss
    Sortingss --> quiks
    Sortingss --> merg
    Sortingss --> heaps
```

## Notes for Developers

- The module uses `null` layouts and fixed bounds for every Swing component. That makes the UI straightforward but brittle if the window size, font, or platform rendering changes.
- `Sortingss.vis()` sets `tname` through a couple of placeholder assignments before writing the real value. Those intermediate assignments do not affect behavior and look like leftover experimentation.
- The parsing logic expects integers separated by single spaces. There is no validation for empty input, repeated spaces, or non-numeric tokens, so malformed input will throw at `Integer.parseInt(...)`.
- Bubble sort, selection sort, and insertion sort are implemented inline in the submit handler, while quick sort, merge sort, and heap sort are factored into helper classes.
- The `Open Code` feature depends on local files and Notepad, so it will not work in a non-Windows environment unless adapted.
- `Frame` and `Sortingss` each create their own `JFrame`, which means the application uses a two-window flow rather than reusing a single frame.

## Method Reference

### `Frame.main(String[] args)`

Starts the application on the Swing event thread and shows the menu frame.

### `Frame.Frame()`

Constructs the menu window by calling `initialize()`.

### `Frame.initialize()`

Creates the algorithm selection buttons and binds each one to a specific call to `Sortingss.vis(...)`.

### `quiks.quickSort(int[] num, int start, int end)`

Recursively sorts the selected range using quick sort. It depends on `partition()` to place the pivot and then recurses into the left and right partitions.

### `quiks.partition(int[] num, int start, int end)`

Chooses the last element as pivot, moves smaller elements before it, and returns the pivot’s final index.

### `merg.merge(int num[], int l, int m, int r)`

Merges two sorted halves `[l..m]` and `[m+1..r]` into the original array.

### `merg.mergeSort(int num[], int l, int r)`

Recursively splits the array until single-element ranges remain, then merges them back into sorted order.

### `heaps.heapify(int num[], int size, int i)`

Restores the max-heap property at index `i` and may recurse down the affected subtree.

### `heaps.heapSort(int num[], int size)`

Builds a max heap, then repeatedly swaps the root with the end of the unsorted region to produce ascending order.

### `Sortingss.main(String[] args)`

Starts the sorting window application on the Swing event thread.

### `Sortingss.Sortingss()`

Constructs the sorting window by calling `initialize()`.

### `Sortingss.vis(int k, String str)`

Stores the selected algorithm key and title, then makes the main window visible. It returns `0` and has no other functional result.

### `Sortingss.initialize()`

Creates the full sorting UI, parses input on submit, dispatches to the selected sort, formats output, and handles the `Exit` and `Open Code` actions.
