java problem

i need java code of pancake problem using Heuristic function
syeda nusratAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Rosetta Code - Pancake Sort:

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) {
      else if (bestXPos == 0) {
      else if (altXPos == n-1) {
         dir = 1-dir;
         flipped = true;
      else {
      sort(n, dir);
      if (flipped) {
   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);

Open in new window

ste5an, we don't just supply code on demand here, especially for something that's likely to be classwork

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Nice Smash 'n' Grab there, ste5an     .      .     .
Ethics versus expediency. Question.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.