extract lists + separate tests, quicksort

This commit is contained in:
wgroeneveld 2018-03-24 10:10:49 +01:00
parent 198fcbf051
commit 8ff8f3ec53
6 changed files with 157 additions and 30 deletions

View File

@ -0,0 +1,18 @@
package be.brainbaking.lists;
import java.util.List;
import java.util.stream.Collectors;
public class Lists {
public static void swap(List<Integer> list, int oneBasedA, int oneBasedB) {
int temp = list.get(oneBasedA - 1);
list.set(oneBasedA - 1, list.get(oneBasedB - 1));
list.set(oneBasedB - 1, temp);
}
public static String asString(List<Integer> list) {
return list.stream().map(i -> i.toString()).collect(Collectors.joining(", "));
}
}

View File

@ -1,14 +1,14 @@
package be.brainbaking.sorting;
import be.brainbaking.lists.Lists;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class HeapSort implements Sortable {
import static be.brainbaking.lists.Lists.asString;
private String asString(List<Integer> list) {
return list.stream().map(i -> i.toString()).collect(Collectors.joining(", "));
}
public class HeapSort implements Sortable {
/**
* heap-size[A] <- length[A]
@ -53,17 +53,11 @@ public class HeapSort implements Sortable {
}
if(largesti != oneBasedIndex) {
swap(list, oneBasedIndex, largesti);
Lists.swap(list, oneBasedIndex, largesti);
maxHeapify(list, largesti);
}
}
private void swap(List<Integer> list, int oneBasedA, int oneBasedB) {
int temp = list.get(oneBasedA - 1);
list.set(oneBasedA - 1, list.get(oneBasedB - 1));
list.set(oneBasedB - 1, temp);
}
/**
* BUILD-MAX-HEAP(A)
* for i <- length[A] downto 2
@ -79,6 +73,7 @@ public class HeapSort implements Sortable {
for(int i = heap.size(); i >= 1; i--) {
// in plaats van te swappen met 1, i pitsen we het hoofd van de tree er af
// ik wil niet heap-size manipuleren en doorgeven
// !! Vergeet niet dat dit O(n!) tijd toevoegt aan ons algoritme
sorted.add(0, heap.get(0));
heap.remove(0);
maxHeapify(heap, 1);

View File

@ -0,0 +1,79 @@
package be.brainbaking.sorting;
import be.brainbaking.lists.Lists;
import java.util.ArrayList;
import java.util.List;
import static be.brainbaking.lists.Lists.asString;
public class QuickSort implements Sortable {
/**
* quicksort met randomizer, anders wordt als partition index altijd A[r] gebruikt!
* @param list
* @return
*/
@Override
public List<Integer> sort(List<Integer> list) {
List<Integer> sorted = new ArrayList<>(list);
int i = (int)(Math.random() * list.size());
int r = list.size();
Lists.swap(sorted, i, r);
quickSort(sorted, 1, sorted.size());
return sorted;
}
/**
* quicksort default
* if p < r
* then q <- partition(A, p, r)
* quicksort(A, p, q - 1)
* quicksort(A, q + 1, r)
* @param list to sort
* @param oneBasedPartitionStart p
* @param oneBasedPartitionEnd r
* @return sorted list
*/
private void quickSort(List<Integer> list, int oneBasedPartitionStart, int oneBasedPartitionEnd) {
if(oneBasedPartitionStart >= oneBasedPartitionEnd) return;
int q = partition(list, oneBasedPartitionStart, oneBasedPartitionEnd);
quickSort(list, oneBasedPartitionStart, q - 1);
quickSort(list, q + 1, oneBasedPartitionEnd);
}
/**
* partition list so that each element of A[p..q-1] <= A[q] <= A[q + 1 .. r]
* x <- A[r]
* i <- p - 1
* for j <- p to r - 1
* do if A[j] <= x
* then i <- i + 1
* exchange A[i] with A[j]
* exchange A[i + 1] with A[r]
* return i + 1
* @param list A
* @param oneBasedStartIndex p
* @param oneBasedEndIndex r
* @return i + 1 (partition index)
*/
protected int partition(List<Integer> list, int oneBasedStartIndex, int oneBasedEndIndex) {
System.out.println("to partition: " + asString(list));
int x = list.get(oneBasedEndIndex - 1);
int i = oneBasedStartIndex - 1;
for(int j = oneBasedStartIndex; j <= oneBasedEndIndex - 1; j++) {
if(list.get(j - 1) <= x) {
i++;
Lists.swap(list, i, j);
}
}
Lists.swap(list, i + 1, oneBasedEndIndex);
System.out.println("partitioned: " + asString(list));
return i + 1;
}
}

View File

@ -0,0 +1,30 @@
package be.brainbaking.sorting;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.List;
public class HeapSortTest {
@Test
public void heapSort_withBiggerHeap() {
HeapSort sort = new HeapSort();
List<Integer> expetedSort = Arrays.asList(1, 2, 3, 4, 7, 8, 9, 10, 14, 16);
List<Integer> actualSort = sort.sort(Arrays.asList(4, 1, 3, 2, 16, 9, 10, 14, 8, 7));
Assertions.assertArrayEquals(expetedSort.toArray(), actualSort.toArray());
}
@Test
public void heapSort_buildMaxHeap() {
HeapSort sort = new HeapSort();
List<Integer> expectedHeap = Arrays.asList(16, 14, 10, 8, 7, 9, 3, 2, 4, 1);
List<Integer> maxHeap = sort.buildMaxHeap(Arrays.asList(4, 1, 3, 2, 16, 9, 10, 14, 8, 7));
Assertions.assertArrayEquals(expectedHeap.toArray(), maxHeap.toArray());
}
}

View File

@ -0,0 +1,22 @@
package be.brainbaking.sorting;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class QuickSortTest {
@Test
public void partition_accordingToR() {
QuickSort sort = new QuickSort();
List<Integer> toPartition = Arrays.asList(2, 8, 7, 1, 3, 5, 6, 4);
List<Integer> partitioned = new ArrayList<>(toPartition);
sort.partition(partitioned, 1, toPartition.size());
Assertions.assertArrayEquals(partitioned.toArray(), Arrays.asList(2, 1, 3, 4, 7, 5, 6, 8).toArray());
}
}

View File

@ -1,33 +1,16 @@
package be.brainbaking.sorting;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SortTest {
@Test
public void heapSort_withBiggerHeap() {
HeapSort sort = new HeapSort();
List<Integer> expetedSort = Arrays.asList(1, 2, 3, 4, 7, 8, 9, 10, 14, 16);
List<Integer> actualSort = sort.sort(Arrays.asList(4, 1, 3, 2, 16, 9, 10, 14, 8, 7));
Assertions.assertArrayEquals(expetedSort.toArray(), actualSort.toArray());
}
@Test
public void heapSort_buildMaxHeap() {
HeapSort sort = new HeapSort();
List<Integer> expectedHeap = Arrays.asList(16, 14, 10, 8, 7, 9, 3, 2, 4, 1);
List<Integer> maxHeap = sort.buildMaxHeap(Arrays.asList(4, 1, 3, 2, 16, 9, 10, 14, 8, 7));
Assertions.assertArrayEquals(expectedHeap.toArray(), maxHeap.toArray());
public void quickSort() {
simpleTestCaseFor(new QuickSort());
}
@Test