7 years ago a combinatorial algorithm missed the goose farm offer, and then specialized in the permutation combination algorithm, cruised into the society, most of the algorithm is not used at all. There is nothing to do, memory arrangement algorithm how to achieve.
The most important step of the algorithm-proves that it seems that the general school does not teach. The mathematical induction can be simply considered to be right.
1 intGetvalidlen (intA[],intmax)2 {3 return 6;4 }5 6 voidPrintArray (intA[],intLenintTimes )7 {8printf"%04d:", times);9 for(inti =1; I <= Len; i++)Ten { Oneprintf"%d", A[i]); A } -printf"\ n"); - } the - voidReversesort (intA[],intLenintposition) - { - intMiddle = (len + position)/2; + intEnd =Len; - intTMP =0; + for(inti = position; I <= Middle; i++) A { atTMP =A[i]; -A[i] =A[end]; -A[end] =tmp; -End-=1; - } - } in intMain () - { to inta[ -] = {0 }; + intLen = Getvalidlen (A, in); - intTimes =0; the for(inti =1; I <= Len; i++) * { $A[i] =i;Panax Notoginseng } - intloop =0; the intLOOP1 =0; + while(true) A { thetimes++; + PrintArray (A, Len, times); - for(loop = Len; loop >1; loop--) $ { $ if(A[loop] > A[loop-1]) - Break; - } the if(Loop = =1) - {Wuyiprintf"over!\n"); the Break; - } Wu //Insert Sort - for(Loop1 = len; Loop1 >= Loop; loop1--) About { $ if(A[LOOP1] > A[loop-1]) - { -a[0] = A[loop-1]; -A[loop-1] =A[LOOP1]; AA[LOOP1] = a[0]; + Break; the } - } $ //Reverse Sort the Reversesort (A, Len, loop); the } the}
View Code
Full permutation algorithm implementation