Examples of common PHP algorithms and data structures

Source: Internet
Author: User
 ";//---------------------------------------//Common sort algorithm//---------------------------------------//bubble sort function B    Ubblesort ($arr) {$length = count ($arr);    if ($length <=1) {return $arr;                } for ($i =0; $i < $length; $i + +) {for ($j = $length; $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)). "
";//fast 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 the left half, pivot, right half}echo "quick sort:"; Echo implode (", QSort ($arr))."
";//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)). "
";//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)). "
";//---------------------------------------//Common lookup algorithm//---------------------------------------//two points find function b Inary_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 finds the location of {$key}:" 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 "
Order General Lookup {$key} location: "; Echo Sqsearch ($arr, $key);//---------------------------------------//Common data structure//-------------- Delete of-------------------------///Linear table (array implementation) function 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 "
After the element in the position {$pos}: "; Echo implode (', Delete_array_element ($arr, $pos))."
";/** * 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 list $linkList->next = null;} Head interpolation creates a list of 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 creates a linked 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++; }}//inserts the specified element function insert ($linkList, $pos, $elem) at the specified location {if ($pos <1 && $pos > $linkList->data+1) {Ech o "wrong insertion position! "; } $p = $linkList; for ($i =1; $i < $pos; $i + +) {$p = $p->next;} $newNode = new Node (); $newNode->data = $elem; $newNode->next = $p->next; $p->next = $newNode;} Removes 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 '
';} $linkList = new Node (), Init ($linkList);//Initialize Createtail ($linkList, 10);//tail interpolation create linked list show ($linkList);//Print out list insert ($ linklist,3, ' a ');//Insert Show ($linkList);d elete ($linkList, 3);//delete show ($linkList);/** * Class stack * Basic operation of the sequence stack with PHP */ Class stack{//initializes the stack directly with default values, or initializes the stack private $top =-1 using the constructor method; Private $maxSize = 5; Private $stack = Array (); into the stack public function push ($elem) {if ($this->top >= $this->maxsize-1) {echo "stack is full!)
"; 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 "
"; }} $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 ();

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The above describes the PHP common algorithms and data structure examples, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • 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.