diff --git a/README.md b/README.md index 4ae95d0..32f0a0f 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,29 @@

Insertion sort gif

-## How to build executable file? +Welcome to our sorting algorithm visualization project! This program offers real-time visualizations of various sorting +algorithms, allowing you to understand and compare them in action. It currently supports the following algorithms: -In an IDE's terminal: +- Bubble Sort +- Selection Sort +- Quick Sort +- Insertion Sort +- Heap Sort + +## Table of Contents + +- [How to Build executable file?](#how-to-build-executable-file) +- [How to Run](#how-to-run) +- [Algorithms](#algorithms) + - [Bubble Sort](#bubble-sort) + - [Selection Sort](#selection-sort) + - [Quick Sort](#quick-sort) + - [Insertion Sort](#insertion-sort) + - [Heap Sort](#heap-sort) + +## How to Build executable file? + +In an IDE's terminal: ``` mvn clean package @@ -14,12 +34,60 @@ mvn clean package This command will create `/taget` folder and executable `sav-version-jar-with-dependencies.jar` inside. -## How to run? +## How to Run? -1. Build project or download latest release from release section. +1. Build project using the instructions above, or download latest release from release section. -2. Open terminal in a folder with `.jar` file and run this command: +2. Open terminal, navigate to your project folder, and then go into the ```target``` directory where the `.jar` file is + located. Run this command to start the program: ``` java -jar sav-1.0-jar-with-dependencies.jar -``` \ No newline at end of file +``` + +# Algorithms + +## Bubble Sort + +**Bubble Sort** is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and +swaps them if they are in the wrong order. This process continues until the list is fully sorted, with larger elements " +bubbling" to the top. For e.g: in ascending order, the largest elements gradually move to the last position with each +pass. + +
Show Bubble Sort Visualization

Bubble Sort visualization

+ +## Selection Sort + +**Selection Sort** is a simple comparison-based algorithm that sorts a list by dividing it into a sorted and an unsorted +part. It repeatedly finds the smallest (or largest) element from the unsorted part and swaps it with the first unsorted +element, gradually growing the sorted portion until the entire list is sorted. + +
Show Selection Sort Visualization

Selection Sort visualization

+ +## Quick Sort + +**QuickSort** is a highly efficient sorting algorithm that uses a divide-and-conquer approach. It selects a "pivot" +element and partitions the array into two sub-arrays: one with elements less than the pivot and one with elements +greater than +the pivot. This process is recursively applied to each sub-array until the entire list is sorted. + +
Show Quick Sort Visualization

Quick Sort visualization

+ +## Insertion Sort + +**Insertion Sort** is a simple sorting algorithm that builds a sorted list one element at a time. It picks the next +element +from the unsorted list, finds its correct position in the sorted list, and inserts it there. This process repeats until +all elements are sorted. + +
Show Insertion Sort Visualization

Insertion Sort visualization

+ +## Heap Sort + +**HeapSort** is a sorting algorithm that uses a binary heap data structure to sort elements. It first builds a max heap +from the input data, then repeatedly removes the largest element from the heap and places it at the end of the list, +sorting +the list in ascending order. + + +
Show Heap Sort Visualization

Insertion Heap visualization

diff --git a/src/main/java/algorithms/SortingAlgorithm.java b/src/main/java/algorithms/SortingAlgorithm.java index 11e6ce2..af02859 100644 --- a/src/main/java/algorithms/SortingAlgorithm.java +++ b/src/main/java/algorithms/SortingAlgorithm.java @@ -1,5 +1,7 @@ package algorithms; +import ui.Utils; + import java.util.ArrayList; public abstract class SortingAlgorithm { @@ -19,16 +21,25 @@ public SortingAlgorithm(ArrayList array) { public void printStatistics(String algorithmName) { clearScreen(); - System.out.println("\n========= " + algorithmName + " Statistics: ========="); - System.out.printf("+--------------------+--------------------+%n"); - System.out.printf("| Metric | Value |%n"); - System.out.printf("+--------------------+--------------------+%n"); - System.out.printf("| Array Size | %-18d |%n", array.size()); - System.out.printf("| Total Comparisons | %-18d |%n", comparisonCount); - System.out.printf("| Total Swaps/Shifts | %-18d |%n", swapCount); - System.out.printf("+--------------------+--------------------+%n"); + Utils.printInCenter("========= " + algorithmName + " Statistics: =========", " "); + + String header = "+--------------------+--------------------+"; + String metrics = "| Metric | Value |"; + String separator = "+--------------------+--------------------+"; + String arraySize = String.format("| %-18s | %-18d |", "Array Size", array.size()); + String comparisons = String.format("| %-18s | %-18d |", "Total Comparisons", comparisonCount); + String swaps = String.format("| %-18s | %-18d |", "Total Swaps/Shifts", swapCount); + + Utils.printInCenter(header, " "); + Utils.printInCenter(metrics, " "); + Utils.printInCenter(separator, " "); + Utils.printInCenter(arraySize, " "); + Utils.printInCenter(comparisons, " "); + Utils.printInCenter(swaps, " "); + Utils.printInCenter(separator, " "); } + private static void clearScreen() { System.out.print("\033[H\033[2J"); System.out.flush();