Browse Source

Implemented merge sort using my custom array list.

master
jrtechs 6 years ago
parent
commit
64dd6b6693
3 changed files with 152 additions and 0 deletions
  1. +101
    -0
      src/main/java/net/jrtechs/www/Sorting/MergeSort.java
  2. +1
    -0
      src/main/java/net/jrtechs/www/Sorting/SortableList.java
  3. +50
    -0
      src/test/java/net/jrtechs/www/sorting/MergeSortTest.java

+ 101
- 0
src/main/java/net/jrtechs/www/Sorting/MergeSort.java View File

@ -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 <E>
*/
public class MergeSort<E extends Comparable>
{
/** 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<E> mergeSort(ArrayBackedList<E> list)
{
if(list.size() <= 1)
return list;
ArrayBackedList<E> left = new ArrayBackedList<>(list.size()/2);
ArrayBackedList<E> 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<E> mergeLists(ArrayBackedList<E> left,
ArrayBackedList<E> right)
{
SortableList<E> 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;
}
}

+ 1
- 0
src/main/java/net/jrtechs/www/Sorting/SortableList.java View File

@ -55,4 +55,5 @@ public class SortableList extends ArrayBackedList
}
}
}
}

+ 50
- 0
src/test/java/net/jrtechs/www/sorting/MergeSortTest.java View File

@ -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<Double> sorter = new MergeSort<>(MergeSort.DECREASING_ORDER);
assertNotNull(sorter);
}
@Test
public void testInsertionSortAscending()
{
MergeSort<Double> sorter = new MergeSort<>(MergeSort.INCREASING_ORDER);
ArrayBackedList<Double> 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<Double> sorter = new MergeSort<>(MergeSort.DECREASING_ORDER);
ArrayBackedList<Double> 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);
}
}

Loading…
Cancel
Save