Common algorithms and data structures in PHP

Source: Internet
Author: User
This article mainly introduces PHP common algorithms and data structure, interested in the small partner reference, I hope to help you.

</pre><pre name= "code" class= "PHP" ><?php/** * Created by Phpstorm.  * User:qishou * date:15-8-2 * time: Morning 9:12 */header ("Content-type:text/html;charset=utf-8");  $arr = Array (3,5,8,4,9,6,1,7,2); echo Implode ("", $arr). "  <br/> "; ---------------------------------------//Common sort algorithm//---------------------------------------//bubble sort function Bub      Blesort ($arr) {$length = count ($arr);      if ($length <=1) {return $arr;                  } for ($i =0; $i < $length; $i + +) {for ($j = $length-1; $j > $i; $j-) {if ($arr [$j]< $arr [$j-1]) {                  $tmp = $arr [$j];                  $arr [$j] = $arr [$j-1];              $arr [$j-1] = $tmp;  }}} return $arr;  } echo ' bubble sort: '; Echo Implode (", Bubblesort ($arr))."  <br/> ";      Quick Sort function QSort ($arr) {$length = count ($arr);      if ($length <=1) {return $arr;      } $pivot = $arr [0];//pivot $left _arr = array (); $right _arr = ARray ();          for ($i =1; $i < $length; $i + +) {//note $i starting from 1 0 is the pivot if ($arr [$i]<= $pivot) {$left _arr[] = $arr [$i];          }else{$right _arr[] = $arr [$i]; }} $left _arr = QSort ($left _arr);//recursive sort left half $right _arr = QSort ($right _arr);//recursive sort right half return Array_merge ($  Left_arr,array ($pivot), $right _arr);//merge left half, Pivot, right half part} echo "Quick sort:"; Echo Implode (", QSort ($arr))."  <br/> ";      Select sort (unstable) function Selectsort ($arr) {$length = count ($arr);      if ($length <=1) {return $arr;          } for ($i =0; $i < $length; $i + +) {$min = $i;              for ($j = $i +1; $j < $length; $j + +) {if ($arr [$j]< $arr [$min]) {$min = $j;              }} if ($i! = $min) {$tmp = $arr [$i];              $arr [$i] = $arr [$min];          $arr [$min] = $tmp;  }} return $arr;  } echo "select Sort:"; Echo Implode (", Selectsort ($arr))."  <br/> ";  Insert Sort function Insertsort ($arr) {    $length = count ($arr);      if ($length <=1) {return $arr;          } for ($i =1; $i < $length; $i + +) {$x = $arr [$i];          $j = $i-1;              while ($x < $arr [$j] && $j >=0) {$arr [$j +1] = $arr [$j];          $j--;      } $arr [$j +1] = $x;  } return $arr;  } echo ' Insert sort: '; Echo Implode (", Insertsort ($arr))."  <br/> "; ---------------------------------------//Common find algorithm//---------------------------------------//Two points find function bin          Ary_search ($arr, $low, $high, $key) {while ($low <= $high) {$mid = Intval (($low + $high)/2);          if ($key = = $arr [$mid]) {return $mid +1;          }elseif ($key < $arr [$mid]) {$high = $mid-1;          }elseif ($key > $arr [$mid]) {$low = $mid +1;  }} return-1;  } $key = 6;  echo "binary find {$key} location:";  Echo Binary_search (QSort ($arr), 0,8, $key);    Order Lookup Function Sqsearch ($arr, $key) {$length = count ($arr);  for ($i =0; $i < $length; $i + +) {if ($key = = $arr [$i]) {return $i +1;  }} return-1;  } $key = 8;  echo "<br/> Order general find {$key} location:";  Echo Sqsearch ($arr, $key); ---------------------------------------//Common data structure//---------------------------------------///Linear table deletion (array implementation) Func      tion Delete_array_element ($arr, $pos) {$length = count ($arr);      if ($pos <1 | | $pos > $length) {return "Error deleting location!";      } for ($i = $pos-1; $i < $length-1; $i + +) {$arr [$i] = $arr [$i +1];      } array_pop ($arr);  return $arr;  } $pos = 3;  echo "<br/> In addition to the element {$pos} at position:"; Echo Implode (", Delete_array_element ($arr, $pos))."  <br/> ";      /** * Class Node * PHP analog List of basic operations */class node{public $data = ";  public $next = null;  }//Initialize function init ($linkList) {$linkList->data = 0;//to record the length of the linked list $linkList->next = null;    }//Head interpolation create List function Createhead (& $linkList, $length) {for ($i =0; $i < $length; $i + +) {      $newNode = new Node ();          $newNode->data = $i;          $newNode->next = $linkList->next;//Because the object itself in PHP is a reference so no longer available "&" $linkList->next = $newNode;      $linkList->data++;      }}//tail interpolation to create a list function Createtail (& $linkList, $length) {$r = $linkList;          for ($i =0; $i < $length; $i + +) {$newNode = new Node ();          $newNode->data = $i;          $newNode->next = $r->next;          $r->next = $newNode;          $r = $newNode;      $linkList->data++;          }}//insert the specified element function insert ($linkList, $pos, $elem) at the specified location {if ($pos <1 && $pos > $linkList->data+1) { echo "Insertion position Error!      ";      } $p = $linkList;      for ($i =1; $i < $pos; $i + +) {$p = $p->next;      } $newNode = new Node ();      $newNode->data = $elem;      $newNode->next = $p->next;  $p->next = $newNode; }//Delete the element at the specified location function delete ($linkList, $pos) {if ($pos <1 && $pos > $linkList->data+1{echo "position does not exist!)      ";      } $p = $linkList;      for ($i =1; $i < $pos; $i + +) {$p = $p->next;      } $q = $p->next;      $p->next = $q->next;      Unset ($q);  $linkList->data--;      }//Output list data function Show ($linkList) {$p = $linkList->next; while ($p!=null) {echo $p->data. "          ";      $p = $p->next;  } echo ' <br/> ';  } $linkList = new Node (); Init ($linkList);//Initialize Createtail ($linkList, 10);//tail interpolation to create a linked list show ($linkList);//Print out the list insert ($linkList, 3, ' a ');//Insert Show  ($linkList);  Delete ($linkList, 3);//Remove show ($linkList);      /** * Class stack * uses PHP to simulate the basic operation of the sequence stack */class stack{//with default values to initialize the stack directly, you can also use the constructor method to initialize the stack private $top =-1;      Private $maxSize = 5;      Private $stack = Array (); into the stack public function push ($elem) {if ($this->top >= $this->maxsize-1) {echo "stack is full!)              <br/> ";          Return          } $this->top++;      $this->stack[$this->top] = $elem;}//out-Stack public function pop () {if ($this->top = =-1) {echo "stack is empty!)              ";          return;          } $elem = $this->stack[$this->top];          unset ($this->stack[$this->top]);          $this->top--;      return $elem; }//Print stack public function show () {for ($i = $this->top; $i >=0; $i-) {echo $this->stack[$i ]."          ";      } echo "<br/>";  }} $stack = new stack ();  $stack->push (3);  $stack->push (5);  $stack->push (8);  $stack->push (7);  $stack->push (9);  $stack->push (2);  $stack->show ();  $stack->pop ();  $stack->pop ();  $stack->pop ();  $stack->show ();      /** * Class Deque * Use PHP to implement bidirectional queue */class deque{Private $queue = Array ();      Public Function AddFirst ($item) {//Head queue Array_unshift ($this->queue, $item);      } Public Function AddLast ($item) {//Tail Array_push ($this->queue, $item); } Public Function removeFirst () {//head-out Team Array_shift ($this->queue);      } public Function Removelast () {//Tail out Team Array_pop ($this->queue); Public function Show () {//print foreach ($this->queue as $item) {echo $item.          ";      } echo "<br/>";  }} $deque = new deque ();  $deque->addfirst (2);  $deque->addlast (3);  $deque->addlast (4);  $deque->addfirst (5);  $deque->show ();      PHP solves Joseph Ring Problem//Method one function joseph_ring ($n, $m) {$arr = range (1, $n);      $i = 0;          while (count ($arr) >1) {$i = $i +1;          $head = Array_shift ($arr);          if ($i% $m! = 0) {//If not re-press into the array array_push ($arr, $head);  }} return $arr [0];      }//Method two function Joseph_ring2 ($n, $m) {$r = 0;      for ($i =2; $i <= $n; $i + +) {$r = ($r + $m)% $i;  } return $r + 1; } echo "<br/>". Joseph_ring (60,5). "  <br/> "; echo "<br/>". Joseph_ring2 (60,5). " <br/> ";

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.