Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,82 @@
|
|||
public class PancakeSort
|
||||
{
|
||||
int[] heap;
|
||||
|
||||
public String toString() {
|
||||
String info = "";
|
||||
for (int x: heap)
|
||||
info += x + " ";
|
||||
return info;
|
||||
}
|
||||
|
||||
public void flip(int n) {
|
||||
for (int i = 0; i < (n+1) / 2; ++i) {
|
||||
int tmp = heap[i];
|
||||
heap[i] = heap[n-i];
|
||||
heap[n-i] = tmp;
|
||||
}
|
||||
System.out.println("flip(0.." + n + "): " + toString());
|
||||
}
|
||||
|
||||
public int[] minmax(int n) {
|
||||
int xm, xM;
|
||||
xm = xM = heap[0];
|
||||
int posm = 0, posM = 0;
|
||||
|
||||
for (int i = 1; i < n; ++i) {
|
||||
if (heap[i] < xm) {
|
||||
xm = heap[i];
|
||||
posm = i;
|
||||
}
|
||||
else if (heap[i] > xM) {
|
||||
xM = heap[i];
|
||||
posM = i;
|
||||
}
|
||||
}
|
||||
return new int[] {posm, posM};
|
||||
}
|
||||
|
||||
public void sort(int n, int dir) {
|
||||
if (n == 0) return;
|
||||
|
||||
int[] mM = minmax(n);
|
||||
int bestXPos = mM[dir];
|
||||
int altXPos = mM[1-dir];
|
||||
boolean flipped = false;
|
||||
|
||||
if (bestXPos == n-1) {
|
||||
--n;
|
||||
}
|
||||
else if (bestXPos == 0) {
|
||||
flip(n-1);
|
||||
--n;
|
||||
}
|
||||
else if (altXPos == n-1) {
|
||||
dir = 1-dir;
|
||||
--n;
|
||||
flipped = true;
|
||||
}
|
||||
else {
|
||||
flip(bestXPos);
|
||||
}
|
||||
sort(n, dir);
|
||||
|
||||
if (flipped) {
|
||||
flip(n);
|
||||
}
|
||||
}
|
||||
|
||||
PancakeSort(int[] numbers) {
|
||||
heap = numbers;
|
||||
sort(numbers.length, 1);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] numbers = new int[args.length];
|
||||
for (int i = 0; i < args.length; ++i)
|
||||
numbers[i] = Integer.valueOf(args[i]);
|
||||
|
||||
PancakeSort pancakes = new PancakeSort(numbers);
|
||||
System.out.println(pancakes);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
$ java PancakeSort 1 2 5 4 3 10 9 8 7
|
||||
flip(0..5): 10 3 4 5 2 1 9 8 7
|
||||
flip(0..8): 7 8 9 1 2 5 4 3 10
|
||||
flip(0..2): 9 8 7 1 2 5 4 3 10
|
||||
flip(0..7): 3 4 5 2 1 7 8 9 10
|
||||
flip(0..2): 5 4 3 2 1 7 8 9 10
|
||||
flip(0..4): 1 2 3 4 5 7 8 9 10
|
||||
1 2 3 4 5 7 8 9 10
|
||||
|
||||
$ java PancakeSort 6 7 2 1 8 9 5 3 4
|
||||
flip(0..5): 9 8 1 2 7 6 5 3 4
|
||||
flip(0..8): 4 3 5 6 7 2 1 8 9
|
||||
flip(0..1): 3 4 5 6 7 2 1 8 9
|
||||
flip(0..4): 7 6 5 4 3 2 1 8 9
|
||||
flip(0..6): 1 2 3 4 5 6 7 8 9
|
||||
1 2 3 4 5 6 7 8 9
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public final class PancakeSort {
|
||||
|
||||
public static void main(String[] aArgs) {
|
||||
List<Integer> numbers = Arrays.asList( 1, 5, 4, 2, 3, 2, 8, 6, 7 );
|
||||
System.out.println("Initial list: " + numbers);
|
||||
pancakeSort(numbers);
|
||||
}
|
||||
|
||||
private static void pancakeSort(List<Integer> aList) {
|
||||
for ( int i = aList.size() - 1; i >= 0; i-- ) {
|
||||
int index = IntStream.rangeClosed(0, i).boxed().max(Comparator.comparing(aList::get)).get();
|
||||
|
||||
if ( index != i ) {
|
||||
flip(aList, index);
|
||||
flip(aList, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void flip(List<Integer> aList, int aIndex) {
|
||||
if ( aIndex > 0 ) {
|
||||
Collections.reverse(aList.subList(0, aIndex + 1));
|
||||
System.out.println("flip 0.." + aIndex + " --> " + aList);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue