Home > Backend Development > PHP Tutorial > Several program examples of php array sorting_PHP tutorial

Several program examples of php array sorting_PHP tutorial

WBOY
Release: 2016-07-13 16:56:31
Original
796 people have browsed it

A. Internal sorting (directly loaded into memory for sorting): including exchange sorting (bubble and quick methods), selection sorting, and insertion sorting B. External sorting (due to the large amount of data, external storage is required for sorting): including merge sort and direct merge sort

[Selection sort: The second to nth numbers are compared with the first number respectively, and then Exchange, the third to nth numbers are compared with the second number respectively, and exchange is performed until the arrangement is completed]

The code is as follows Copy code
 代码如下 复制代码

function selectSort($arr,$style)
             {
                 $temp=0;
                 for($i=0;$i {
for($j=$i+1;$j {
if($style=='bts') $op=$arr[$i]<$arr[$j];
else if($style=='stb') $op=$arr[$i]>$arr[$j];
                         if($op)
                         {
                             $temp=$arr[$i];
                             $arr[$i]=$arr[$j];
                             $arr[$j]=$temp;
                         }
                     }
                 }
                 foreach ($arr as $key => $value)
                 {
                     echo $value.',';   
                 }
             }
             $arr1=array(21.5,33,90,7,-4,5,55,11);
             selectSort($arr1,'stb');

function selectSort($arr,$style)
                                                            {
                     $temp=0;
for($i=0;$i                                                                         for($j=$i+1;$j                                                                                     {
If($style=='bts') $op=$arr[$i]<$arr[$j];
                                            else if($style=='stb') $op=$arr[$i]>$arr[$j];
If($op)
                                                                                                           $temp=$arr[$i];
$arr[$i]=$arr[$j];
$arr[$j]=$temp;
                                                                                                                                                                                                                                                                            }
                          }
                      }
foreach ($arr as $key => $value)
                                                                        echo $value.',';
                      }
              }
                 $arr1=array(21.5,33,90,7,-4,5,55,11);
                                         selectSort($arr1,'stb');

[By default, the value is passed, not the address. If you add an & before $arr, it points to the same address as $arr1, and $arr1 outside the function is also arranged]

The code is as follows Copy code
 代码如下 复制代码

function maoPao($arr,$style)    {
                 $temp=0;
                 for($i=0;$i {
for($j=0;$j {
if($style=='bts') $op=$arr[$j]<$arr[$j+1];
else if($style=='stb') $op=$arr[$j]>$arr[$j+1];
                         if($op)
                         {
                             $temp=$arr[$j];
                             $arr[$j]=$arr[$j+1];
                             $arr[$j+1]=$temp;
                         }
                     }
                 }
                 foreach ($arr as $key => $value)
                 {
                     echo $value.',';   
                 }
             }
             $arr1=array(101,101,-9,-8,0,76,1,57,43,90,23,-56);
             maoPao($arr1,'stb');//small to big

 

function maoPao($arr,$style) {
                     $temp=0;
for($i=0;$i                                                                         for($j=0;$j                                                                                     {
If($style=='bts') $op=$arr[$j]<$arr[$j+1];
                                          else if($style=='stb') $op=$arr[$j]>$arr[$j+1];
If($op)
                                                                                                                                               $temp=$arr[$j];
$arr[$j]=$arr[$j+1];
$arr[$j+1]=$temp;
                                                                                                                                                                                                                                                                            }
                          }
                      }
foreach ($arr as $key => $value)
                                                                        echo $value.',';
                      }
              }
                  $arr1=array(101,101,-9,-8,0,76,1,57,43,90,23,-56);
               maoPao($arr1,'stb');//small to big

[Insertion sort: The second one is compared with the first one and exchanged, and the third one is compared with the first two and exchanged. . . . Compare the nth one with the first n-1 and exchange]

The code is as follows
 代码如下 复制代码

function insertSort($arr,$style)
             {
                 $temp=0;
                 for($i=1;$i {
for($j=0;$j<$i;$j++)
{
if($style=='bts') $op=$arr[$j]<$arr[$i];
else if($style=='stb') $op=$arr[$j]>$arr[$i];
                         if($op)
                         {
                             $temp=$arr[$j];
                             $arr[$j]=$arr[$i];
                             $arr[$i]=$temp;
                         }
                     }
                 }
                 foreach ($arr as $key => $value)
                 {
                     echo $value.',';   
                 }
             }
             $arr1=array(4,7,0,-7,1,14,5);
             insertSort($arr1,'bts');

Copy code
function insertSort($arr,$style)
                                                            {
                     $temp=0;
for($i=1;$i                                                                         for($j=0;$j<$i;$j++)
                                                                                    {
If($style=='bts') $op=$arr[$j]<$arr[$i];
                                        else if($style=='stb') $op=$arr[$j]>$arr[$i];
If($op)
                                                                                                            {
                                    $temp=$arr[$j];
$arr[$j]=$arr[$i];
$arr[$i]=$temp;
                             }
                          }
                   }
foreach ($arr as $key => $value)
                                                                        echo $value.',';
                   }
               }
                 $arr1=array(4,7,0,-7,1,14,5);
                 insertSort($arr1,'bts');

http://www.bkjia.com/PHPjc/631612.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/631612.html
TechArticle
A. Internal sorting (directly loaded into memory for sorting): including exchange sorting (bubble and fast methods) , selective sorting, insertion sorting B. External sorting (due to the large amount of data, external sorting is required...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template