27 |
28 | ### Preview
29 | 
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 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
139 |
140 |
141 |
142 |
143 |
144 |
--------------------------------------------------------------------------------
/src/main/java/sortingalgoritms/ui/AnimationController.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.ui;
16 |
17 | import java.io.IOException;
18 | import java.util.logging.Level;
19 | import java.util.logging.Logger;
20 | import java.util.stream.IntStream;
21 | import javafx.animation.KeyFrame;
22 | import javafx.animation.KeyValue;
23 | import javafx.animation.Timeline;
24 | import javafx.beans.Observable;
25 | import javafx.beans.property.SimpleObjectProperty;
26 | import javafx.fxml.FXML;
27 | import javafx.fxml.FXMLLoader;
28 | import javafx.scene.control.TextField;
29 | import javafx.scene.layout.AnchorPane;
30 | import javafx.scene.layout.GridPane;
31 | import javafx.scene.layout.Region;
32 | import javafx.scene.paint.Color;
33 | import javafx.util.Duration;
34 | import sortingalgoritms.MainController;
35 |
36 | import sortingalgoritms.util.CompareValue;
37 | import sortingalgoritms.util.RandomValues;
38 | import sortingalgoritms.util.ISortOperator;
39 |
40 | /**
41 | * FXML Controller class
42 | *
43 | * @author Eric Canull
44 | */
45 | public class AnimationController extends AnchorPane implements ISortOperator {
46 |
47 | @FXML private GridPane barsGrid;
48 | @FXML private GridPane textFieldsGrid;
49 |
50 | public AnimationController() {
51 | initialize();
52 | }
53 |
54 | /**
55 | * Initializes the controller class.
56 | */
57 | private void initialize() {
58 | try {
59 | final FXMLLoader loader = new FXMLLoader();
60 | loader.setLocation(AnimationController.class.getResource("/fxml/FXMLAnimationPane.fxml")); //NOI18N
61 | loader.setController(this);
62 | loader.setRoot(this);
63 | loader.load();
64 | } catch (IOException ex) {
65 | Logger.getLogger(AnimationController.class.getName()).log(Level.SEVERE, null, ex);
66 | }
67 |
68 | barsGrid.widthProperty().addListener(evt -> onResize());
69 | barsGrid.heightProperty().addListener(evt -> onResize());
70 | }
71 |
72 | /**
73 | * Checks if the bars grid is done resizing.
74 | */
75 | private boolean isPaneLoaded() {
76 | // Compare actual width vs preferred width
77 | return barsGrid.getWidth() >= barsGrid.getPrefWidth()
78 | || barsGrid.getHeight() >= barsGrid.getPrefHeight();
79 | }
80 |
81 | /**
82 | * Sets the text field values.
83 | * @param presetChoice
84 | */
85 | public void setPresetValues(String presetChoice) {
86 | RandomValues.setRandomSet(presetChoice, null);
87 |
88 | IntStream.range(0, 10).forEachOrdered(index -> {
89 | TextField tf = (TextField) textFieldsGrid.getChildren().get(index);
90 | tf.setText(String.valueOf(RandomValues.getArray()[index].getValue()));
91 | });
92 | }
93 |
94 | /**
95 | * Adds the bars to the grid pane when resizing occurs.
96 | */
97 | private void onResize() {
98 | // If the array is still null or pane hasn't finished resizing
99 | if (RandomValues.getArray() == null || !isPaneLoaded()) {
100 | return; // let's bounce
101 | }
102 |
103 | // Only if the bars grid pane is empty
104 | if (barsGrid.getChildren().isEmpty()) {
105 | // Create new bars and add a style class
106 | IntStream.range(0, RandomValues.MAX_SIZE).forEachOrdered((int index) -> {
107 | CompareValue compareValue = RandomValues.getArray()[index];
108 | double height = calculateHeight(compareValue.getValue());
109 |
110 | Bar rect = new Bar();
111 | rect.getStyleClass().add("rect");
112 | rect.setPrefHeight(height);
113 | rect.setMaxHeight(height);
114 | barsGrid.add(rect, index, 0);
115 | });
116 | } else {
117 | // Just resize the height of the existing bars
118 | IntStream.range(0, RandomValues.MAX_SIZE).forEachOrdered((int index) -> {
119 | CompareValue compareValue = RandomValues.getArray()[index];
120 | double height = calculateHeight(compareValue.getValue());
121 |
122 | Bar bar = (Bar) barsGrid.getChildren().get(index);
123 | bar.setPrefHeight(height);
124 | bar.setMaxHeight(height);
125 |
126 | });
127 | }
128 | }
129 |
130 | /**
131 | * Resize the bars with animation.
132 | */
133 | public void onResizeAnimated() {
134 | if (isPaneLoaded()) {
135 | IntStream.range(0, RandomValues.MAX_SIZE).forEachOrdered((int index) -> {
136 | CompareValue compareValue = RandomValues.getArray()[index];
137 | double height = calculateHeight(compareValue.getValue());
138 |
139 | Bar bar = (Bar) barsGrid.getChildren().get(index);
140 | animate(bar, height, CompareValue.NORMAL_COLOR);
141 | });
142 | }
143 | }
144 |
145 | /**
146 | * Animates the resized bar height.
147 | * @param rect
148 | * @param height
149 | * @param color
150 | */
151 | private void animate(Bar rect, double height, Color color) {
152 | final Timeline tl = new Timeline();
153 | tl.getKeyFrames().addAll(
154 | new KeyFrame(Duration.millis(Math.max(MainController.DELAY_PROPERTY.get(), 20)),
155 | new KeyValue(rect.colorProperty, color),
156 | new KeyValue(rect.prefHeightProperty(), height),
157 | new KeyValue(rect.maxHeightProperty(), height)));
158 | tl.play();
159 | }
160 |
161 | /**
162 | * Use slope and y-intercept formulas to calculate the bars height
163 | * for resizing.
164 | */
165 | private double calculateHeight(double value) {
166 | double x1 = RandomValues.getMaxValue(),
167 | y2 = barsGrid.getHeight();
168 |
169 | // calculate the new height
170 | double height = y2 - ((-y2 / x1) * value + ((y2 * x1) / (x1)));
171 |
172 | return Math.round(height);
173 | }
174 |
175 | @Override
176 | public Object apply(Object object) {
177 | CompareValue[] objectArray = (CompareValue[]) object;
178 |
179 | for (int indexPos = 0; indexPos < objectArray.length; indexPos++) {
180 |
181 | CompareValue compareValue = objectArray[indexPos];
182 |
183 | String webColor = "#".concat(Integer.toHexString(compareValue.getColor().hashCode()));
184 | Color color = compareValue.getColor();
185 | int value = compareValue.getValue();
186 |
187 | Bar rect = (Bar) barsGrid.getChildren().get(indexPos);
188 | TextField textfield = (TextField) textFieldsGrid.getChildren().get(indexPos);
189 | rect.setStyle("-fx-background-color: " + webColor + ";");
190 | final Timeline tl = new Timeline();
191 | tl.getKeyFrames().addAll(
192 | new KeyFrame(Duration.millis(Math.max(MainController.DELAY_PROPERTY.get(), 20)),
193 | new KeyValue(rect.colorProperty, color)));
194 | tl.play();
195 | double height = calculateHeight(value);
196 | if(rect.getHeight() != height) {
197 | animate(rect, calculateHeight(value), color);
198 | }
199 | textfield.setStyle("-fx-border-color: " + webColor + ";"
200 | + "-fx-background-color: " + webColor.replace("ff", "33") + ";");
201 |
202 | textfield.setText(String.valueOf(value));
203 |
204 | }
205 |
206 | return null;
207 | }
208 |
209 | /**
210 | * Private inner class creates a region with a color property listener that
211 | * sets the background during animations.
212 | */
213 | private final class Bar extends Region {
214 |
215 | public final SimpleObjectProperty colorProperty = new SimpleObjectProperty<>(Color.web("#3073b4"));
216 |
217 | public Bar() {
218 |
219 | colorProperty.addListener(this::colorPropertyAction);
220 | }
221 |
222 | public void colorPropertyAction(Observable observable) {
223 | if (colorProperty.get() == null) {
224 | return;
225 | }
226 | String color = "#".concat(Integer.toHexString(colorProperty.get().hashCode()));
227 | setStyle("-fx-background-color: " + color + ";");
228 | }
229 | }
230 | }
231 |
--------------------------------------------------------------------------------
/src/main/java/sortingalgoritms/MainController.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 java.net.URL;
18 | import java.util.Arrays;
19 | import java.util.List;
20 |
21 | import java.util.ResourceBundle;
22 | import java.util.concurrent.ExecutorService;
23 | import java.util.concurrent.Executors;
24 | import java.util.concurrent.TimeUnit;
25 | import javafx.animation.Animation;
26 | import javafx.animation.KeyFrame;
27 | import javafx.animation.Timeline;
28 | import javafx.application.Platform;
29 | import javafx.beans.Observable;
30 |
31 | import javafx.beans.property.SimpleBooleanProperty;
32 | import javafx.beans.property.SimpleIntegerProperty;
33 | import javafx.event.ActionEvent;
34 | import javafx.fxml.FXML;
35 | import javafx.fxml.Initializable;
36 | import javafx.scene.control.Button;
37 | import javafx.scene.control.ComboBox;
38 | import javafx.scene.control.Label;
39 | import javafx.scene.control.Spinner;
40 | import javafx.scene.control.SpinnerValueFactory;
41 | import javafx.scene.control.TextArea;
42 | import javafx.scene.input.ScrollEvent;
43 | import javafx.scene.layout.AnchorPane;
44 | import sortingalgoritms.sorts.AbstractSort;
45 | import sortingalgoritms.sorts.SortOperatorList;
46 | import sortingalgoritms.ui.AnimationController;
47 | import sortingalgoritms.util.Logger;
48 | import sortingalgoritms.sorts.SortOperator;
49 | import sortingalgoritms.util.RandomValues;
50 |
51 | /**
52 | * FXML Controller class
53 | *
54 | * @author Eric Canull
55 | */
56 | public class MainController implements Initializable {
57 |
58 | public static final SimpleIntegerProperty DELAY_PROPERTY = new SimpleIntegerProperty();
59 |
60 | private final SimpleBooleanProperty disableUI = new SimpleBooleanProperty(false);
61 |
62 | private final SortOperatorList sortOperators = new SortOperatorList();
63 |
64 | private AnimationController animationController;
65 |
66 | private ExecutorService executor;
67 |
68 | private Timeline timeline;
69 |
70 | @FXML private AnchorPane anchorPane;
71 | @FXML private TextArea logTextArea;
72 | @FXML private Button sortButton, clearButton;
73 | @FXML private ComboBox algorithmsComboBox, presetsComboBox;
74 | @FXML private Spinner delaySpinner;
75 | @FXML private Label algorithmLabel, countLabel, statusLabel;
76 |
77 | /**
78 | * Initializes the main controller class.
79 | * @param url
80 | * @param rb
81 | */
82 | @Override
83 | public void initialize(URL url, ResourceBundle rb) {
84 |
85 | // Add the graphics controller pane
86 | animationController = new AnimationController();
87 | AnchorPane.setTopAnchor(animationController, 50.0);
88 | AnchorPane.setBottomAnchor(animationController, 0.0);
89 | AnchorPane.setLeftAnchor(animationController, 0.0);
90 | AnchorPane.setRightAnchor(animationController, 0.0);
91 | anchorPane.getChildren().add(animationController);
92 |
93 | // Add algoritms list and select the first index in the combobox
94 | algorithmsComboBox.getItems().setAll(getAlgorithmsList());
95 | algorithmsComboBox.getSelectionModel().select(1);
96 |
97 | // Add preset values list and listener to combobox
98 | presetsComboBox.getItems().setAll(getPresetsList());
99 | presetsComboBox.valueProperty().addListener(this::presetComboBoxAction);
100 | presetsComboBox.getSelectionModel().select(0);
101 |
102 | // Create spinner factory with default min, max, current, step
103 | SpinnerValueFactory valueFactory = new SpinnerValueFactory
104 | .IntegerSpinnerValueFactory(0, 2000, 250, 10);
105 |
106 | // Set the spinner value factory
107 | delaySpinner.setValueFactory(valueFactory);
108 | DELAY_PROPERTY.bind(delaySpinner.valueProperty());
109 |
110 | // Create a timeline with animation delay and indefinite cycle count
111 | timeline = new Timeline(new KeyFrame(javafx.util.Duration.millis(
112 | delaySpinner.getValue()), ae -> updateViews()));
113 | timeline.setCycleCount(Animation.INDEFINITE);
114 |
115 | // Bind algorithm name to the label
116 | algorithmLabel.textProperty().set(algorithmsComboBox.getValue());
117 |
118 | // Bind the UI controls to the disableUI boolean property
119 | statusLabel.getGraphic().visibleProperty().bind(disableUI);
120 | sortButton.disableProperty().bind(disableUI);
121 | clearButton.disableProperty().bind(disableUI);
122 | delaySpinner.disableProperty().bind(disableUI);
123 | algorithmsComboBox.disableProperty().bind(disableUI);
124 | presetsComboBox.disableProperty().bind(disableUI);
125 | }
126 |
127 | /**
128 | * Observable helper method updates the preset values
129 | * on selection change.
130 | */
131 | private void presetComboBoxAction(Observable observable) {
132 | animationController.setPresetValues(presetsComboBox.getValue());
133 | animationController.onResizeAnimated();
134 | }
135 |
136 | /**
137 | * Mouse listener increases or decreases the spinner value with
138 | * the mouse scroll wheel.
139 | * @param event
140 | */
141 | @FXML
142 | private void spinnerScrollAction(ScrollEvent event) {
143 | if (event.getDeltaY() > 0) {
144 | delaySpinner.increment();
145 | } else if (event.getDeltaY() < 0) {
146 | delaySpinner.decrement();
147 | }
148 | }
149 |
150 | /**
151 | * Start button method to startSort the sorting operation
152 | */
153 | @FXML
154 | private void sortAction(ActionEvent event) {
155 | start();
156 | }
157 |
158 | /**
159 | * Starts the sorting operation
160 | */
161 | public void start() {
162 |
163 | disableUI.set(true); // Disable UI
164 | countLabel.setText("0"); // Reset count label
165 |
166 | // Load the selected algorithm
167 | int sortIndex = getAlgorithmIndex();
168 |
169 | //display the preset values in the text area
170 | logTextArea.appendText("Preset Values\n");
171 | logTextArea.appendText(RandomValues.getString().concat("\n\n"));
172 |
173 | // Update the algorithm name to the labels and text area
174 | String sortName = algorithmsComboBox.getValue().concat(" Sort\n");
175 | algorithmLabel.setText(sortName);
176 | logTextArea.appendText(sortName);
177 |
178 | // Start the timeline
179 | timeline.play();
180 |
181 | // Create a new thread to startSort the sorting algorithm
182 | executor = Executors.newSingleThreadExecutor();
183 | executor.submit(() -> {
184 |
185 | // Perform the sort at the position in the list
186 | AbstractSort sorter = sortOperators.getList().get(sortIndex);
187 | sorter.sort(RandomValues.getArray(), 0, RandomValues.MAX_SIZE - 1);
188 |
189 | // Append text area with metric data
190 | Platform.runLater(() -> {
191 | updateViews();
192 | appendMetricText();
193 | });
194 |
195 | // Sort completed
196 | stop();
197 | });
198 | }
199 |
200 | /**
201 | * Shutdown running tasks and update the status label
202 | */
203 | private void stop() {
204 | try {
205 | executor.shutdown();
206 | executor.awaitTermination(100, TimeUnit.MILLISECONDS);
207 | } catch (InterruptedException e) {
208 | setStatusText("Status: Interrupted");
209 | } finally {
210 | if (!executor.isTerminated()) {
211 | executor.shutdownNow();
212 | }
213 | timeline.stop();
214 | setDisableUI(false); // enable UI
215 | setStatusText("Status: Ready"); // Set status
216 | }
217 | }
218 |
219 | /**
220 | * Updates the animation pane bars and numbered text fields
221 | * with progressive sort data until the sorting is complete.
222 | */
223 | private void updateViews() {
224 | SortOperator.getInstance().apply(RandomValues.getArray(), animationController);
225 | Platform.runLater(() -> {
226 | statusLabel.setText("Status: Sorting");
227 | countLabel.setText(Long.toString(Logger.getCount()));
228 | });
229 | }
230 |
231 | /**
232 | * Sets the disable UI flag in the FX thread
233 | * @param status
234 | */
235 | private void setDisableUI(Boolean isDisable) {
236 | Platform.runLater(() -> (this.disableUI.set(isDisable)));
237 | }
238 |
239 | /**
240 | * Sets the status label text in the FX thread
241 | * @param status
242 | */
243 | private void setStatusText(String status) {
244 | Platform.runLater(() -> (statusLabel.setText(status)));
245 | }
246 |
247 | /**
248 | * Appends the info text area with the metric data for the sorting routine.
249 | */
250 | private void appendMetricText() {
251 |
252 | // Calculates the difference between start and end time
253 | long delta = Logger.endTime - Logger.startTime;
254 |
255 | // Create a new string builder with metric data
256 | final StringBuilder sb = new StringBuilder();
257 | sb.append("Start: ").append(Logger.startTime).append(" ns \n");
258 | sb.append("Ended: ").append(Logger.endTime).append(" ns \n");
259 | sb.append("Delay: ").append(delaySpinner.getValue()).append(" ms\n");
260 | sb.append("Speed: ").append(delta).append(" ns").append("\n");
261 | sb.append("Steps: ").append(Logger.getCount()).append("\n\n");
262 |
263 | // Appends the time stamp to the text area on the left-side display
264 | logTextArea.appendText(sb.toString());
265 | }
266 |
267 | /**
268 | * Gets the current index selected for the algorithm combo box
269 | * @return
270 | */
271 | private int getAlgorithmIndex() {
272 | return algorithmsComboBox.getSelectionModel().getSelectedIndex();
273 | }
274 |
275 | /**
276 | * Clears the text area
277 | * @param event
278 | */
279 | @FXML
280 | private void clearTextArea(ActionEvent event) {
281 | logTextArea.clear();
282 | }
283 |
284 | /**
285 | * The list of sort algorithms to choose in the combobox
286 | */
287 | private static List getAlgorithmsList() {
288 | String[] algorithms
289 | = {"Bubble", "Selection", "Insertion", "Merge", "Quick",
290 | "Shell", "Pancake", "Cocktail", "Heap", "Exchange"};
291 | return Arrays.asList(algorithms);
292 | }
293 |
294 | /**
295 | * The list of preset values to choose in the combobox
296 | */
297 | private static List getPresetsList() {
298 | String[] presets
299 | = {"Random", "Ordered", "Reverse", "Hundreds", "Thousands"};
300 | return Arrays.asList(presets);
301 | }
302 | }
303 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 | {one line to give the program's name and a brief idea of what it does.}
635 | Copyright (C) {year} {name of author}
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | {project} Copyright (C) {year} {fullname}
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------