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