How to resolve the algorithm Pancake numbers step by step in the Java programming language
How to resolve the algorithm Pancake numbers step by step in the Java programming language
Table of Contents
Problem Statement
Adrian Monk has problems and an assistant, Sharona Fleming. Sharona can deal with most of Adrian's problems except his lack of punctuality paying her remuneration. 2 pay checks down and she prepares him pancakes for breakfast. Knowing that he will be unable to eat them unless they are stacked in ascending order of size she leaves him only a skillet which he can insert at any point in the pile and flip all the above pancakes, repeating until the pile is sorted. Sharona has left the pile of n pancakes such that the maximum number of flips is required. Adrian is determined to do this in as few flips as possible. This sequence n->p(n) is known as the Pancake numbers. The task is to determine p(n) for n = 1 to 9, and for each show an example requiring p(n) flips. Sorting_algorithms/Pancake_sort actually performs the sort some giving the number of flips used. How do these compare with p(n)? Few people know p(20), generously I shall award an extra credit for anyone doing more than p(16).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Pancake numbers step by step in the Java programming language
First Source Code:
This Java code finds the minimum number of flips required to sort a stack of pancakes. A "flip" operation reverses the order of the pancakes within a specified range.
-
Function pancake(int n):
- Takes an integer
n
representing the number of pancakes in the stack. - Calculates the minimum flips using a mathematical formula and returns the result.
- Takes an integer
-
Main Method:
- Prints the minimum flips for various values of
n
.
- Prints the minimum flips for various values of
How it Works:
The pancake function uses a formula to approximate the minimum flips needed to sort a stack of n
pancakes. The formula is based on the observation that the minimum flips required for a stack of n
pancakes is approximately n + sqrt(n)
.
Second Source Code:
This Java code addresses the same pancake sorting problem using a brute-force approach.
-
Function flipStack(List
stack, int spatula): - Takes a list representing the stack of pancakes and an integer representing the spatula size.
- Reverses the order of the pancakes within the range of the spatula.
-
Function pancake(int n):
- Takes an integer
n
representing the number of pancakes in the stack. - Performs a breadth-first search on all possible stack configurations by flipping pancakes with various spatulas.
- Records the minimum number of flips required for each stack configuration.
- Returns the stack configuration with the minimum number of flips.
- Takes an integer
-
Main Method:
- Prints the minimum flips and the sorted stack configuration for values of
n
from 1 to 10.
- Prints the minimum flips and the sorted stack configuration for values of
How it Works:
The pancake function starts with an unsorted stack and iteratively flips pancakes with different spatula sizes. It keeps track of the minimum number of flips required for each stack configuration. By exploring all possible stack configurations, it ensures that it finds the optimal solution.
Source code in the java programming language
public class Pancake {
private static int pancake(int n) {
int gap = 2;
int sum = 2;
int adj = -1;
while (sum < n) {
adj++;
gap = 2 * gap - 1;
sum += gap;
}
return n + adj;
}
public static void main(String[] args) {
for (int i = 0; i < 4; i++) {
for (int j = 1; j < 6; j++) {
int n = 5 * i + j;
System.out.printf("p(%2d) = %2d ", n, pancake(n));
}
System.out.println();
}
}
}
import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toList;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.stream.IntStream;
public class Pancake {
private static List<Integer> flipStack(List<Integer> stack, int spatula) {
List<Integer> copy = new ArrayList<>(stack);
Collections.reverse(copy.subList(0, spatula));
return copy;
}
private static Map.Entry<List<Integer>, Integer> pancake(int n) {
List<Integer> initialStack = IntStream.rangeClosed(1, n).boxed().collect(toList());
Map<List<Integer>, Integer> stackFlips = new HashMap<>();
stackFlips.put(initialStack, 1);
Queue<List<Integer>> queue = new ArrayDeque<>();
queue.add(initialStack);
while (!queue.isEmpty()) {
List<Integer> stack = queue.remove();
int flips = stackFlips.get(stack) + 1;
for (int i = 2; i <= n; ++i) {
List<Integer> flipped = flipStack(stack, i);
if (stackFlips.putIfAbsent(flipped, flips) == null) {
queue.add(flipped);
}
}
}
return stackFlips.entrySet().stream().max(comparing(e -> e.getValue())).get();
}
public static void main(String[] args) {
for (int i = 1; i <= 10; ++i) {
Map.Entry<List<Integer>, Integer> result = pancake(i);
System.out.printf("pancake(%s) = %s. Example: %s\n", i, result.getValue(), result.getKey());
}
}
}
You may also check:How to resolve the algorithm Middle three digits step by step in the D programming language
You may also check:How to resolve the algorithm Rosetta Code/Rank languages by popularity step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Apply a callback to an array step by step in the Nim programming language
You may also check:How to resolve the algorithm Levenshtein distance step by step in the TUSCRIPT programming language
You may also check:How to resolve the algorithm Multiplication tables step by step in the Elixir programming language