serial sort(discontinuous serial combined to start_serial end_serial array)

<?php 
printf(' memory usage: %01.2f MB', memory_get_usage()/1024/1024); 
/*
     * sortBreakProcessor
     * example:
     * $serial    =    array('ab001','ab002','ab003','ab004','ab006','ab008','ab009','ab0010');
     * $start        =    4;
     * $length        =    1;
     * return array(
                  array('ab001','ab004'),
                  array('ab006','ab006'),
                  array('ab008','ab0010'),
                 );
    copy right http:://zhimingji.com/
*/
    $serial    	=    array('ab001','ab002','ab003','ab004','ab006','ab008','ab009','ab0010');
    $start      =    4;
    $length     =    1;
	$re			=	sortBreakProcessor($serial,$start,$length);
	var_dump($re);
    function sortBreakProcessor($serial,$start,$length)
    {
 
            if(!is_array($serial))
            {
                return false;
            }
            if(!isset($serial[0]) && empty($serial[0]))
            {
                return false;
            }
 
            $prefix    			=    substr($serial[0], 0,$start);
            $val    			=    '';
            foreach ($serial as $k=>$v)
            {
                $val[$k]     	=    substr($v, $start,$length);
            }
            //print_r($val);
 
            //Bubble sort
            $val    =    paixu($val);
            //print_r($val);
 
            //get 1,4,6,6,8,10
            $resultArr[]    =    $val[0];
            foreach ($val as $k=>$v)
            {
                if($val[$k]+1<$val[$k+1])
                {
                    $resultArr[]    =    $val[$k];
                    $resultArr[]    =    $val[$k+1];
                }
            }
            $resultArr[]    =    end($val);
            //print_r($resultArr);
 
            foreach ($resultArr as $k=>$v)
            {
                if($k%2==0)
                {
                    $return[] =    array($prefix.$resultArr[$k],$prefix.$resultArr[$k+1]);
                }
            }
            return $return;
    }
 
    //Bubble sort
    function paixu($str) 
    {
        $n=count($str);
        for ($i=0;$i<$n;$i++)
        {    
            for ($j=$n-2;$j>=$i;$j--)
            {
                    if($str[$j+1]<$str[$j])
                    {   
                        $tmp = $str[$j+1];
                        $str[$j+1]=$str[$j];
                        $str[$j]=$tmp;
                    }
 
            }
 
        }
        return $str;
    }

Related posts:

Leave a Reply

Your email address will not be published.