├── src └── main │ ├── resources │ ├── images │ │ ├── hourglass.gif │ │ └── sortanimation.gif │ ├── fonts │ │ ├── FiraCode-Regular.ttf │ │ └── OpenSans-Regular.ttf │ ├── styles │ │ └── graphicspane.css │ └── fxml │ │ ├── FXMLAnimationPane.fxml │ │ └── FXMLMainPane.fxml │ └── java │ └── sortingalgoritms │ ├── util │ ├── ISortOperator.java │ ├── IComparable.java │ ├── Logger.java │ ├── CompareValue.java │ └── RandomValues.java │ ├── sorts │ ├── AbstractSort.java │ ├── SortOperator.java │ ├── CExchangeSort.java │ ├── CInsertionSort.java │ ├── SortOperatorList.java │ ├── CSelectionSort.java │ ├── CPancakeSort.java │ ├── CCocktailSort.java │ ├── CBubbleSort.java │ ├── CShellSort.java │ ├── CHeapSort.java │ ├── CQuickSort.java │ └── CMergeSort.java │ ├── MainApp.java │ ├── ui │ └── AnimationController.java │ └── MainController.java ├── .travis.yml ├── README.md ├── nbactions.xml ├── nb-configuration.xml ├── pom.xml └── LICENSE.md /src/main/resources/images/hourglass.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EricCanull/fxsortinganimation/HEAD/src/main/resources/images/hourglass.gif -------------------------------------------------------------------------------- /src/main/resources/images/sortanimation.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EricCanull/fxsortinganimation/HEAD/src/main/resources/images/sortanimation.gif -------------------------------------------------------------------------------- /src/main/resources/fonts/FiraCode-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EricCanull/fxsortinganimation/HEAD/src/main/resources/fonts/FiraCode-Regular.ttf -------------------------------------------------------------------------------- /src/main/resources/fonts/OpenSans-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EricCanull/fxsortinganimation/HEAD/src/main/resources/fonts/OpenSans-Regular.ttf -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | 3 | jdk: oraclejdk8 4 | 5 | sudo: false 6 | 7 | addons: 8 | apt: 9 | packages: 10 | - oracle-java8-installer 11 | 12 | 13 | install: true 14 | 15 | script: mvn clean install 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # FX Sort Animation Demo 2 | [![Build Status](https://travis-ci.org/EricCanull/fxsortinganimation.svg?branch=master)](https://travis-ci.org/EricCanull/fxsortinganimation) 3 | 4 | ### JPRO WebVersion 5 | Thanks to JPRO for featuring a live demo version using their software which runs Java on the web. 6 | - The [web version](https://demos.jpro.one/sorting.html) is hosted at [www.jpro.one](https://www.jpro.one/). 7 | 8 | ### Visualize the following sorting algorithms 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 |
Bubble SortInsertion SortSelection SortMerge SortHeap Sort
Quick SortCocktail SortShell SortPancake SortExchange Sort
27 | 28 | ### Preview 29 | ![alt text](https://github.com/EricCanull/fxsortinganimation/blob/master/src/main/resources/images/sortanimation.gif "Sort Demo") 30 | 31 | 32 | -------------------------------------------------------------------------------- /nbactions.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | run 5 | 6 | clean 7 | package 8 | org.codehaus.mojo:exec-maven-plugin:1.2.1:exec 9 | 10 | 11 | -jar "${project.build.directory}/${project.build.finalName}.jar" 12 | 13 | 14 | 15 | debug 16 | 17 | clean 18 | package 19 | org.codehaus.mojo:exec-maven-plugin:1.2.1:exec 20 | 21 | 22 | -Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address} -Dglass.disableGrab=true -jar "${project.build.directory}/${project.build.finalName}.jar" 23 | true 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /src/main/java/sortingalgoritms/util/ISortOperator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This program is free software: you can redistribute it and/or modify 3 | * it under the terms of the GNU General Public License as published by 4 | * the Free Software Foundation, either version 3 of the License, or 5 | * any later version. 6 | * 7 | * This program is distributed in the hope that it will be useful, 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | * GNU General Public License for more details. 11 | * 12 | * You should have received a copy of the GNU General Public License 13 | * along with this program. If not, see . 14 | */ 15 | package sortingalgoritms.util; 16 | 17 | /** 18 | * Represents the abstract lambda expression whose sole purpose in life is to 19 | * evaluate itself on an input and return the result of the evaluation. 20 | */ 21 | public interface ISortOperator { 22 | 23 | /** 24 | * @param arg input object for the lambda expression. 25 | * @return an output object resulting from evaluating the lambda expression 26 | * on the input arg. 27 | */ 28 | public Object apply(Object arg); 29 | } 30 | -------------------------------------------------------------------------------- /nb-configuration.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 9 | 10 | 16 | none 17 | true 18 | gpl20 19 | true 20 | 21 | 22 | -------------------------------------------------------------------------------- /src/main/java/sortingalgoritms/util/IComparable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This program is free software: you can redistribute it and/or modify 3 | * it under the terms of the GNU General Public License as published by 4 | * the Free Software Foundation, either version 3 of the License, or 5 | * any later version. 6 | * 7 | * This program is distributed in the hope that it will be useful, 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | * GNU General Public License for more details. 11 | * 12 | * You should have received a copy of the GNU General Public License 13 | * along with this program. If not, see . 14 | */ 15 | package sortingalgoritms.util; 16 | 17 | import javafx.scene.paint.Color; 18 | 19 | /** 20 | * Implements the functionality of the collection lists java.lang.Comparable. 21 | */ 22 | public interface IComparable { 23 | 24 | int LESS = -1; 25 | int EQUAL = 0; 26 | int GREATER = 1; 27 | 28 | Color NORMAL_COLOR = Color.web("#3073b4"); 29 | Color SELECTED_COLOR = Color.web("#a07617"); 30 | Color GREATER_COLOR = Color.web("#2da762"); 31 | Color LESS_COLOR = Color.web("#7F5096"); 32 | 33 | /** 34 | * Similar to Comparable.compareTo 35 | * 36 | * @param number a value to compare 37 | * @return the result of the comparison 38 | */ 39 | int compare(IComparable number); 40 | 41 | void setColor(Color color); 42 | 43 | Color getColor(); 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/sortingalgoritms/sorts/AbstractSort.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This program is free software: you can redistribute it and/or modify 3 | * it under the terms of the GNU General Public License as published by 4 | * the Free Software Foundation, either version 3 of the License, or 5 | * any later version. 6 | * 7 | * This program is distributed in the hope that it will be useful, 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | * GNU General Public License for more details. 11 | * 12 | * You should have received a copy of the GNU General Public License 13 | * along with this program. If not, see . 14 | */ 15 | package sortingalgoritms.sorts; 16 | 17 | import sortingalgoritms.util.IComparable; 18 | import sortingalgoritms.util.Logger; 19 | 20 | /** 21 | * An abstract base for the concrete sorting classes 22 | * 23 | * @author Eric Canull 24 | */ 25 | public abstract class AbstractSort { 26 | 27 | /** 28 | * Sets up the sorting data and then invokes the abstract method to start 29 | * sorting. 30 | * 31 | * @param numbers the array of numbers to be sorted. 32 | * @param lowIndex the low index of the array. 33 | * @param highIndex the high index of array. 34 | */ 35 | public void sort(IComparable[] numbers, int lowIndex, int highIndex) { 36 | Logger.initiateLog(); 37 | startSort(numbers, lowIndex, highIndex); 38 | Logger.terminateLog(); 39 | } 40 | 41 | /** 42 | * The abstract method used by all of the concrete sorting classes 43 | * 44 | * @param numbers the array of numbers to be sorted. 45 | * @param lowIndex the low index of the array. 46 | * @param highIndex the high index of array. 47 | */ 48 | protected abstract void startSort(IComparable[] numbers, int lowIndex, int highIndex); 49 | 50 | /** 51 | * Increments iteration count. 52 | */ 53 | protected void count() { 54 | Logger.count(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/sortingalgoritms/sorts/SortOperator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This program is free software: you can redistribute it and/or modify 3 | * it under the terms of the GNU General Public License as published by 4 | * the Free Software Foundation, either version 3 of the License, or 5 | * any later version. 6 | * 7 | * This program is distributed in the hope that it will be useful, 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | * GNU General Public License for more details. 11 | * 12 | * You should have received a copy of the GNU General Public License 13 | * along with this program. If not, see . 14 | */ 15 | package sortingalgoritms.sorts; 16 | 17 | import java.util.Arrays; 18 | import sortingalgoritms.util.ISortOperator; 19 | 20 | /** 21 | * Creates a singleton for retrieving the bars array data as its being sorted 22 | * after the timer animation is started. 23 | * 24 | * @author Eric Canull 25 | * @version 1.0 26 | */ 27 | public class SortOperator implements ISortOperator { 28 | private SortOperator() {} /* non-use private constructor */ 29 | 30 | private static SortOperator sortOperator = null; 31 | 32 | public static SortOperator getInstance() { 33 | if (sortOperator == null) { 34 | sortOperator = new SortOperator(); 35 | } 36 | return sortOperator; 37 | } 38 | 39 | /** 40 | * Returns the sorted array every time the timer running 41 | * @param objects an array being sorted 42 | * @param sortOperator an object that returns the sorted array 43 | * @return an output sorted object or null 44 | */ 45 | public Object[] apply(Object objects[], ISortOperator sortOperator) { 46 | Object[] result = Arrays.copyOf(objects, objects.length); 47 | sortOperator.apply(objects); 48 | return result; 49 | } 50 | 51 | /** 52 | * Null object pattern method 53 | * @param arg an object 54 | */ 55 | @Override 56 | public Object apply(Object arg) { 57 | return null; 58 | } 59 | } -------------------------------------------------------------------------------- /src/main/java/sortingalgoritms/MainApp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This program is free software: you can redistribute it and/or modify 3 | * it under the terms of the GNU General Public License as published by 4 | * the Free Software Foundation, either version 3 of the License, or 5 | * any later version. 6 | * 7 | * This program is distributed in the hope that it will be useful, 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | * GNU General Public License for more details. 11 | * 12 | * You should have received a copy of the GNU General Public License 13 | * along with this program. If not, see . 14 | */ 15 | package sortingalgoritms; 16 | 17 | import javafx.application.Application; 18 | import javafx.fxml.FXMLLoader; 19 | import javafx.scene.Scene; 20 | import javafx.scene.text.Font; 21 | import javafx.stage.Stage; 22 | 23 | /** 24 | * JavaFX sorting algorithms demo application 25 | * 26 | * @author Eric Canull 27 | * @version 1.0 28 | */ 29 | public class MainApp extends Application { 30 | 31 | @Override 32 | public void start(Stage stage) throws Exception { 33 | 34 | // Load custom fonts used in css stylesheet 35 | Font.loadFont(MainApp.class.getResource("/fonts/OpenSans-Regular.ttf").toExternalForm(), 10); 36 | Font.loadFont(MainApp.class.getResource("/fonts/FiraCode-Regular.ttf").toExternalForm(), 10); 37 | 38 | Scene scene = new Scene(FXMLLoader.load(getClass().getResource("/fxml/FXMLMainPane.fxml"))); 39 | stage.setTitle("Sorting Demo"); 40 | stage.setScene(scene); 41 | stage.show(); 42 | } 43 | 44 | /** 45 | * The main() method is ignored in correctly deployed JavaFX application. 46 | * main() serves only as fallback in case the application can not be 47 | * launched through deployment artifacts, e.g., in IDEs with limited FX 48 | * support. NetBeans ignores main(). 49 | * 50 | * @param args the command line arguments 51 | */ 52 | public static void main(String[] args) { 53 | launch(args); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/sortingalgoritms/sorts/CExchangeSort.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This program is free software: you can redistribute it and/or modify 3 | * it under the terms of the GNU General Public License as published by 4 | * the Free Software Foundation, either version 3 of the License, or 5 | * any later version. 6 | * 7 | * This program is distributed in the hope that it will be useful, 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | * GNU General Public License for more details. 11 | * 12 | * You should have received a copy of the GNU General Public License 13 | * along with this program. If not, see . 14 | */ 15 | package sortingalgoritms.sorts; 16 | 17 | import sortingalgoritms.util.IComparable; 18 | 19 | /** 20 | * Implementation of the exchange sort algorithm. 21 | * 22 | * @author Eric Canull 23 | */ 24 | public final class CExchangeSort extends AbstractSort { 25 | 26 | public static final CExchangeSort SINGLETON = new CExchangeSort(); 27 | 28 | /** Implementation of the exchange sort algorithm. */ 29 | private CExchangeSort() { } 30 | 31 | /** 32 | * Starts the exchange sort algorithm. 33 | * 34 | * @param numbers an array of numbers used for the sorting 35 | * @param lowIndex a integer representing the lowest index position in the 36 | * array 37 | * @param highIndex a integer representing the highest index position in the 38 | * array 39 | */ 40 | @Override 41 | protected void startSort(IComparable[] numbers, int lowIndex, int highIndex) { 42 | IComparable temp; 43 | for (int i = 0; i < numbers.length - 1; i++) { 44 | count(); 45 | for (int j = i + 1; j < numbers.length; j++) { 46 | count(); 47 | if (numbers[i].compare(numbers[j]) == IComparable.GREATER) { 48 | temp = numbers[i]; 49 | numbers[i] = numbers[j]; 50 | numbers[j] = temp; 51 | count(); 52 | } 53 | } 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/sortingalgoritms/util/Logger.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This program is free software: you can redistribute it and/or modify 3 | * it under the terms of the GNU General Public License as published by 4 | * the Free Software Foundation, either version 3 of the License, or 5 | * any later version. 6 | * 7 | * This program is distributed in the hope that it will be useful, 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | * GNU General Public License for more details. 11 | * 12 | * You should have received a copy of the GNU General Public License 13 | * along with this program. If not, see . 14 | */ 15 | package sortingalgoritms.util; 16 | 17 | /** 18 | * Performs the counting for the algorithms iterations and provides access to 19 | * the count. 20 | * 21 | * @author Eric Canull 22 | */ 23 | public final class Logger { 24 | 25 | private static String infoText = ""; 26 | private static long count; 27 | public static long startTime, endTime; 28 | 29 | /** 30 | * Resets the iteration count. 31 | */ 32 | public static void initiateLog() { 33 | count = 0; 34 | startTime = System.nanoTime(); 35 | } 36 | 37 | public static void terminateLog() { 38 | endTime = System.nanoTime(); 39 | } 40 | 41 | /** 42 | * Gets the iteration count. 43 | * 44 | * @return The value for the iteration count 45 | */ 46 | public static long getCount() { 47 | return count; 48 | } 49 | 50 | /** 51 | * Increments the count. 52 | */ 53 | public static void count() { 54 | count++; 55 | } 56 | 57 | /** 58 | * Changes the text of this infoText. 59 | * 60 | * @param text a reference specifying the new text of this infoText 61 | */ 62 | public static void setLogText(String text) { 63 | Logger.infoText = text; 64 | } 65 | 66 | /** 67 | * Returns the text in this infoText. 68 | * 69 | * @return a String reference specifying the text in this infoText 70 | */ 71 | public static String getLogText() { 72 | return Logger.infoText; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/sortingalgoritms/sorts/CInsertionSort.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This program is free software: you can redistribute it and/or modify 3 | * it under the terms of the GNU General Public License as published by 4 | * the Free Software Foundation, either version 3 of the License, or 5 | * any later version. 6 | * 7 | * This program is distributed in the hope that it will be useful, 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | * GNU General Public License for more details. 11 | * 12 | * You should have received a copy of the GNU General Public License 13 | * along with this program. If not, see . 14 | */ 15 | package sortingalgoritms.sorts; 16 | 17 | import sortingalgoritms.util.IComparable; 18 | 19 | /** 20 | * Implementation of the insertion sort algorithm. 21 | * 22 | * @author Eric Canull 23 | */ 24 | public final class CInsertionSort extends AbstractSort { 25 | 26 | public static final CInsertionSort SINGLETON = new CInsertionSort(); 27 | 28 | /** Implementation of the insertion sort algorithm. */ 29 | private CInsertionSort() { } 30 | 31 | /** 32 | * Starts the insertion sort algorithm 33 | * 34 | * @param numbers an array of numbers used for the sorting 35 | * @param lowIndex the lowest index position in the array 36 | * @param highIndex the highest index position in the array 37 | */ 38 | @Override 39 | protected void startSort(IComparable[] numbers, int lowIndex, int highIndex) { 40 | // Sub-array used to hold the sorted numbers 41 | IComparable temp; 42 | 43 | // Iterates through numbers array one time, swapping any numbers it finds less than 44 | // it's next index until reaching the last and the array is sorted 45 | for (int i = 1; i < numbers.length; i++) { 46 | count(); 47 | for (int j = i; j > 0; j--) { 48 | count(); 49 | if (numbers[j].compare(numbers[j - 1]) == IComparable.LESS) { 50 | temp = numbers[j]; 51 | numbers[j] = numbers[j - 1]; 52 | numbers[j - 1] = temp; 53 | count(); 54 | } 55 | } 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/sortingalgoritms/sorts/SortOperatorList.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This program is free software: you can redistribute it and/or modify 3 | * it under the terms of the GNU General Public License as published by 4 | * the Free Software Foundation, either version 3 of the License, or 5 | * any later version. 6 | * 7 | * This program is distributed in the hope that it will be useful, 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | * GNU General Public License for more details. 11 | * 12 | * You should have received a copy of the GNU General Public License 13 | * along with this program. If not, see . 14 | */ 15 | package sortingalgoritms.sorts; 16 | 17 | import java.util.ArrayList; 18 | import java.util.List; 19 | 20 | /** 21 | * @author Eric Canull 22 | */ 23 | public class SortOperatorList { 24 | 25 | private final List operators; 26 | 27 | public SortOperatorList() { 28 | 29 | // Add the Sorting classes into the arrayList 30 | operators = new ArrayList<>(); 31 | operators.add(CBubbleSort.SINGLETON); 32 | operators.add(CSelectionSort.SINGLETON); 33 | operators.add(CInsertionSort.SINGLETON); 34 | operators.add(CMergeSort.SINGLETON); 35 | operators.add(CQuickSort.SINGLETON); 36 | operators.add(CShellSort.SINGLETON); 37 | operators.add(CPancakeSort.SINGLETON); 38 | operators.add(CCocktailSort.SINGLETON); 39 | operators.add(CHeapSort.SINGLETON); 40 | operators.add(CExchangeSort.SINGLETON); 41 | } 42 | 43 | public List getList() { 44 | return operators; 45 | } 46 | 47 | /** 48 | * create a lambda method that accepts a boolean condition statement and 49 | * uses the test interface to search for specific algorithm 50 | * @param condition 51 | * @return 52 | */ 53 | public List getOperator(SortOperatorEquals condition) { 54 | List getSortOperator = new ArrayList<>(); 55 | operators.stream().filter((b) -> (condition.test(b))).forEachOrdered((b) -> { 56 | getSortOperator.add(b); 57 | }); 58 | return getSortOperator; 59 | } 60 | 61 | public interface SortOperatorEquals { 62 | boolean test(AbstractSort a); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/sortingalgoritms/sorts/CSelectionSort.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This program is free software: you can redistribute it and/or modify 3 | * it under the terms of the GNU General Public License as published by 4 | * the Free Software Foundation, either version 3 of the License, or 5 | * any later version. 6 | * 7 | * This program is distributed in the hope that it will be useful, 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | * GNU General Public License for more details. 11 | * 12 | * You should have received a copy of the GNU General Public License 13 | * along with this program. If not, see . 14 | */ 15 | package sortingalgoritms.sorts; 16 | 17 | import sortingalgoritms.util.IComparable; 18 | 19 | /** 20 | * Implementation of the selection sort algorithm. 21 | * 22 | * @author Eric Canull 23 | */ 24 | public final class CSelectionSort extends AbstractSort { 25 | 26 | public static final CSelectionSort SINGLETON = new CSelectionSort(); 27 | 28 | /** Implementation of the selection sort algorithm */ 29 | private CSelectionSort() { } 30 | 31 | /** 32 | * Starts of the selection sort algorithm. 33 | * 34 | * @param numbers an array of numbers used for the sorting 35 | * @param lowIndex the lowest index position in the array 36 | * @param highIndex the highest index position in the array 37 | */ 38 | @Override 39 | public void startSort(IComparable[] numbers, int lowIndex, int highIndex) { 40 | IComparable minValue; 41 | 42 | int index, minIndex; 43 | for (lowIndex = 0; lowIndex < numbers.length; lowIndex++) { 44 | minValue = numbers[lowIndex]; 45 | minIndex = lowIndex; 46 | count(); 47 | for (index = lowIndex; index < numbers.length; index++) { 48 | if (numbers[index].compare(minValue) == IComparable.LESS) { 49 | minValue = numbers[index]; 50 | minIndex = index; 51 | count(); 52 | 53 | } 54 | count(); 55 | } 56 | 57 | if (minValue.compare(numbers[lowIndex]) == IComparable.LESS) { 58 | minValue = numbers[lowIndex]; 59 | numbers[lowIndex] = numbers[minIndex]; 60 | numbers[minIndex] = minValue; 61 | count(); 62 | } 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/sortingalgoritms/sorts/CPancakeSort.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This program is free software: you can redistribute it and/or modify 3 | * it under the terms of the GNU General Public License as published by 4 | * the Free Software Foundation, either version 3 of the License, or 5 | * any later version. 6 | * 7 | * This program is distributed in the hope that it will be useful, 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | * GNU General Public License for more details. 11 | * 12 | * You should have received a copy of the GNU General Public License 13 | * along with this program. If not, see . 14 | */ 15 | package sortingalgoritms.sorts; 16 | 17 | import sortingalgoritms.util.IComparable; 18 | 19 | /** 20 | * Implementation of the pancake sort algorithm. 21 | * 22 | * @author Eric Canull 23 | */ 24 | public final class CPancakeSort extends AbstractSort { 25 | 26 | public static final CPancakeSort SINGLETON = new CPancakeSort(); 27 | 28 | /** Implementation of the pancake sort algorithm. */ 29 | private CPancakeSort() { } 30 | 31 | /** 32 | * Starts the pancake sort algorithm. 33 | * 34 | * @param numbers an array of numbers used for the sorting 35 | * @param lowIndex a integer representing the lowest index position in the 36 | * array 37 | * @param highIndex a integer representing the highest index position in the 38 | * array 39 | */ 40 | @Override 41 | protected void startSort(IComparable[] numbers, int lowIndex, int highIndex) { 42 | IComparable max; 43 | 44 | for (int index = 0; index < numbers.length; index++) { 45 | max = numbers[0]; 46 | lowIndex = 0; 47 | count(); 48 | for (int j = 0; j < numbers.length - index; j++) { 49 | count(); 50 | if (numbers[j].compare(max) == IComparable.GREATER) { 51 | max = numbers[j]; 52 | lowIndex = j; 53 | count(); 54 | } 55 | } 56 | 57 | flip(numbers, lowIndex, numbers.length - 1 - index); 58 | count(); 59 | } 60 | } 61 | 62 | /** 63 | * Flips the array like a pancake. 64 | * 65 | * @param numbers an array of numbers used for the sorting 66 | * @param left a integer representing the left index position in the array 67 | * @param right a integer representing the right index position in the array 68 | */ 69 | public void flip(IComparable[] numbers, int left, int right) { 70 | 71 | while (left <= right) { 72 | IComparable temp = numbers[left]; 73 | numbers[left] = numbers[right]; 74 | numbers[right] = temp; 75 | left++; 76 | right--; 77 | count(); 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/sortingalgoritms/sorts/CCocktailSort.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This program is free software: you can redistribute it and/or modify 3 | * it under the terms of the GNU General Public License as published by 4 | * the Free Software Foundation, either version 3 of the License, or 5 | * any later version. 6 | * 7 | * This program is distributed in the hope that it will be useful, 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | * GNU General Public License for more details. 11 | * 12 | * You should have received a copy of the GNU General Public License 13 | * along with this program. If not, see . 14 | */ 15 | package sortingalgoritms.sorts; 16 | 17 | import sortingalgoritms.util.IComparable; 18 | 19 | /** 20 | * Implementation of the cocktail or shaker sort algorithm. 21 | * 22 | * @author Eric Canull 23 | * @version 1.0 24 | */ 25 | public final class CCocktailSort extends AbstractSort { 26 | 27 | public static final CCocktailSort SINGLETON = new CCocktailSort(); 28 | 29 | /** Implementation of the cocktail or shaker sort algorithm. */ 30 | private CCocktailSort() { } 31 | 32 | /** 33 | * Starts the Cocktail Sorting algorithm. 34 | * 35 | * @param numbers an array of numbers used for the sorting 36 | * @param lowIndex a integer representing the lowest index position in the array 37 | * @param highIndex a integer representing the highest index position in the array 38 | */ 39 | @Override 40 | protected void startSort(IComparable[] numbers, int lowIndex, int highIndex) { 41 | boolean swapped; 42 | do { 43 | swapped = false; 44 | for (int i = 0; i <= numbers.length - 2; i++) { 45 | count(); 46 | if (numbers[i].compare(numbers[i + 1]) == IComparable.GREATER) { 47 | // test whether the two elements are in the wrong order 48 | IComparable temp = numbers[i]; 49 | numbers[i] = numbers[i + 1]; 50 | numbers[i + 1] = temp; 51 | swapped = true; 52 | count(); 53 | } 54 | } 55 | 56 | if (!swapped) { 57 | // exit the outer loop here if no swaps occurred. 58 | break; 59 | } 60 | swapped = false; 61 | for (int i = numbers.length - 2; i >= 0; i--) { 62 | count(); 63 | if (numbers[i].compare(numbers[i + 1]) == IComparable.GREATER) { 64 | IComparable temp = numbers[i]; 65 | numbers[i] = numbers[i + 1]; 66 | numbers[i + 1] = temp; 67 | swapped = true; 68 | count(); 69 | } 70 | } 71 | // If no elements have been swapped, then the list is sorted 72 | } while (swapped); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/sortingalgoritms/sorts/CBubbleSort.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This program is free software: you can redistribute it and/or modify 3 | * it under the terms of the GNU General Public License as published by 4 | * the Free Software Foundation, either version 3 of the License, or 5 | * any later version. 6 | * 7 | * This program is distributed in the hope that it will be useful, 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | * GNU General Public License for more details. 11 | * 12 | * You should have received a copy of the GNU General Public License 13 | * along with this program. If not, see . 14 | */ 15 | package sortingalgoritms.sorts; 16 | 17 | import sortingalgoritms.util.IComparable; 18 | 19 | /** 20 | * Implementation of the bubble sort algorithm. 21 | * @author Eric Canull 22 | * @version 1.0 23 | */ 24 | public final class CBubbleSort extends AbstractSort { 25 | private CBubbleSort() { } /* non-use private constructor */ 26 | 27 | public static final CBubbleSort SINGLETON = new CBubbleSort(); 28 | 29 | /** 30 | * Starts the Bubble Sort algorithm. 31 | * @param numbers an array of numbers used for the sorting 32 | * @param lowIndex a integer representing the lowest index position in the array 33 | * @param highIndex a integer representing the highest index position in the array 34 | */ 35 | @Override 36 | protected void startSort(IComparable[] numbers, int lowIndex, int highIndex) { 37 | 38 | // Marker for the final swap's position 39 | int lastSwap = numbers.length - 1; 40 | 41 | // Start at position index 1 and iterate through the length of the array 42 | for (int i = 1; i < numbers.length; i++) { 43 | boolean isSorted = true; // Condition to stop the iteration 44 | int currentSwap = -1; // Keep track of the swaps 45 | count(); 46 | 47 | // Starting at position index 0 and iterate until the last swap position 48 | for (int startIndex = 0; startIndex < lastSwap; startIndex++) { 49 | count(); 50 | // If the start index is greater than the index it proceeds then swap the values 51 | if (numbers[startIndex].compare(numbers[startIndex + 1]) == IComparable.GREATER) { 52 | IComparable temp = numbers[startIndex]; 53 | numbers[startIndex] = numbers[startIndex + 1]; 54 | numbers[startIndex + 1] = temp; 55 | isSorted = false; 56 | currentSwap = startIndex; 57 | count(); 58 | } 59 | } 60 | 61 | // Exits the loop if the sorting is complete 62 | if (isSorted) 63 | return; 64 | 65 | // Decrements lastSwap position until sorting has completed 66 | lastSwap = currentSwap; // -1 67 | count(); 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/main/resources/styles/graphicspane.css: -------------------------------------------------------------------------------- 1 | .root { 2 | -fx-base: #31363b; 3 | -fx-accent: #3073b4; 4 | -fx-background: #31363b; 5 | -fx-focus-color: #3073b4; 6 | -fx-control-inner-background: #232629; 7 | -fx-inner-border: linear-gradient(to bottom, 8 | derive(-fx-color,30%) 0%, 9 | derive(-fx-color,-5%) 100%); 10 | -fx-body-color: linear-gradient(to bottom, 11 | derive(-fx-color, -0%) 0%, 12 | derive(-fx-color, -2%) 20%, 13 | derive(-fx-color, -5%) 60%, 14 | derive(-fx-color, -8%) 100%); 15 | } 16 | #Content { 17 | 18 | } 19 | 20 | .hyperlink { 21 | -fx-fill: -fx-text-background-color; 22 | } 23 | 24 | .log-text-area { 25 | -fx-font-family: FiraCode-Regular; 26 | -fx-font-size: 13px; 27 | -fx-text-fill: #BDBDBD; 28 | -fx-background-insets: -1; 29 | } 30 | 31 | .log-text-area .content { 32 | -fx-background-color: #192022; 33 | } 34 | .spinner .text-field:readonly { 35 | -fx-background-color: #192022; 36 | } 37 | 38 | .toggle-button:selected { 39 | -fx-text-fill: -fx-text-base-color; 40 | } 41 | 42 | .sort-label { 43 | -fx-font-family: OpenSans-Regular; 44 | -fx-font-size: 14px; 45 | } 46 | 47 | .control-label { 48 | -fx-font-family: OpenSans-Regular; 49 | -fx-font-size: 12px; 50 | } 51 | 52 | .status-label { 53 | -fx-font-family: OpenSans-Regular; 54 | -fx-font-size: 11px; 55 | } 56 | 57 | .title-bar { 58 | -fx-border-width: 0 0 2 0; 59 | -fx-background-color: #2d3136; 60 | -fx-border-color: #686971; 61 | } 62 | .rect { 63 | -fx-background-color: #3073b4; 64 | -fx-border-radius: 3 3 1 1; 65 | -fx-background-radius: 3 3 1 1; 66 | } 67 | 68 | .controls-grid-pane { 69 | -fx-background-color: #31363b; 70 | -fx-padding: 6px; 71 | -fx-background-radius: 3, 3, 2, 1; 72 | } 73 | 74 | .number-field { 75 | -fx-border-color: #3073b4; 76 | -fx-background-color: #3073b433; 77 | } 78 | 79 | .split-pane { 80 | -fx-background-color: #31363b; 81 | } 82 | 83 | .split-pane:focused { 84 | -fx-background-color: transparent; 85 | } 86 | 87 | .choice-box .label { /* Workaround for RT-20015 */ 88 | -fx-text-fill: -fx-text-base-color; 89 | } 90 | 91 | .label, 92 | .check-box, 93 | .text, 94 | .radio-button { 95 | -fx-fill: -fx-text-background-color; 96 | -fx-text-fill: -fx-text-background-color; 97 | } 98 | 99 | .control-button { 100 | -fx-background-color: linear-gradient(from 48.0% 100.0% to 50.0% 43.0%, #960505 0.0%, #C21A4D 96.0%, #C21A4D 100.0%); 101 | } 102 | 103 | .button, .toggle-button, .check-box .box, .radio-button .radio, .choice-box, .menu-button, .tab, .combo-box-base { 104 | /* -fx-background-insets: 0 0 0 0, 0, 1, 1;*/ 105 | } 106 | 107 | .choice-box .label { 108 | /* Workaround for RT-20015 */ 109 | -fx-text-fill: -fx-text-base-color; 110 | } 111 | 112 | .menu-button .label { 113 | /* Workaround for RT-20015 */ 114 | -fx-text-fill: -fx-text-base-color; 115 | } 116 | 117 | .footer { 118 | -fx-border-width: 1.5 0 0 0; 119 | -fx-border-color: #464d54; 120 | 121 | } -------------------------------------------------------------------------------- /src/main/java/sortingalgoritms/sorts/CShellSort.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This program is free software: you can redistribute it and/or modify 3 | * it under the terms of the GNU General Public License as published by 4 | * the Free Software Foundation, either version 3 of the License, or 5 | * any later version. 6 | * 7 | * This program is distributed in the hope that it will be useful, 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | * GNU General Public License for more details. 11 | * 12 | * You should have received a copy of the GNU General Public License 13 | * along with this program. If not, see . 14 | */ 15 | package sortingalgoritms.sorts; 16 | 17 | import sortingalgoritms.util.IComparable; 18 | 19 | /** 20 | * Implementation of the shell sort algorithm. 21 | * 22 | * @author Eric Canull 23 | */ 24 | public final class CShellSort extends AbstractSort { 25 | 26 | public static final CShellSort SINGLETON = new CShellSort(); 27 | 28 | /** Implementation of the shell sort algorithm. */ 29 | private CShellSort() { } 30 | 31 | /** 32 | * Starts the shell sort algorithm. 33 | * 34 | * @param numbers an array of numbers used for the sorting 35 | * @param lowIndex the lowest index position in the array 36 | * @param highIndex the highest index position in the array 37 | */ 38 | @Override 39 | protected void startSort(IComparable[] numbers, int lowIndex, int highIndex) { 40 | int inner, outer; 41 | IComparable temp; 42 | 43 | highIndex = 1; 44 | while (highIndex <= numbers.length / 3) { 45 | highIndex = highIndex * 3 + 1; 46 | } 47 | 48 | while (highIndex > 0) { 49 | for (outer = highIndex; outer < numbers.length; outer++) { 50 | temp = numbers[outer]; 51 | inner = outer; 52 | 53 | while (inner > highIndex - 1 && numbers[inner - highIndex].compare(temp) >= IComparable.GREATER) { 54 | numbers[inner] = numbers[inner - highIndex]; 55 | inner -= highIndex; 56 | 57 | // count outer loop swaps 58 | count(); 59 | } 60 | 61 | numbers[inner] = temp; 62 | 63 | } 64 | // count outer loop swaps 65 | count(); 66 | highIndex = (highIndex - 1) / 3; 67 | } 68 | } 69 | 70 | // protected void startAlgorithm(IComparable[] numbers, int lowIndex, int highIndex) { 71 | // // alternative for analysis manages to have the best time 72 | // 73 | // int hi; 74 | // int lo = 0; 75 | // int leftSide = numbers.length - 1; 76 | // for (hi = 1; hi <= (leftSide - lo) / 9; hi = 3 * hi + 1); 77 | // for (; hi > 0; hi /= 3) { 78 | // for (int i = lo + hi; i <= leftSide; i++) { 79 | // int j = i; 80 | // IComparable x = numbers[i]; 81 | // while ((j >= lo + hi) && (x.compare(numbers[j - hi]) == IComparable.LESS)) { 82 | // numbers[j] = numbers[j - hi]; 83 | // j -= hi; 84 | // 85 | // // count every inner loop swap 86 | // count(); 87 | // } 88 | // numbers[j] = x; 89 | // 90 | // // count outer loop swaps 91 | // count(); 92 | // } 93 | // } 94 | // } 95 | } 96 | -------------------------------------------------------------------------------- /src/main/java/sortingalgoritms/sorts/CHeapSort.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This program is free software: you can redistribute it and/or modify 3 | * it under the terms of the GNU General Public License as published by 4 | * the Free Software Foundation, either version 3 of the License, or 5 | * any later version. 6 | * 7 | * This program is distributed in the hope that it will be useful, 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | * GNU General Public License for more details. 11 | * 12 | * You should have received a copy of the GNU General Public License 13 | * along with this program. If not, see . 14 | */ 15 | package sortingalgoritms.sorts; 16 | 17 | import sortingalgoritms.util.IComparable; 18 | 19 | /** 20 | * Implementation of the Heap Sort algorithm. 21 | * 22 | * @author Eric Canull 23 | */ 24 | public final class CHeapSort extends AbstractSort { 25 | 26 | public static final CHeapSort SINGLETON = new CHeapSort(); 27 | 28 | private IComparable temp[]; 29 | private int number; 30 | private int left; 31 | private int right; 32 | private int largest; 33 | 34 | /** Implementation of the Heap Sort algorithm. */ 35 | private CHeapSort() { } 36 | 37 | /** 38 | * Starts the Heap Sort algorithm. 39 | * 40 | * @param numbers an array of numbers used for the sorting 41 | * @param lowIndex lowest index position in the array 42 | * @param highIndex highest index position in the array 43 | */ 44 | @Override 45 | protected void startSort(IComparable[] numbers, int lowIndex, int highIndex) { 46 | temp = numbers; 47 | buildheap(temp); 48 | for (int i = number; i > 0; i--) { 49 | count(); 50 | exchange(0, i); 51 | number = number - 1; 52 | maxheap(temp, 0); 53 | } 54 | } 55 | 56 | /** 57 | * @param numbers an array of numbers used for the sorting 58 | */ 59 | private void buildheap(IComparable[] numbers) { 60 | number = numbers.length - 1; 61 | for (int i = number / 2; i >= 0; i--) { 62 | maxheap(numbers, i); 63 | } 64 | } 65 | 66 | /** 67 | * 68 | * @param numbers an array of numbers used for the sorting 69 | * @param index the lowest index position in the array 70 | */ 71 | private void maxheap(IComparable[] numbers, int index) { 72 | left = 2 * index; 73 | right = 2 * index + 1; 74 | 75 | if (left <= number && numbers[left].compare(numbers[index]) == IComparable.GREATER) { 76 | largest = left; 77 | count(); 78 | } else { 79 | largest = index; 80 | count(); 81 | } 82 | 83 | if (right <= number && numbers[right].compare(numbers[largest]) == IComparable.GREATER) { 84 | largest = right; 85 | } 86 | 87 | if (largest != index) { 88 | exchange(index, largest); 89 | maxheap(numbers, largest); 90 | count(); 91 | } 92 | } 93 | 94 | /** 95 | * Exchange the numbers in the arrays 96 | * 97 | * @param i a integer representing the lowest index position in the array 98 | * @param j a integer representing the lowest index position in the array 99 | */ 100 | private void exchange(int i, int j) { 101 | IComparable arrayExchange = temp[i]; 102 | temp[i] = temp[j]; 103 | temp[j] = arrayExchange; 104 | count(); 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /src/main/resources/fxml/FXMLAnimationPane.fxml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /src/main/java/sortingalgoritms/util/CompareValue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This program is free software: you can redistribute it and/or modify 3 | * it under the terms of the GNU General Public License as published by 4 | * the Free Software Foundation, either version 3 of the License, or 5 | * any later version. 6 | * 7 | * This program is distributed in the hope that it will be useful, 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | * GNU General Public License for more details. 11 | * 12 | * You should have received a copy of the GNU General Public License 13 | * along with this program. If not, see . 14 | */ 15 | package sortingalgoritms.util; 16 | 17 | import java.util.HashMap; 18 | import java.util.Map; 19 | import javafx.scene.paint.Color; 20 | import sortingalgoritms.MainController; 21 | 22 | /** 23 | * Creates comparable value with changing colors. 24 | * 25 | * @author Eric Canull 26 | */ 27 | public final class CompareValue implements IComparable { 28 | 29 | private int value; 30 | private Color color; 31 | 32 | int i; 33 | 34 | /** 35 | * Creates comparable value with changing colors. 36 | * 37 | * @param value an integer reference to a number 38 | */ 39 | public CompareValue(int value) { 40 | this.value = value; 41 | this.color = NORMAL_COLOR; 42 | } 43 | 44 | /** 45 | * Compares two values, changes their colors and returns -1, 0, or 1. 46 | * 47 | * @param comparable 48 | * @return An object with a value less than, greater than, or equals 49 | */ 50 | @Override 51 | public int compare(IComparable comparable) { 52 | CompareValue compareValue = (CompareValue) comparable; 53 | 54 | compareValue.color = SELECTED_COLOR; 55 | color = SELECTED_COLOR; 56 | try { 57 | Thread.sleep(MainController.DELAY_PROPERTY.get()/2); 58 | } catch (InterruptedException e) { 59 | System.out.println(e.getMessage()); 60 | } 61 | if (value < compareValue.value) { 62 | // compareValue.color = SELECTED_COLOR; 63 | // color = SELECTED_COLOR; 64 | i = IComparable.LESS; 65 | } else if (value > compareValue.value) { 66 | // compareValue.color = SELECTED_COLOR; 67 | // color = SELECTED_COLOR; 68 | i = IComparable.GREATER; 69 | } else { 70 | i = IComparable.EQUAL; 71 | } 72 | try { 73 | Thread.sleep(MainController.DELAY_PROPERTY.get()/2); 74 | } catch (InterruptedException e) { 75 | System.out.println(e.getMessage()); 76 | } 77 | compareValue.color = NORMAL_COLOR; 78 | color = NORMAL_COLOR; 79 | return i; 80 | } 81 | 82 | /** 83 | * Gets the value. 84 | * 85 | * @return An integer value specifying the current number stored 86 | */ 87 | public int getValue() { 88 | return value; 89 | } 90 | 91 | /** 92 | * Sets the value. 93 | * 94 | * @param value a reference to the new integer value to be set 95 | */ 96 | public void setValue(int value) { 97 | this.value = value; 98 | } 99 | 100 | /** 101 | * Sets the color 102 | * @param color 103 | */ 104 | @Override 105 | public void setColor(Color color) { 106 | this.color = color; 107 | } 108 | 109 | /** 110 | * Gets the color 111 | * @return 112 | */ 113 | @Override 114 | public Color getColor() { 115 | return this.color; 116 | } 117 | 118 | /** 119 | * Gets the value as a string 120 | * @return 121 | */ 122 | @Override 123 | public String toString() { 124 | return String.valueOf(value); 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /src/main/java/sortingalgoritms/sorts/CQuickSort.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This program is free software: you can redistribute it and/or modify 3 | * it under the terms of the GNU General Public License as published by 4 | * the Free Software Foundation, either version 3 of the License, or 5 | * any later version. 6 | * 7 | * This program is distributed in the hope that it will be useful, 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | * GNU General Public License for more details. 11 | * 12 | * You should have received a copy of the GNU General Public License 13 | * along with this program. If not, see . 14 | */ 15 | package sortingalgoritms.sorts; 16 | 17 | import sortingalgoritms.util.IComparable; 18 | 19 | /** 20 | * Implementation of the quick sort algorithm 21 | * 22 | * @author Eric Canull 23 | */ 24 | public final class CQuickSort extends AbstractSort { 25 | 26 | public static final CQuickSort SINGLETON = new CQuickSort(); 27 | 28 | /** Implementation of the quick sort algorithm */ 29 | private CQuickSort() { } 30 | 31 | /** 32 | * Starts the quick sort algorithm. 33 | * 34 | * @param numbers an array of numbers used for the sorting 35 | * @param lowIndex a integer representing the lowest index position in the 36 | * array 37 | * @param highIndex a integer representing the highest index position in the 38 | * array 39 | */ 40 | @Override 41 | protected void startSort(IComparable[] numbers, int lowIndex, int highIndex) { 42 | qsort(numbers, 0, numbers.length - 1); 43 | } 44 | 45 | /** 46 | * Recursive method to partition the array. 47 | * 48 | * @param numbers an array of numbers used for the sorting 49 | * @param lowIndex a integer representing the lowest index position in the 50 | * array 51 | * @param highIndex a integer representing the highest index position in the 52 | * array 53 | */ 54 | private void qsort(IComparable[] numbers, int lowIndex, int highIndex) { 55 | if (highIndex <= lowIndex) { 56 | return; 57 | } 58 | count(); 59 | int index = partition(numbers, lowIndex, highIndex); 60 | qsort(numbers, lowIndex, index - 1); 61 | qsort(numbers, index + 1, highIndex); 62 | } 63 | 64 | /** 65 | * Partitions the array. 66 | * 67 | * @param numbers an array of numbers used for the sorting 68 | * @param lowIndex a integer representing the lowest index position in the 69 | * array 70 | * @param highIndex a integer representing the highest index position in the 71 | * array 72 | */ 73 | private int partition(IComparable[] numbers, int lowIndex, int highIndex) { 74 | IComparable tmp; 75 | int low = lowIndex - 1; 76 | int high = highIndex; 77 | IComparable pivot = numbers[highIndex]; // partition point 78 | while (true) { 79 | 80 | // scan up to find first item greater than partition 81 | // won't go past end because partition = last item in array 82 | while (numbers[++low].compare(pivot) == IComparable.LESS) { 83 | count(); 84 | } 85 | 86 | // scan down down to find first item less in partion 87 | // or quit if none 88 | while (pivot.compare(numbers[--high]) == IComparable.LESS) { 89 | count(); 90 | if (high == lowIndex) { 91 | break; 92 | } 93 | } 94 | // if scan points cross, quit 95 | if (low >= high) { 96 | break; 97 | } 98 | count(); 99 | // exchange the elements 100 | tmp = numbers[low]; 101 | numbers[low] = numbers[high]; 102 | numbers[high] = tmp; 103 | } 104 | 105 | // final swap 106 | numbers[highIndex] = numbers[low]; 107 | numbers[low] = pivot; 108 | 109 | count(); 110 | return low; 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /src/main/java/sortingalgoritms/sorts/CMergeSort.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This program is free software: you can redistribute it and/or modify 3 | * it under the terms of the GNU General Public License as published by 4 | * the Free Software Foundation, either version 3 of the License, or 5 | * any later version. 6 | * 7 | * This program is distributed in the hope that it will be useful, 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | * GNU General Public License for more details. 11 | * 12 | * You should have received a copy of the GNU General Public License 13 | * along with this program. If not, see . 14 | */ 15 | package sortingalgoritms.sorts; 16 | 17 | import sortingalgoritms.util.IComparable; 18 | 19 | /** 20 | * Implementation of the merge sort algorithm. 21 | * 22 | * @author Eric Canull 23 | */ 24 | public final class CMergeSort extends AbstractSort { 25 | 26 | public static final CMergeSort SINGLETON = new CMergeSort(); 27 | 28 | /** Implementation of the merge sort algorithm. */ 29 | private CMergeSort() { } 30 | 31 | /** 32 | * Starts the merge sort algorithm. 33 | * 34 | * @param numbers an array of numbers used for the sorting 35 | * @param lowIndex the lowest index position in the array 36 | * @param highIndex the highest index position in the array 37 | */ 38 | @Override 39 | protected void startSort(IComparable[] numbers, int lowIndex, int highIndex) { 40 | mergeSort(numbers); 41 | } 42 | 43 | /** 44 | * Create a temporary array to store the divisions. 45 | * 46 | * @param numbers an array of numbers used for the sorting 47 | */ 48 | private void mergeSort(IComparable[] numbers) { 49 | IComparable[] temp = new IComparable[numbers.length]; 50 | mergeSort(numbers, temp, 0, numbers.length - 1); 51 | } 52 | 53 | /** 54 | * Recursively divides the arrays in half and merges them. 55 | * 56 | * @param numbers an array of numbers used for the sorting 57 | * @param temp an array of numbers used for the sorting 58 | * @param left a integer representing the left index position in the array 59 | * @param right a integer representing the right index position in the array 60 | */ 61 | private void mergeSort(IComparable[] numbers, IComparable[] temp, int left, int right) { 62 | if (left < right) { 63 | int center = (left + right) / 2; 64 | mergeSort(numbers, temp, left, center); 65 | mergeSort(numbers, temp, center + 1, right); 66 | merge(numbers, temp, left, center + 1, right); 67 | count(); 68 | } 69 | } 70 | 71 | /** 72 | * Sorts the divided arrays and copies the temporary array back into the 73 | * original array. 74 | * 75 | * @param numbers an array of numbers used for the sorting 76 | * @param temp an array of numbers used for the sorting 77 | * @param left a integer representing the left index position in the array 78 | * @param right a integer representing the right index position in the array 79 | * @param rightEnd a integer representing the rightEnd position in the array 80 | */ 81 | private void merge(IComparable[] numbers, IComparable[] temp, int left, int right, int rightEnd) { 82 | int leftEnd = right - 1; 83 | int index = left; 84 | int num = rightEnd - left + 1; 85 | count(); 86 | 87 | while (left <= leftEnd && right <= rightEnd) { 88 | 89 | if (numbers[left].compare(numbers[right]) == IComparable.LESS) { 90 | temp[index++] = numbers[left++]; 91 | count(); 92 | } else { 93 | temp[index++] = numbers[right++]; 94 | count(); 95 | } 96 | } 97 | 98 | // Copy rest of first half 99 | while (left <= leftEnd) { 100 | temp[index++] = numbers[left++]; 101 | count(); 102 | } 103 | 104 | // Copy rest of right half 105 | while (right <= rightEnd) { 106 | temp[index++] = numbers[right++]; 107 | count(); 108 | } 109 | 110 | // Copy the temp array back into the original array 111 | for (int i = 0; i < num; i++, rightEnd--) { 112 | numbers[rightEnd] = temp[rightEnd]; 113 | } 114 | count(); 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | com.sortingalgoritms 6 | fxsortinganimation 7 | 1.0-SNAPSHOT 8 | jar 9 | 10 | fxsortinganimation 11 | 12 | 13 | UTF-8 14 | sortingalgoritms.MainApp 15 | 16 | 17 | 18 | 19 | Your Organisation 20 | 21 | 22 | 23 | 24 | 25 | org.apache.maven.plugins 26 | maven-dependency-plugin 27 | 2.6 28 | 29 | 30 | unpack-dependencies 31 | package 32 | 33 | unpack-dependencies 34 | 35 | 36 | system 37 | junit,org.mockito,org.hamcrest 38 | ${project.build.directory}/classes 39 | 40 | 41 | 42 | 43 | 44 | org.codehaus.mojo 45 | exec-maven-plugin 46 | 1.2.1 47 | 48 | 49 | unpack-dependencies 50 | 51 | package 52 | 53 | exec 54 | 55 | 56 | ${java.home}/../bin/javafxpackager 57 | 58 | -createjar 59 | -nocss2bin 60 | -appclass 61 | ${mainClass} 62 | -srcdir 63 | ${project.build.directory}/classes 64 | -outdir 65 | ${project.build.directory} 66 | -outfile 67 | ${project.build.finalName}.jar 68 | 69 | 70 | 71 | 72 | default-cli 73 | 74 | exec 75 | 76 | 77 | ${java.home}/bin/java 78 | ${runfx.args} 79 | 80 | 81 | 82 | 83 | 84 | org.apache.maven.plugins 85 | maven-compiler-plugin 86 | 3.1 87 | 88 | 1.8 89 | 1.8 90 | 91 | ${sun.boot.class.path}${path.separator}${java.home}/lib/jfxrt.jar 92 | 93 | 94 | 95 | 96 | org.apache.maven.plugins 97 | maven-surefire-plugin 98 | 2.16 99 | 100 | 101 | ${java.home}/lib/jfxrt.jar 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | -------------------------------------------------------------------------------- /src/main/java/sortingalgoritms/util/RandomValues.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This program is free software: you can redistribute it and/or modify 3 | * it under the terms of the GNU General Public License as published by 4 | * the Free Software Foundation, either version 3 of the License, or 5 | * any later version. 6 | * 7 | * This program is distributed in the hope that it will be useful, 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | * GNU General Public License for more details. 11 | * 12 | * You should have received a copy of the GNU General Public License 13 | * along with this program. If not, see . 14 | */ 15 | package sortingalgoritms.util; 16 | 17 | import java.util.Arrays; 18 | import java.util.Random; 19 | import java.util.stream.IntStream; 20 | 21 | /** 22 | * Creates an array of ten values in random, reversed or ascending 23 | * order based on the selected number set. 24 | * 25 | * @author Eric Canull 26 | */ 27 | public class RandomValues { 28 | 29 | /** max array size */ 30 | public static final int MAX_SIZE = 10; 31 | 32 | private static CompareValue[] array = null; 33 | 34 | /** 35 | * Gets the array. 36 | * @return an array of values to be sorted 37 | */ 38 | public static CompareValue[] getArray() { 39 | return array; 40 | } 41 | 42 | private static int maxValue; // array max value 43 | 44 | /** 45 | * Gets the max value in the array. 46 | * @return an array of values to be sorted 47 | */ 48 | public static int getMaxValue() { 49 | return maxValue; 50 | } 51 | 52 | /** 53 | * Sets the values in the array 54 | * @param type a String representing the name of the data type 55 | * @param values 56 | */ 57 | public static void setRandomSet(String type, int[] values) { 58 | resetArray(); 59 | 60 | if (values == null) { 61 | switch (type) { 62 | case "Random" : array = randomValues(); break; 63 | case "Ordered" : array = inorderValues(); break; 64 | case "Reverse" : array = reverseValues(); break; 65 | case "Hundreds" : array = randomHundreds(); break; 66 | case "Thousands": array = randomThousands(); break; 67 | } 68 | } else { 69 | setManualSet(values); 70 | } 71 | 72 | setMaxValue(); 73 | } 74 | 75 | /** Sets the array with values manually entered by the user. */ 76 | private static void setManualSet(int[] values) { 77 | array = new CompareValue[MAX_SIZE]; 78 | IntStream.range(0, array.length).forEachOrdered(index -> { 79 | CompareValue bar = new CompareValue(values[index]); 80 | array[index] = bar; 81 | }); 82 | } 83 | 84 | /** Resets the array. */ 85 | public static void resetArray() { 86 | array = new CompareValue[MAX_SIZE]; 87 | IntStream.range(0, array.length).forEachOrdered(index -> { 88 | array[index] = new CompareValue(index + 1); 89 | }); 90 | } 91 | 92 | /** Determines the highest value in the array. */ 93 | public static void setMaxValue() { 94 | int max = 0; 95 | for (CompareValue value : array) { 96 | max = value.getValue() > max ? value.getValue() : max; 97 | } 98 | RandomValues.maxValue = max; 99 | } 100 | 101 | /** 102 | * Randomly distributes values 1-10 without duplicates 103 | * @return an array of random values 104 | */ 105 | private static CompareValue[] randomValues() { 106 | for (int index = 0; index < array.length - 1; index++) { 107 | int randomIndex = (int) (Math.random() * (array.length - index)) + index; 108 | int tempArray = array[index].getValue(); 109 | array[index].setValue(array[randomIndex].getValue()); 110 | array[randomIndex].setValue(tempArray); 111 | } 112 | return array; 113 | } 114 | 115 | /** 116 | * Gets the array with values 1-10 in ascending order without duplicates 117 | * @return An array of values in-order 118 | */ 119 | private static CompareValue[] inorderValues() { 120 | return array; 121 | } 122 | 123 | /** 124 | * Gets an array with values 1-10 in reverse order without duplicates 125 | * @return An array of values in reverse order 126 | */ 127 | private static CompareValue[] reverseValues() { 128 | int lastIndex = array.length; 129 | for (CompareValue value : array) { 130 | value.setValue(lastIndex); 131 | lastIndex--; 132 | } 133 | return array; 134 | } 135 | 136 | /** 137 | * Gets an array with random values between 1-10,000. 138 | * @return an array of random values 139 | */ 140 | private static CompareValue[] randomHundreds() { 141 | for (CompareValue value : array) { 142 | int randomInt = new Random().nextInt(1000) + 100; 143 | value.setValue(randomInt); 144 | } 145 | return array; 146 | } 147 | 148 | /** 149 | * Gets an array with random array between 1-999,000. 150 | * @return an array of random values 151 | */ 152 | private static CompareValue[] randomThousands() { 153 | for (CompareValue value : array) { 154 | int randomInt = new Random().nextInt(999000) + 1; 155 | value.setValue(randomInt); 156 | } 157 | return array; 158 | } 159 | 160 | /** 161 | * The values in the array as a string. 162 | * @return a String of the values stored in the array 163 | */ 164 | public static String getString() { 165 | return Arrays.asList(array).toString() 166 | .replace("[", "") 167 | .replace("]", ""); 168 | } 169 | } 170 | -------------------------------------------------------------------------------- /src/main/resources/fxml/FXMLMainPane.fxml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 48 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 74 | 75 | 83 | 84 | 92 | 93 |