Examples of common PHP Algorithms and Data Structures

Source: Internet
Author: User
: This article mainly introduces common PHP Algorithms and Data Structure examples. For more information about PHP tutorials, see.
 "; // 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 = $ 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 sorting: '; echo implode ('', BubbleSort ($ arr ))."
"; // 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 that $ I starts from 1 and 0 is the pivot if ($ arr [$ I] <= $ scheme) {$ 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 ))."
"; // 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 ))."
"; // 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 ))."
"; // --------------------------------------- // 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 "binary query {$ key} location:"; echo binary_search (QSort ($ arr), $ key); // sequential query 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"
Location of the {$ key} in the regular order: "; echo SqSearch ($ arr, $ key ); // FUNCTIONS // common data structures // ------------------------------------------- // 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"
Except for the element at the position {$ pos}: "; echo implode ('', delete_array_element ($ arr, $ pos ))."
";/*** 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 (& $ linkList, $ 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 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 ++ ;}// insert the specified function insert ($ linkList, $ pos, $ elem) {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'
';}$ LinkList = new Node (); init ($ linkList); // initialize createTail ($ linkList, 10); // Create a linked list show ($ linkList) by means of the end plug-in ); // Print the chain table insert ($ linkList, 3, 'A'); // insert show ($ linkList); delete ($ linkList, 3 ); // delete show ($ linkList);/*** Class Stack * simulate basic operations of the sequence Stack with PHP */class Stack {// use the default value to initialize the Stack directly, you can also use constructor to initialize the stack private $ top =-1; private $ maxSize = 5; private $ stack = array (); // public function push ($ elem) of the inbound stack) {if ($ this-> top >=$ this-> maxSize-1 ){ Echo "the stack is full!
"; 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"
";}}$ 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-> show ();

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above describes common PHP Algorithms and Data Structure examples, including the content, and hope to be helpful to friends who are interested in PHP tutorials.

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.