This commit is contained in:
parent
959e23575d
commit
d30a8c82da
|
@ -5,6 +5,10 @@ import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class Lists {
|
public class Lists {
|
||||||
|
|
||||||
|
public static ListsWrapper exchange(int oneBasedA) {
|
||||||
|
return new ListsWrapper(oneBasedA);
|
||||||
|
}
|
||||||
|
|
||||||
public static void swap(List<Integer> list, int oneBasedA, int oneBasedB) {
|
public static void swap(List<Integer> list, int oneBasedA, int oneBasedB) {
|
||||||
int temp = list.get(oneBasedA - 1);
|
int temp = list.get(oneBasedA - 1);
|
||||||
list.set(oneBasedA - 1, list.get(oneBasedB - 1));
|
list.set(oneBasedA - 1, list.get(oneBasedB - 1));
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,6 +6,7 @@ import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static be.brainbaking.lists.Lists.asString;
|
import static be.brainbaking.lists.Lists.asString;
|
||||||
|
import static be.brainbaking.lists.Lists.exchange;
|
||||||
|
|
||||||
public class QuickSort implements Sortable {
|
public class QuickSort implements Sortable {
|
||||||
|
|
||||||
|
@ -68,11 +69,11 @@ public class QuickSort implements Sortable {
|
||||||
for(int j = oneBasedStartIndex; j <= oneBasedEndIndex - 1; j++) {
|
for(int j = oneBasedStartIndex; j <= oneBasedEndIndex - 1; j++) {
|
||||||
if(list.get(j - 1) <= x) {
|
if(list.get(j - 1) <= x) {
|
||||||
i++;
|
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));
|
System.out.println("partitioned: " + asString(list));
|
||||||
return i + 1;
|
return i + 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue