From c92d0d15312bce3e032663fcb5c7c8994d4eb0c6 Mon Sep 17 00:00:00 2001 From: jrtechs Date: Mon, 27 Aug 2018 20:34:45 -0400 Subject: [PATCH] Implemented insertion sort using my custom array list. --- .../Lists/ArrayList/ArrayBackedList.java | 2 +- .../net/jrtechs/www/Sorting/SortableList.java | 58 +++++++++++++++++++ .../www/sorting/InsertionSortTest.java | 52 +++++++++++++++++ 3 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 src/main/java/net/jrtechs/www/Sorting/SortableList.java create mode 100644 src/test/java/net/jrtechs/www/sorting/InsertionSortTest.java diff --git a/src/main/java/net/jrtechs/www/DataStructures/Lists/ArrayList/ArrayBackedList.java b/src/main/java/net/jrtechs/www/DataStructures/Lists/ArrayList/ArrayBackedList.java index ac9cab5..86e70ca 100644 --- a/src/main/java/net/jrtechs/www/DataStructures/Lists/ArrayList/ArrayBackedList.java +++ b/src/main/java/net/jrtechs/www/DataStructures/Lists/ArrayList/ArrayBackedList.java @@ -26,7 +26,7 @@ public class ArrayBackedList implements IList /** Array to store data elements in -- * is an array of Objects since you can't * create a list of generics*/ - private Object[] dataList; + protected Object[] dataList; /** diff --git a/src/main/java/net/jrtechs/www/Sorting/SortableList.java b/src/main/java/net/jrtechs/www/Sorting/SortableList.java new file mode 100644 index 0000000..591887a --- /dev/null +++ b/src/main/java/net/jrtechs/www/Sorting/SortableList.java @@ -0,0 +1,58 @@ +package net.jrtechs.www.Sorting; + +import net.jrtechs.www.DataStructures.Lists.ArrayList.ArrayBackedList; + + +/** + * Created an array list which is able to sort + * itself upon request. + * + * @param + * @author Jeffery Russell + */ +public class SortableList extends ArrayBackedList +{ + /** Constant for an increasing list */ + public final int INCREASING_ORDER = 1; + + /** Constant for a decreasing list */ + public final int DECREASING_ORDER = -1; + + + /** + * Creates a new list of default size + */ + public SortableList() + { + super(); + } + + + /** + * Creates a new list of non-default size + */ + public SortableList(int size) + { + super(size); + } + + + /** + * Sorts the list using insertion sort. + * + * @param sortOrder constant corresponding to sort type + */ + public void insertionSort(int sortOrder) + { + for(int i = 1; i < this.size(); i++) + { + int reverseIndex = i; + while(reverseIndex > 0 && this.get(reverseIndex-1) + .compareTo(this.get(reverseIndex)) == sortOrder) + { + this.swap(reverseIndex, reverseIndex-1); + reverseIndex--; + } + } + } +} diff --git a/src/test/java/net/jrtechs/www/sorting/InsertionSortTest.java b/src/test/java/net/jrtechs/www/sorting/InsertionSortTest.java new file mode 100644 index 0000000..aeb1c1f --- /dev/null +++ b/src/test/java/net/jrtechs/www/sorting/InsertionSortTest.java @@ -0,0 +1,52 @@ +package net.jrtechs.www.sorting; + + +import net.jrtechs.www.Sorting.SortableList; +import org.junit.Test; + +import static junit.framework.TestCase.assertNotNull; +import static junit.framework.TestCase.assertTrue; + +/** + * Class for testing insertion sort + * + * @author Jeffery Russell 8-27-18 + */ +public class InsertionSortTest +{ + @Test + public void testInitalization() + { + SortableList list = new SortableList<>(); + + assertNotNull(list); + } + + + @Test + public void testInsertionSortAscending() + { + SortableList list = new SortableList<>(); + list.add(1.0); + list.add(0.0); + list.add(3.0); + list.add(-12.0); + list.insertionSort(list.INCREASING_ORDER); + assertTrue(list.get(0) == -12.0); + assertTrue(list.get(list.size() -1) == 3); + } + + @Test + public void testInsertionSortDescending() + { + SortableList list = new SortableList<>(); + list.add(1.2); + list.add(1.0); + list.add(0.0); + list.add(3.0); + list.add(-12.0); + list.insertionSort(list.DECREASING_ORDER); + assertTrue(list.get(0) == 3.0); + assertTrue(list.get(list.size() -1) == -12); + } +}