Chapter One the game of the music of a stack of pies sort of __ stack cakes

Source: Internet
Author: User

Requirements: Large In the bottom, small on top, output reversal process


/****************************************************************/

//

Pancake Sort Realization

//

/****************************************************************/

Class Cprefixsorting

{

Public

Cprefixsorting ()

{

m_ncakecnt = 0;

M_nmaxswap = 0;

}

~cprefixsorting ()

{

if (M_cakearray!= NULL)

{

Delete M_cakearray;

}

if (M_swaparray!= NULL)

{

Delete M_swaparray;

}

if (M_reversecakearray!= NULL)

{

Delete M_reversecakearray;

}

if (M_reversecakearrayswap!= NULL)

{

Delete M_reversecakearrayswap;

}

}

//

Calculate Pancake Flip Information

@param

Pcakearray Storage Pancake Index Array

Number of ncakecnt pancakes

//

Voidrun (int* pcakearray, int ncakecnt)

{

Init (Pcakearray, ncakecnt);

M_nsearch = 0;

Search (0);

}

//

The number of specific flip of the pancake

//

Voidoutput ()

{

for (int i = 0; i < M_nmaxswap; i++)

{

printf ("%d", m_arrswap[i]);

}

printf ("\ n | Search times| :%d\n ", M_nsearch);

printf ("total swaps times =%d\n", m_nmaxswap);

}

Private

//

Initializing an array of information

@param

Pcakearray Storage Pancake Index Array

Number of ncakecnt pancakes

//

Voidinit (int* pcakearray, int ncakecnt)

{

Assert (Pcakearray!= NULL);

Assert (ncakecnt > 0);

m_ncakecnt = ncakecnt;

Initialize the pancake array

M_cakearray = new INT[M_NCAKECNT];

Assert (M_cakearray!= NULL);

for (int i = 0; i < m_ncakecnt; i++)

{

M_cakearray[i] = Pcakearray[i];

}

Set Maximum exchange times information

M_nmaxswap = Upbound (m_ncakecnt);

Initializing an array of exchange results

M_swaparray = Newint[m_nmaxswap + 1];

Assert (M_swaparray!= NULL);

Initializing Intermediate Exchange results information

M_reversecakearray = new INT[M_NCAKECNT];

for (i = 0; i < m_ncakecnt; i++)

{

M_reversecakearray[i] = M_cakearray[i];

}

M_reversecakearrayswap = new Int[m_nmaxswap];

}

//

Find the upper bound of the current flip

//

//

Intupbound (int ncakecnt)

{

return ncakecnt*2;

}

//

Find the lower bound of the current flip

//

//

Intlowerbound (int* pcakearray, int ncakecnt)

{

Intt, ret = 0;

Determine the minimum number of times to swap based on the current array's ordering information

for (int i = 1; i < ncakecnt; i++)

{

Judge the position adjacent to the two pancakes, is the size of the sort on the adjacent

t = pcakearray[i]-pcakearray[i-1];

if ((t = = 1) | | (t = = 1))

{

}

Else

{

ret++;

}

}

return ret;

}

Sort the main function

Voidsearch (int step)

{

Inti, Nestimate;

m_nsearch++;

Estimate the minimum number of exchanges needed for this search

Nestimate = Lowerbound (M_reversecakearray, m_ncakecnt);

if (step + nestimate > M_nmaxswap)

Return

If the sequence has been arranged, that is, the flip is done, the output

if (issorted (M_reversecakearray, m_ncakecnt))

{

if (Step < M_nmaxswap)

{

M_nmaxswap = step;

for (i = 0; i < m_nmaxswap;i++)

M_arrswap[i] =m_reversecakearrayswap[i];

}

Return

}

Flip recursively

for (i = 1; i < m_ncakecnt; i++)

{

Revert (0, I);

M_reversecakearrayswap[step] = i;

Search (step + 1);

Revert (0, I);

}

}

//

True: The order is already sorted.

False: Not sorted

//

BOOL IsSorted (int* Pcakearray, intncakecnt)

{

for (int i = 1; i < ncakecnt; i++)

{

if (Pcakearray[i-1] >pcakearray[i])

{

return false;

}

}

return true;

}

//

Flip Pancake Info

//

void Revert (int nbegin, int nend)

{

Assert (Nend > Nbegin);

int I, j, T;

Flip Pancake Info

for (i = nbegin, j = nend; I < j;i++, j--)

{

t = M_reversecakearray[i];

M_reversecakearray[i] =m_reversecakearray[j];

M_REVERSECAKEARRAY[J] = t;

}

}

Private

int* M_cakearray; Pancake Information Array

int m_ncakecnt; Number of Pancakes

int M_nmaxswap; The maximum number of exchanges. According to the previous inference, this is the most

M_NCAKECNT * 2

int* M_swaparray; Swap results Array

int* M_reversecakearray; Current Flip Pancake Information array

int* M_reversecakearrayswap; Current Flip pancake Swap results array

int m_nsearch; Current Search count Information

};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.