Question

Solve the pancake sorting problem with the initial state of 2,1,3,2 and the final state of...

Solve the pancake sorting problem with the initial state of 2,1,3,2 and the final state of 1,2,2,3. Note that there are two pancakes of size 2 to reduce the number of states needed. The arc-cost is the number of pancakes flipped.

  1. Apply A* search using the heuristic h1 = the index of the largest pancake that is out of place.
  2. Apply A* search using the heuristic h2 = the number of pancakes out of position.
  3. Apply UCS

Use graph search (keeping track of closed nodes) whenever applicable; that is, in your work, do not show children that are found in the set of already closed nodes.

NO CODE NEEDED

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Pancake Sorting Algorithm ,unlike a traditional sorting algorithm, helps to sort the arrays with the fewest comparisons possible. The main aim is to sort the sequence in as few reversals as possible.

Basic steps to be followed for this algorithm are :

1) Let given array be arr[] and size of array be n. let currentsize be equal to n initially. keep reducing the currentsize by 1 while it is greater than 1.

2) while (currentsize is greater than 1)
……a) Find index of the maximum element in arr[0..curr_szie-1]. Let the index be ‘mi’
……b) Call a function flip(arr,mi) which will reverse the array elements from 0 to mi.
……c) Call a function flip(arr,mi) which will reverse the array elements from 0 to (currentsize-1).

static int pancakeSort(int arr[], int n) { for (int curr_size = n; curr_size > 1; --curr_size) { int mi = findMax(arr, curr_size); if (mi != curr_size-1) { flip(arr, mi); flip(arr, curr_size-1); } } return 0; } static void flip(int arr[], int i) { int temp, start = 0; while (start < i) { temp = arr[start]; arr[start] = arr[i]; arr[i] = temp; start++; i--; } } static int findMax(int arr[], int n) { int mi, i; for (mi = 0, i = 0; i < n; ++i) if (arr[i] > arr[mi]) mi = i; return mi; }
for the given intial states : 2 1 3 2 , n=4 and currsize=4, [ ] this signifies the current state of elements in array.

1) maxindex=2, hence flip elements of array from (0,2) [ 3 1 2 2 ] and since currsize=4 ,again flip array elements from (0,3) , [ 2 2 1 3] and currsize=currsize-1=3;

Array : [ 2 2 1 3]

2) maxindex=0, hence flip elements of array from (0,0) [ 2 2 1 3 ] and since currsize=3 ,again flip array elements from (0,2) , [ 1 2 2 3] and currsize=currsize-1=2;

Array : [1 2 2 3] // array is sorted here itself with two flips.

3) maxindex=1, hence flip elements of array from (0,1) [ 2 1 2 3 ] and since currsize=2 ,again flip array elements from (0,1) , [1 2 2 3] and currsize=currsize-1=1;

Array : [ 1 2 2 3]

Hence the array is sorted.

Add a comment
Know the answer?
Add Answer to:
Solve the pancake sorting problem with the initial state of 2,1,3,2 and the final state of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Solve the pancake sorting problem with the initial state of 2,1,3,2 and the final state of...

    Solve the pancake sorting problem with the initial state of 2,1,3,2 and the final state of 1,2,2,3. Note that there are two pancakes of size 2 to reduce the number of states needed. The arc-cost is the number of pancakes flipped. a. Apply A* search using the heuristic h1 = the index of the largest pancake that is out of place. b. Apply A* search using the heuristic h2 = the number of pancakes out of position. c. Apply UCS...

  • Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A*...

    Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* search algorithm. 1. Objectives • To gain more experience on using pointers and linked lists in C programs. • To learn how to solve problems using state space search and A* search algorithm. 2. Background A* search and 15-puzzle problem have been introduced in the class. For more information, please read the wiki page of 15-puzzle problem at https://en.wikipedia.org/wiki/15_puzzle, and the wiki page of...

  • Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* s...

    Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* search algorithm. Please include pictures that the code runs and shows the different states as it reaches goal state please. 1. Objectives • To gain more experience on using pointers and linked lists in C programs. • To learn how to solve problems using state space search and A* search algorithm. 2. Background A* search and 15-puzzle problem have been introduced in the class....

  • Programming Language: JAVA Construct a program that uses an agent to solve a Sudoku puzzle as...

    Programming Language: JAVA Construct a program that uses an agent to solve a Sudoku puzzle as a Constraint Satisfaction Problem, with the following guidelines: 1. Since 3 x 3 puzzles are too trivial for a computer, your program should use 4 x 4 puzzles (also known as Super Sudoku puzzles; see Figure 2 for an example). 2. The program should read a Sudoku puzzle from a text file. The user should be able to browse the file system to select...

  • Playgrounds and Performance: Results Management at KaBOOM! (A) We do this work because we want to...

    Playgrounds and Performance: Results Management at KaBOOM! (A) We do this work because we want to make a difference in the world; how can we go further faster? - Darell Hammond, CEO and co-founder, KaBOOM! Darell Hammond stepped onto the elementary school playground and took a long, slow look around. It was 8 a.m. on an unusually warm fall day in 2002 and the playground was deserted, but Hammond knew the children would start arriving soon to admire their new...

  • Can Dogs Understand Human Cues? EXPLORATION Dogs have been domesticated for about 14,000 years. In that...

    Can Dogs Understand Human Cues? EXPLORATION Dogs have been domesticated for about 14,000 years. In that time, have they been able to develop an understanding of human gestures such as pointing or glancing? How about simi lar nonhuman cues? Researchers Udell, Giglio, and Wynne tested a small number of dogs in order to answer these questions. In this exploration, we wll first see whether dogs can understand human gestures as well as nonhuman gestures. To test this, the researchers positioned...

  • This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation...

    This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation inside a class. Task One common limitation of programming languages is that the built-in types are limited to smaller finite ranges of storage. For instance, the built-in int type in C++ is 4 bytes in most systems today, allowing for about 4 billion different numbers. The regular int splits this range between positive and negative numbers, but even an unsigned int (assuming 4 bytes)...

  • I have this case study to solve. i want to ask which type of case study...

    I have this case study to solve. i want to ask which type of case study in this like problem, evaluation or decision? if its decision then what are the criterias and all? Stardust Petroleum Sendirian Berhad: how to inculcate the pro-active safety culture? Farzana Quoquab, Nomahaza Mahadi, Taram Satiraksa Wan Abdullah and Jihad Mohammad Coming together is a beginning; keeping together is progress; working together is success. - Henry Ford The beginning Stardust was established in 2013 as a...

  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

  • In your judgement, and given only the facts described in this case, should the management of...

    In your judgement, and given only the facts described in this case, should the management of Massey energy Company be held morally responsible for the deaths of the 29 miners? Explain in detail. Suppose that nothing more is learned about the explosion other than what is described in this case. Do you think Don Blankership should be held morally responsible for the deaths of the 29 miners? Explain in detail. Given only the facts described in this case, should the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT