Common PHP algorithms and data structure examples (required), data structure examples
Example:
</Pre> <pre name = "code" class = "php"> <? Php/*** Created by PhpStorm. * User: qishou * Date: 15-8-2 * Time: am */header ("content-type: text/html; charset = UTF-8 "); $ arr = array (, 2); echo implode ("", $ arr ). "<br/>"; // sort // common sorting algorithms // ------------------------------------------- // Bubble Sorting function BubbleSort ($ arr) {$ length = count ($ arr ); if ($ length <= 1) {return $ arr;} for ($ I = 0; $ I <$ length; $ I ++) {for ($ j = $ l Ength-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 sorting function QSort ($ arr) {$ length = count ($ arr); if ($ length <= 1) {return $ arr ;}$ outputs = $ 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] <= $ detail) {$ left_arr [] = $ Arr [$ I];} else {$ right_arr [] = $ arr [$ I] ;}}$ left_arr = QSort ($ left_arr ); // recursive sorting of the left half $ right_arr = QSort ($ right_arr); // recursive sorting of the right half return array_merge ($ left_arr, array ($ weight), $ right_arr ); // merge the left half, pivot, and right half} echo "quick sorting:"; echo implode ('', QSort ($ arr )). "<br/>"; // 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 sorting:"; echo implode ('', SelectSort ($ arr )). "<br/>"; // insert sorting 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 order: '; echo implode (' ', InsertSort ($ arr )). "<br/>"; // examples // common search algorithms // --------------------------------------------- // binary search function binary_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 "Location of {$ key} in Binary Search:"; echo bina Ry_search (QSort ($ arr), $ key); // sequential search 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/> location of {$ key} in regular order :"; echo SqSearch ($ arr, $ key); // --------------------------------------- // common data structures // linear table deletion (Array Implementation) function delete_array_element ($ arr, $ pos) {$ length = count ($ arr ); If ($ pos <1 | $ pos> $ length) {return "An error occurred while deleting the location! ";}For ($ I = $ pos-1; $ I <$ length-1; $ I ++) {$ arr [$ I] = $ arr [$ I + 1];} array_pop ($ arr); return $ arr ;}$ pos = 3; echo "<br/> after removing the element at position {$ pos}:"; echo implode ('', delete_array_element ($ arr, $ pos )). "<br/>";/*** Class Node * PHP simulates the basic operation of the linked list */class Node {public $ data = ''; public $ next = null ;} // initialize function init ($ linkList) {$ linkList-> data = 0; // records the length of the linked list $ linkList-> next = null ;} // create the function createHead (& $ linkLis T, $ length) {for ($ I = 0; $ I <$ length; $ I ++) {$ newNode = new Node (); $ newNode-> data = $ I; $ newNode-> next = $ linkList-> next; // because the object in PHP is referenced, you do not need to use "&" $ linkList-> next = $ newNode; $ linkList-> data ++ ;}} // create the 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; $ linkLis T-> data ++; }}// insert the specified function insert ($ linkList, $ pos, $ elem) in the specified position) {if ($ pos <1 & $ pos> $ linkList-> data + 1) {echo "Insertion Location 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 function delete ($ linkList, $ pos) {if ($ pos <1 & $ pos> $ linkList-> data + 1) element at the specified position) {echo "location 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 --;} // 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); // create the show ($ linkList) linked list by means of the end plug; // print the insert ($ linkList, 3, 'A') of the linked list '); // insert show ($ linkList); delete ($ linkList, 3); // delete show ($ linkList ); /* ** Class Stack * simulate basic operations on the sequence Stack with PHP */class Stack {// initialize the Stack directly with the default value, you can also use constructor to initialize the stack private $ top =-1; private $ maxSize = 5; private $ stack = array (); // public fun Ction push ($ elem) {if ($ this-> top >=$ this-> maxSize-1) {echo "Stack is full! <Br/> "; return ;}$ this-> top ++; $ this-> stack [$ this-> top] = $ elem ;} // 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 the 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-> sh Ow ();/*** Class Deque * implement bidirectional queue using PHP */class Deque {private $ queue = array (); public function addFirst ($ item) {// array_unshift ($ this-> queue, $ item);} public function addLast ($ item) {// array_push ($ this-> queue, $ item);} public function removeFirst () {// array_shift ($ this-> queue);} public function removeLast () {// 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 the Joseph Ring problem // method 1 function Joseph ph_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 the array array_push ($ arr, $ head) ;}} return $ arr [0];} // method 2 function Joseph ph_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/> ";
The above examples of common PHP algorithms and data structures (this article is required) are all the content shared by the editor. I hope you can give us a reference and support for the customer's house.