Programming a bunch of pancakes intermediate sort 1

Source: Internet
Author: User

Get this problem.

The first reaction is to use the algorithm idea to divide and conquer, each time the biggest pancake of the current to the designated place

。 The idea is simple and easy to implement. But this is just a workable solution to the problem, which is found after reading the contents of the book. The problem is that the optimization of the output process is required, and our approach obviously does not take into account the optimization of the <-_->!!

In fact, I think that even if I saw the demand for the most optimal output, I was not expected to expect the design idea in the book. After a while, wait for yourself to forget the ideas in the book. Let's see if we can think of such algorithmic thinking. This is supposed to be a pit, <-_->!!.

Here, put your own code on it:

//================ "pie problem" =================//@ author:zhyh2010//@ date:20150611//@ version:1.0//@ Description: Required to guarantee that the incoming is the ID in the real sequence. IE subscript is calculated from the beginning of 1//The program internal self-conversion into subscript for 0 start to calculate//=============== "End flipping problem" =================#include <stdio.h>#include <stdlib.h>#include <time.h>#define NUMintArr[num] = {0};intFlip_num =0;voidInit () {time_t TM;    Time (&AMP;TM); Srand ((unsigned int) tm); for(inti =0; I! = NUM; i++) Arr[i] = rand ()% -;}voidDisplay () { for(inti =0; I! = NUM; i++)printf("%5d", Arr[i]);printf("\ n");}intGetmaxpie (intN_max) {intId_max =0; for(inti =0; I! = N_max; i++) {if(Arr[id_max] < arr[i]) Id_max = i; }//require that the IDs in the real sequence be passed in. IE subscript is calculated from the beginning of 1    returnId_max +1;}voidSwapint& A,int& B) {A = a ^ b;    b = a ^ b; A = a ^ b;}voidFlipintID) {flip_num++;intLow =0;intHigh = ID-1; while(Low < High) swap (arr[low++], arr[high--]);printf("Post%d rollover result: \n\t", Flip_num); Display ();}voidFlipsort (intN) {if(n = =1)return;intid = getmaxpie (n);if(id! =1) Flip (ID);    Flip (n); Flipsort (N-1);}voidMain () {init ();printf("Original Data are:\n\t");    Display (); Flipsort (NUM);printf("total rollover%d times \ n", flip_num);}

Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

Programming a bunch of pancakes intermediate sort 1

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.