Php classic algorithm highlights

Source: Internet
Author: User
This article mainly introduces the classic php algorithm. It provides examples of algorithm techniques such as Tower of parallelism, sorting, search, and recursion, and has some reference value, you can refer to the examples in this article to illustrate several typical php Algorithms. We will share this with you for your reference. The details are as follows:

Five people have stolen a bunch of apples and are going to split them in the next day. In the evening, one person walked out and divided all the fruits into five portions, but the other one threw it to the monkey on the tree, and first hid it for 1/5. I didn't expect the other four people to think so too. they all split the first person into five copies and threw the other one to the monkey and stole 1/5. The next day, we split the stolen goods into five portions and threw one more to the monkey. The last person has a copy. Q: How many apples are there?

For ($ I = 1; $ I ++) {if ($ I % 5 = 1) {// 1/5 for the first person, $ t = $ I-round ($ I/5)-1; if ($ t % 5 = 1) {// The second person gets 1/5, $ r = $ t-round ($ t/5)-1; if ($ r % 5 = 1) {// The third person gets 1/5, $ s = $ r-round ($ r/5)-1; if ($ s % 5 = 1) {// The fourth person gets 1/5, $ x = $ s-round ($ s/5)-1; if ($ x % 5 = 1) {// The fifth person gets 1/5, $ y = $ x-round ($ x/5)-1; if ($ y % 5 = 1) {echo $ I; break ;}}}}}}}

A group of monkeys are arranged in a circle ,..., N is numbered in sequence. Then start counting from 1st, count to m, kick it out of the circle, start counting from behind it, count to m, and kick it out ..., The monkey is called the King until there is only one monkey. Programming to simulate this process, input m, n, and output the number of the last king.

function king($n, $m){  $monkeys = range(1, $n);  $i=0;  $k=$n;  while (count($monkeys)>1) {    if(($i+1)%$m==0) {      unset($monkeys[$i]);    } else {      array_push($monkeys,$monkeys[$i]);      unset($monkeys[$i]);    }    $i++;  }  return current($monkeys);}$a = king(5, 2);var_dump($a);

The Hanoi Tower issue is an ancient legend of India. Brama, the God of Heaven and Earth, left three diamond rods in a temple. The first one was covered with 64 gold slices. the biggest one was under, and the other one was smaller than one, stack them one by one, and the monks in the temple move them one by one from the stick to the other. The Middle stick can be used as help, but only one can be moved at a time, and the big ones cannot be placed on the small ones. For the answer results, run the computation on your own. for the program, see the end. In the face of a huge number (the number of mobile wafers) of 18446744073709551615, it seems that the monks have exhausted their life's energy and it is impossible to complete the movement of gold tablets.

Later, this legend evolved into a tower of Legends game:

1. there are three poles A, B, and C. There are several dishes on Pole.
2. move a dish each time, and the small one can only be stacked on the big one.
3. move all the dishes from pole A to pole C.

After research, it is found that the Tower of Hanoi is very simple to crack, that is, to move gold tablets in one direction according to the mobile rules:
For example, A → C, A → B, C → B, A → C, B → A, B → A, B → C, A → C

In addition, the Tower of Hanoi problem is also a classic recursive problem in programming.

function hanoi($n,$x,$y,$z){  if($n==1){    echo 'move disk 1 from '.$x.' to '.$z."\n";  }else{    hanoi($n-1,$x,$z,$y);    echo 'move disk '.$n.' from '.$x.' to '.$z."\n";    hanoi($n-1,$y,$x,$z);  }   }hanoi(3,'A','B','C');

Use PHP to describe bubble sorting and quick sorting algorithms. the object can be an array.

// Function bubble_sort ($ array) {$ count = count ($ array); if ($ count <= 0) return false; for ($ I = 0; $ I <$ count; $ I ++) {for ($ j = $ count-1; $ j> $ I; $ j -) {if ($ array [$ j] <$ array [$ j-1]) {$ tmp = $ array [$ j]; $ array [$ j] = $ array [$ j-1]; $ array [$ j-1] = $ tmp ;}}return $ array;} function quick_sort ($ array) {if (count ($ array) <= 1) return $ array; $ key = $ array [0]; $ left_arr = array (); $ right_arr = array (); for ($ I = 1; $ I
 
  

Using PHP to describe sequential search and binary search algorithms, efficiency must be taken into account for sequential search. an object can be an ordered array.

// Use binary search for an element in the array function bin_sch ($ array, $ low, $ high, $ k) {if ($ low <= $ high) {$ mid = intval ($ low + $ high)/2); if ($ array [$ mid] ==$ k) {return $ mid ;} elseif ($ k <$ array [$ mid]) {return bin_sch ($ array, $ low, $ mid-1, $ k);} else {return bin_sch ($ array, $ mid + 1, $ high, $ k) ;}} return-1 ;}

Write a two-dimensional array sorting algorithm function that can call the php built-in function and be Universal

function array_sort($arr, $keys, $order=0) {  if (!is_array($arr)) {    return false;  }  $keysvalue = array();  foreach($arr as $key => $val) {    $keysvalue[$key] = $val[$keys];  }  if($order == 0){    asort($keysvalue);  }else {    arsort($keysvalue);  }  reset($keysvalue);  foreach($keysvalue as $key => $vals) {    $keysort[$key] = $key;  }  $new_array = array();  foreach($keysort as $key => $val) {    $new_array[$key] = $arr[$val];  }  return $new_array;}

I hope this article will help you with PHP programming.

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.