Browse Source

Implemented quicksort sort using my custom array list.

master
jrtechs 6 years ago
parent
commit
dd299524c4
2 changed files with 146 additions and 0 deletions
  1. +95
    -0
      src/main/java/net/jrtechs/www/Sorting/QuickSort.java
  2. +51
    -0
      src/test/java/net/jrtechs/www/sorting/QuickSortTest.java

+ 95
- 0
src/main/java/net/jrtechs/www/Sorting/QuickSort.java View File

@ -0,0 +1,95 @@
package net.jrtechs.www.Sorting;
import net.jrtechs.www.DataStructures.Lists.ArrayList.ArrayBackedList;
import java.util.Random;
/**
* Basic implementation of quick sort in java.
*
* @author Jeffery Russell
* @param <E>
*/
public class QuickSort<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 QuickSort(int sortType)
{
this.sortType= sortType;
}
/**
* Wrapper function for initial call.
*
* @param list
*/
public void quickSort(ArrayBackedList<E> list)
{
quickSort(list, 0, list.size() -1);
}
/**
* Quick sorts the array.
*
* @param list array to sort
* @param left left index
* @param right right index
*/
public void quickSort(ArrayBackedList<E> list, int left, int right)
{
if(left < right)
{
int pivot = partition(list, left, right);
quickSort(list, left, pivot -1);
quickSort(list, pivot + 1, right);
}
}
/**
* Selects a random pivot and swaps everything less than the pivot value
* to the left and everything greater than the pivot to the right.
*
* @param list array list to sort
* @param left left index
* @param right right index
* @return index of the sorted part
*/
private int partition(ArrayBackedList<E> list, int left, int right)
{
Random random = new Random();
int p = random.nextInt((right - left) + 1) + left; //select pivot
list.swap(p, right);
int store = left;
for(int i = left; i < right; i++)
{
if(list.get(i).compareTo(list.get(right)) != this.sortType)
{
list.swap(i, store);
store++;
}
}
list.swap(store, right);
return store;
}
}

+ 51
- 0
src/test/java/net/jrtechs/www/sorting/QuickSortTest.java View File

@ -0,0 +1,51 @@
package net.jrtechs.www.sorting;
import net.jrtechs.www.DataStructures.Lists.ArrayList.ArrayBackedList;
import net.jrtechs.www.Sorting.MergeSort;
import net.jrtechs.www.Sorting.QuickSort;
import org.junit.Test;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertTrue;
public class QuickSortTest
{
@Test
public void testInitalization()
{
QuickSort<Double> sorter = new QuickSort<>(MergeSort.DECREASING_ORDER);
assertNotNull(sorter);
}
@Test
public void testInsertionSortAscending()
{
QuickSort<Double> sorter = new QuickSort<>(QuickSort.INCREASING_ORDER);
ArrayBackedList<Double> list = new ArrayBackedList<>();
list.add(1.0);
list.add(0.0);
list.add(3.0);
list.add(-12.0);
sorter.quickSort(list);
assertTrue(list.get(0) == -12.0);
assertTrue(list.get(list.size() -1) == 3);
}
@Test
public void testInsertionSortDescending()
{
QuickSort<Double> sorter = new QuickSort<>(QuickSort.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);
sorter.quickSort(list);
assertTrue(list.get(0) == 3.0);
assertTrue(list.get(list.size() -1) == -12);
}
}

Loading…
Cancel
Save