Basic Algorithms: Personal PHP implementation (1)

Source: Internet
Author: User
<? PHP  /*  ** PHP Binary Search Method  */   $ Arr = Array (, 9 );  Function Search ( $ Arr , $ Low , $ High , $ K  ){  If ( $ High > = $ Low  ){  $ Middle = Intval (( $ High + $ Low )/2 ); //  No difference between Ceil and intval          If ( $ Arr [ $ Middle ] = $ K  ){  Return 'Found';}  Elseif ( $ Arr [ $ Midddle ]> $ K  ){  Return Search ( $ Arr , $ Low , $ Middle -1, $ K  );}  Else  {  Return Search ( $ Arr , $ Middle + 1, $ High , $ K  );}}  Return 'None' ;}  //  Echo search ($ arr, 0, count ($ ARR)-1, 3 );  /*  ** PHP Traversal  */  Function Seq_sch ( $ Array ,$ N , $ K  ){  $ Array [ $ N ] = $ K  ;  For ( $ I = 0; $ I < $ N ; $ I ++ ){  If ( $ Array [$ I ] = $ K  ){  Break  ;}}  If ( $ I < $ N  ){  Return   $ I  ;}  Else  {  Return -1;}}  //  Delete a linear table // the pointer of the value to be deleted points to the following value and then deletes the excess pointer at the end. Here, the pointer is a little too far-fetched.  Function Test ( $ Arr , $ I  ){  $ Length = Count ( $ Arr  );  For ( $ J = $ I ; $ J < $ Length ; $ J ++ ){  $ Arr [ $ J ] = $ Arr [ $ J + 1 ];}  Array_pop ( $ Arr  );  Return   $ Arr  ;}  // Print_r (test ($ arr, 1); // bubble sort  $ Arr = Array (3, 2, 8, 1, 4 );  $ N = Count ( $ Arr  );  For ( $ I = 0; $ I < $ N ; $ I ++ ){ For ( $ J = $ N -1; $ J > $ I ; $ J -- ){  If ( $ Arr [ $ J ] < $ Arr [ $ J -1 ]) {  $ Temp =$ Arr [ $ J  ];  $ Arr [ $ J ] = $ Arr [ $ J -1 ];  $ Arr [ $ J -1] = $ Temp  ;}}}  Print_r ( $ Arr  ); ?>

 

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.