Background:
These days to play a bit more, code knock less, today to fill the training, by the way the first two days to do a problem also affixed.
Business:
1. Merge sort
Concept (source Baidu Encyclopedia): Merge sort is an effective sorting algorithm based on merging operation, which is a very typical application of the method of divide and conquer (Divide and Conquer). The ordered Subsequence is merged to obtain a fully ordered sequence, i.e., the order of each subsequence is ordered, and then the sequence of sub-sequences is ordered. If two ordered tables are combined into an ordered table, they are called two-way merging.
My understanding, it is very simple, is to group an unordered array, 22 groups for comparison, the number of elements of the group is 2^i (i=0,1,2,..., N) growth. Until finally there is only one fully ordered group.
Code (feeling written in a very excrement):
<?PHPfunctionMerging_pass ($list,$i){ //set the start position of the second group $j=$i; //set the first group start position $k= 0; //set the new array subscript $start=$k; //exit when a group is found to have no matching group while($list[$j] !=NULL) { //set the end of each of the two groups in a merge $j _end=$j+$i; $k _end=$k+$i; while($j<$j _end&&$k<$k _end) { if($list[$j] ===NULL||$list[$i] ===NULL) { Break; } if($list[$j] <$list[$k]) { $new _list[$start++] =$list[$j++]; } Else { $new _list[$start++] =$list[$k++]; } } while($j<$j _end&&$list[$j] !=NULL) { $new _list[$start++] =$list[$j++]; } while($k<$k _end&&$list[$k] !=NULL) { $new _list[$start++] =$list[$k++]; } $k=$j; $j+=$i; } //If there is a separate group, copy directly to the new array while($list[$k] !=NULL) { $new _list[$start++] =$list[$k++]; } Print_r($new _list);Echo"<br>"; return $new _list;}functionMerging_sort (&$list){ $count=Count($list); $i= 1; while($i<$count) { $list= Merging_pass ($list,$i); $i*= 2; }}//Test$list=Array(34,39,31,20,50,10,14,28,17);Print_r($list);Echo"<br>"; Merging_sort ($list);
/*
Array ([0] = [1] [+] [2] [3] = [4] [5] = [6] [7] [8] = [+] = [+] = [+] = + 17)
Array ([0] = [1] = [2] [3] [4] = [5] = [6] [7] [8] (+/-) [+] = [+] = [+] = 17)
Array ([0] = [1] = + [2] = [3] [4] [5] [+] = [6] = [7] = [8] = 17)
Array ([0] = [1] = [2] = [3] [4] = + [5] [6] [7] [8] = [+] = [+] = [+] = 17)
Array ([0] = [1] = [2] = [3] = [4] = [5] = [6] = [7] = [8] + = 50)
*/
?>
Merge sort is a stable sort, but relative to other sorts, the spatial complexity is higher, and you need to define some start-end flags, and a new array. But time complexity and heap ordering are the same O (nlog2n). In contrast, the idea of merging sorts is better understood. So the self-feeling merge sort is a very good sort of way, but how to sort well, still depends on the character of the sequence. (Size sort problem, it's over)
2, character sorting
This is on the Internet to see the topic, since you see, then practice.
idea: We all know, the smallest sequence is abcdefghijkl, the maximum sequence: LKJIHGFEDCBA, how many kinds of hair? High school students all know that. Use the knowledge of permutations and combinations, 12! (! Represents a factorial). Take a look at Example 2, the beginning of the letter is not a, but H, then it is indicated that at least the sequence has been ranked at the beginning of the a,b,c,d,e,f,g sequence behind, then by the first we can get 7*11! (after calculating the h from the total alphabet), the second is G then think again, that this sequence at least in order to a,b,c,d,e,f, as the second position of the sequence behind, when the ranking is 7*11!+6*10! (calculated after the G from the total alphabet culling). And so on, the last sequence: Hgebkflacdji is ranked 7*11! +6*10! +4*9! +1*8! +6*7! +3*6! +5*5! +0*4! +0*3! +1*2! +0*1.
Note: each time a single letter is calculated, it is removed from the alphabet because the letter is selected and the letter cannot be selected after the sequence.
Code:
<?PHP/*gets the sequence rank. @param the sequence number given by the array and the sequence that needs to be ranked @param string basic sequence reference return array sequence rank*/functionGetranknum ($input,$example){ $time=strlen($example); $rank=Array_fill(1, 3, 1); for($j= 1;$j<=$input[0];$j++) { $tmp=$example; $start= 0; for($i=$time-1;$i>=1;$i--) { $word=substr($input[$j] ,$start+ +, 1); $r=Strpos($tmp,$word); $rank[$j] + = factorial ($i)*($r); $tmp=Str_replace("$word" , ‘‘ ,$tmp); } } return $rank;}/*calculate factorial: @param int needs to calculate the number return int factorial result*/functionFactorial ($i){ $result= 1; for(;$i>1;$i--) { $result*=$i; } return $result;}//Test$example= ' ABCDEFGHIJKL ';$input=Array(4, ' abcdefghijkl ', ' Hgebkflacdji ', ' Gfkedhjblcia ', ' abcdefghijlk ');Print_r(Getranknum ($input,$example));
Array ([1] = 1 [2] = 302715242 [3] = 260726926 [4] = 2)?>
Algorithm exercises--merge sorting, arranging questions