Programming beauty One of the notes stacks of pancakes in the sort of

Source: Internet
Author: User

One of the stacks of pancakes in the Book of Programming beauty
Here can not be sorted by the basic sorting method, then the most direct method is to find the largest number of N, this is two times the flip to the bottom, and then processing n-1,n-2, etc., until all sorts, so there is a total need to Exchange 2 (N-1) times

void Reverse(intCakes[],intBegintEnd) {intTemp while(Beg < end)        {temp = Cakes[beg];        cakes[beg++] = Cakes[end];    cakes[end--] = temp; }}voidCake_sort (intCakes[],intN) {intITH, MAX_IDX, Cur_max, IDX; for(ith=n-1; ith>=1; --ith) {Cur_max = cakes[0]; Max_idx =0;//aim to find the biggest pie of the present         for(idx=1; idx<=ith; ++IDX) {if(Cakes[idx] > Cur_max)                {Cur_max = Cakes[idx];            Max_idx = idx; }        }if(Max_idx! = ith) {Reverse(Cakes,0, MAX_IDX);Reverse(Cakes,0, ith); }     }}

Programming beauty One of the notes stacks of pancakes in the sort of

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.