From 64dd6b66932fff90efecc8150917f470a52b4bb7 Mon Sep 17 00:00:00 2001 From: jrtechs Date: Mon, 27 Aug 2018 21:59:30 -0400 Subject: [PATCH] Implemented merge sort using my custom array list. --- .../net/jrtechs/www/Sorting/MergeSort.java | 101 ++++++++++++++++++ .../net/jrtechs/www/Sorting/SortableList.java | 1 + .../jrtechs/www/sorting/MergeSortTest.java | 50 +++++++++ 3 files changed, 152 insertions(+) create mode 100644 src/main/java/net/jrtechs/www/Sorting/MergeSort.java create mode 100644 src/test/java/net/jrtechs/www/sorting/MergeSortTest.java diff --git a/src/main/java/net/jrtechs/www/Sorting/MergeSort.java b/src/main/java/net/jrtechs/www/Sorting/MergeSort.java new file mode 100644 index 0000000..4c053f0 --- /dev/null +++ b/src/main/java/net/jrtechs/www/Sorting/MergeSort.java @@ -0,0 +1,101 @@ +package net.jrtechs.www.Sorting; + +import net.jrtechs.www.DataStructures.Lists.ArrayList.ArrayBackedList; + + +/** + * Object used to sort lists + * + * @author Jeffery Russell 8-27-18 + * @param + */ +public class MergeSort +{ + /** Constant for an increasing list */ + public static final int INCREASING_ORDER = 1; + + /** Constant for a decreasing list */ + public static final int DECREASING_ORDER = -1; + + /** Type of ordering*/ + private int sortType; + + + /** + * Creates a new sorter with a specific ordering + * + * @param sortType sorting constant + */ + public MergeSort(int sortType) + { + this.sortType= sortType; + } + + + /** + * Divides and conquer approach to sorting an array + * + * @param list list to sort + * @return sorted list + */ + public ArrayBackedList mergeSort(ArrayBackedList list) + { + if(list.size() <= 1) + return list; + + ArrayBackedList left = new ArrayBackedList<>(list.size()/2); + ArrayBackedList right = new ArrayBackedList<>(list.size()/2); + + + for(int i = 0; i < list.size()/2; i++) + { + left.add(list.get(i)); + } + for(int i = list.size()/2; i< list.size(); i++) + { + right.add(list.get(i)); + } + + left = mergeSort(left); + right = mergeSort(right); + + return mergeLists(left, right); + } + + + /** + * Merges two ordered lists together in a way in which produces + * another ordered list. + * + * @param left ordered list + * @param right ordered list + * @return ordered list + */ + private SortableList mergeLists(ArrayBackedList left, + ArrayBackedList right) + { + SortableList result = new SortableList<> + (left.size() + right.size()); + + //Trick I did because the remove operation of the + //arraybackedList is not O(N) + int rightRemoved = 0, leftRemoved = 0; + + while(left.size() != leftRemoved && right.size() != rightRemoved) + { + if(left.get(leftRemoved).compareTo(right.get(rightRemoved)) + != this.sortType) + result.add(left.get(leftRemoved++)); + else + result.add(right.get(rightRemoved++)); + } + + while(left.size() != leftRemoved) + result.add(left.get(leftRemoved++)); + + while(right.size() != rightRemoved) + result.add(right.get(rightRemoved++)); + + return result; + } +} diff --git a/src/main/java/net/jrtechs/www/Sorting/SortableList.java b/src/main/java/net/jrtechs/www/Sorting/SortableList.java index 591887a..58727d4 100644 --- a/src/main/java/net/jrtechs/www/Sorting/SortableList.java +++ b/src/main/java/net/jrtechs/www/Sorting/SortableList.java @@ -55,4 +55,5 @@ public class SortableList extends ArrayBackedList } } } + } diff --git a/src/test/java/net/jrtechs/www/sorting/MergeSortTest.java b/src/test/java/net/jrtechs/www/sorting/MergeSortTest.java new file mode 100644 index 0000000..a9d8953 --- /dev/null +++ b/src/test/java/net/jrtechs/www/sorting/MergeSortTest.java @@ -0,0 +1,50 @@ +package net.jrtechs.www.sorting; + +import net.jrtechs.www.DataStructures.Lists.ArrayList.ArrayBackedList; +import net.jrtechs.www.Sorting.MergeSort; +import org.junit.Test; + +import static junit.framework.TestCase.assertNotNull; +import static junit.framework.TestCase.assertTrue; + +public class MergeSortTest +{ + @Test + public void testInitalization() + { + MergeSort sorter = new MergeSort<>(MergeSort.DECREASING_ORDER); + + assertNotNull(sorter); + } + + + @Test + public void testInsertionSortAscending() + { + MergeSort sorter = new MergeSort<>(MergeSort.INCREASING_ORDER); + ArrayBackedList list = new ArrayBackedList<>(); + list.add(1.0); + list.add(0.0); + list.add(3.0); + list.add(-12.0); + list = sorter.mergeSort(list); + + assertTrue(list.get(0) == -12.0); + assertTrue(list.get(list.size() -1) == 3); + } + + @Test + public void testInsertionSortDescending() + { + MergeSort sorter = new MergeSort<>(MergeSort.DECREASING_ORDER); + ArrayBackedList list = new ArrayBackedList<>(); + list.add(1.2); + list.add(1.0); + list.add(0.0); + list.add(3.0); + list.add(-12.0); + list = sorter.mergeSort(list); + assertTrue(list.get(0) == 3.0); + assertTrue(list.get(list.size() -1) == -12); + } +}