Point-free download: http://download.csdn.net/detail/php_fly/4660704
Dictionary sortingAlgorithmSteps for full arrangement:
Summary:
1. Find the first element that is smaller than the next one from the right to the left, write down the position, and mark it as the left element.
2. Find the first element greater than the left element from the right to the left, write down the position, and mark it as the right element.
3. Swap the left and right elements.
4. The sequence on the right of the left element is reversed no matter who is on the left element.
5. A new number is obtained.
6. You can continue to repeat 1-5 to continue to get the next arrangement.
7. If you can no longer find a place smaller than the next element, it means that the sequence has been sorted in descending order and the arrangement is complete.
CodeAs follows:
<? PHP /* ** Print array ** @ Param int $ num: number of elements in the array */ Function Printarr ( $ Num ){ Global $ Array ; // Global Array For ( $ I = 0; $ I < $ Num ; $ I ++ ) Echo $ Array [ $ I ]; Echo "<Br>" ;} /* ** Exchange value ** @ Param string $ A * @ Param string $ B */ Function Swap (& $ ,& $ B ){ $ Temp = $ ; $ = $ B ; $ B = $ Temp ;} /* ** Sort the data between $ m and $ N in inverted order ** @ Param int $ m the order $ M * @ Param int $ N in the array $ n */ Function Convert ( $ M , $ N ){ Global $ Array ; // Global Array For ( $ I = $ M ,$ J = $ N ; $ J > $ I ; $ I ++, $ J -- ) Swap ( $ Array [ $ I ], $ Array [ $ J ]);} /* ** To 1 ~ N in full order ** @ Param int $ num total number of elements * @ return 1 */ Function Dictionary_sort ( $ Num ){ Global $ Array ; // Global Array If ( $ Num = 1 ){ Echo "1 <br>" ; Return 1 ;} While (1 ) {Printarr ( $ Num ); // Print Array For ( $ I = $ Num -2; $ I > = 0; $ I --){ // Step 1: Find the first element that is smaller than the next one, write down the position, and Mark $ I If ( $ Array [$ I ] < $ Array [ $ I + 1]) Break ; // Get $ I If ( $ I = 0) Return 1; // Function exit } For ( $ J = $ Num -1; $ J > $ I ; $ J --){ // Step 2: Find the first element larger than $ I from the back and forward, and mark it as $ J. If ( $ Array [ $ J ]> $ Array [ $ I ]) Break ;} Swap ( $ Array [ $ I ], $ Array [ $ J ]); // Step 3: Exchange data for $ array [$ I] and $ array [$ J] Convert ( $ I + 1, $ Num -1 ); // Step 4: sort the sequence on the Right of $ I elements in reverse order }} $ Array = Array (); $ Num = 5 ; For ( $ I = 0; $ I < $ Num ; $ I ++ ){ $ Array [ $ I ] = $ I + 1 ;} Dictionary_sort ( $ Num );
OriginalArticle:Web Development
Reprinted please indicate the source: http://www.cnblogs.com/hongfei/archive/2012/10/18/2719680.html