Wouter Groeneveld 2018-04-09 15:49:34 +02:00
parent 959e23575d
commit d30a8c82da
3 changed files with 29 additions and 2 deletions

View File

@ -5,6 +5,10 @@ import java.util.stream.Collectors;
public class Lists {
public static ListsWrapper exchange(int oneBasedA) {
return new ListsWrapper(oneBasedA);
}
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));

View File

@ -0,0 +1,22 @@
package be.brainbaking.lists;
import java.util.List;
public class ListsWrapper {
private final int oneBasedA;
private int oneBasedB;
public ListsWrapper with(int oneBasedB) {
this.oneBasedB = oneBasedB;
return this;
}
public void in(List<Integer> list) {
Lists.swap(list, oneBasedA, oneBasedB);
}
public ListsWrapper(int oneBasedA) {
this.oneBasedA = oneBasedA;
}
}

View File

@ -6,6 +6,7 @@ import java.util.ArrayList;
import java.util.List;
import static be.brainbaking.lists.Lists.asString;
import static be.brainbaking.lists.Lists.exchange;
public class QuickSort implements Sortable {
@ -68,11 +69,11 @@ public class QuickSort implements Sortable {
for(int j = oneBasedStartIndex; j <= oneBasedEndIndex - 1; j++) {
if(list.get(j - 1) <= x) {
i++;
Lists.swap(list, i, j);
exchange(i).with(j).in(list);
}
}
Lists.swap(list, i + 1, oneBasedEndIndex);
exchange(i + 1).with(oneBasedEndIndex).in(list);
System.out.println("partitioned: " + asString(list));
return i + 1;
}