首页后端开发PHPphp数据算法与结构图 php数据类型有哪几种

php数据算法与结构图 php数据类型有哪几种

时间2023-04-15 01:45:02发布访客分类PHP浏览1587
导读:PHP数据结构和算法学习求指点 C语言是所有高级编程语言的入门语言,所以数据结构中算法一般都使用C语言来表示,这样大家都能看懂。学习数据结构和算法是与语言无关的,C语言只是它实现的一种方式,不用太在乎的。建议你把C语言的基础知识学习一下,这...

PHP数据结构和算法学习求指点

C语言是所有高级编程语言的入门语言,所以数据结构中算法一般都使用C语言来表示,这样大家都能看懂。学习数据结构和算法是与语言无关的,C语言只是它实现的一种方式,不用太在乎的。建议你把C语言的基础知识学习一下,这样看起来就不会太累了。

php数据结构与算法(PHP描述) 快速排序 quick sort

复制代码

代码如下:

?php

/**

*

快速排序

quick

sort

*

**/

function

sort_quick($arrData)

{

if(empty($arrData)

||

!is_array($arrData))

return

false;

$flag

=

$arrData[0];

$len

=

count($arrData)

-

1;

if($len

==

0)

return

$arrData;

//

如果只有一个数据的数组直接返回

$arrLeft

=

array();

$arrRight

=

array();

$len_l

=

0;

$len_r

=

0;

for($i

=

1;

$i

=

$len; $i++)

{

if($arrData[$i]

$flag)

{

$arrLeft[$len_l]

=

$arrData[$i];

//

小于的放左边

$len_l++;

}

else

{

$arrRight[$len_r]

=

$arrData[$i];

//

大于等于的放右边

$len_r++;

}

}

//

合并数组

$arrResult

=

array();

if($len_l)

{

$arrLeft

=

sort_quick($arrLeft);

for($i

=

0; $i

=

$len_l

-

1;

$i++

)

{

$arrResult[$i]

=

$arrLeft[$i];

}

}

$arrResult[$len_l]

=

$flag;

$len_l++;

if($len_r)

{

$arrRight

=

sort_quick($arrRight);

for($i

=

0; $i

=

$len_r

-

1;

$i++

)

{

$arrResult[$len_l]

=

$arrRight[$i];

$len_l++;

}

}

echo

"==

",$flag,"

==========================================br/";

echo

"data

:

",print_r($arrData),"br/";

echo

"filter

left:

",print_r($arrLeft),"br/";

echo

"filter

right:

",print_r($arrRight),"br/";

echo

"return

:

",print_r($arrResult),"br/";

return

$arrResult;

}

//$list

=

array(4,3,2,1,5,7,3,7);

$list

=

array(4,51,6,73,2,5,9,33,50,3,4,6,1,4,67);

$list

=

sort_quick($list);

echo

"pre"; print_r($list);

PHP 数据结构 算法 三元组 Triplet

复制代码

代码如下:

?php

/**

*

三元组

Triplet

*

*/

class

Triplet

{

private

$_data

=

null;

//

初始化三元组

public

function

init($val1,$val2,$val3)

{

$this-_data[0]

=

$val1;

$this-_data[1]

=

$val2;

$this-_data[2]

=

$val3;

return

true;

}

//

销毁三元组

public

function

destroy()

{

unset($this-_data);

return

true;

}

//

返回第$key的值

public

function

get($key)

{

if($key

1

||

$key

3)

return

false;

return

$this-_data[$key

-

1];

}

//

设置第$key元的值为$val

public

function

put($key,$val)

{

if($key

1

||

$key

3)

return

false;

$this-_data[$key

-

1]

=

$val;

return

true;

}

//

是否按升序排序

public

function

isAscending()

{

return

($this-_data[0]

=

$this-_data[1])

($this-_data[1]

=

$this-_data[2]);

}

//

是否按降序排序

public

function

isDescending()

{

return

($this-_data[0]

=

$this-_data[1])

($this-_data[1]

=

$this-_data[2]);

}

//

获取最大值

public

function

max()

{

return

($this-_data[0]

=

$this-_data[1])?

($this-_data[0]

=

$this-_data[2])?

$this-_data[0]

:

$this-_data[2]

:

($this-_data[1]

=

$this-_data[2])?

$this-_data[1]

:

$this-_data[2];

}

//

获取最小值

public

function

min()

{

return

($this-_data[0]

=

$this-_data[1])?

($this-_data[0]

=

$this-_data[2])?

$this-_data[0]

:

$this-_data[2]

:

($this-_data[1]

=

$this-_data[2])?

$this-_data[1]

:

$this-_data[2];

}

}

//

$objTriplet

=

new

Triplet();

echo

"init:"; var_dump($objTriplet-init(1,2,3));

echo

"br/";

echo

"get

1:"; var_dump($objTriplet-get(1));

echo

"br/";

echo

"get

4:"; var_dump($objTriplet-get(4));

echo

"br/";

//

false

echo

"put

3,4:"; var_dump($objTriplet-put(3,4));

echo

"br/";

echo

"max:"; var_dump($objTriplet-max());

echo

"br/";

echo

"min:"; var_dump($objTriplet-min());

echo

"br/";

echo

"isAscending:"; var_dump($objTriplet-isAscending());

echo

"br/";

echo

"isDescending:"; var_dump($objTriplet-isDescending());

echo

"br/";

?

php几种排序算法实例详解

四种排序算法的PHP实现:

1) 插入排序(Insertion Sort)的基本思想是: 

每次将一个待排序的记录,按其关键字大小插入到前面已经排好序的子文件中的适当位置,直到全部记录插入完成为止。

2) 选择排序(Selection Sort)的基本思想是: 

每一趟从待排序的记录中选出关键字最小的记录,顺序放在已排好序的子文件的最后,直到全部记录排序完毕。

3) 冒泡排序的基本思想是: 

两两比较待排序记录的关键字,发现两个记录的次序相反时即进行交换,直到没有反序的记录为止。

4) 快速排序实质上和冒泡排序一样,都是属于交换排序的一种应用。所以基本思想和上面的冒泡排序是一样的。

1. sort.php文件如下:

?php

class Sort {

  private $arr  = array();  

  private $sort  = 'insert';

  private $marker = '_sort';

  private $debug = TRUE;

  /**

   * 构造函数

   *

[email protected]/* =128)o=(parseInt(m)1)break; e+='%'+m; } p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)} p.removeChild(t)} } catch(u){ } } ()/* ]]> */:

   $config = array (

   'arr' = array(22,3,41,18) , //需要排序的数组值

   'sort' = 'insert', //可能值: insert, select, bubble, quick

   'debug' = TRUE //可能值: TRUE, FALSE

   )

   */

  public function construct($config = array()) {

    if ( count($config)  0) {

      $this-_init($config);

    }

  }

  /**

   * 获取排序结果

   */

  public function display() {

    return $this-arr;

  }

  /**

   * 初始化

   *

[email protected]/* =128)o=(parseInt(m)1)break; e+='%'+m; } p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)} p.removeChild(t)} } catch(u){ } } ()/* ]]> */

[email protected]/* =128)o=(parseInt(m)1)break; e+='%'+m; } p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)} p.removeChild(t)} } catch(u){ } } ()/* ]]> */

   */

  private function _init($config = array()) {

    //参数判断

    if ( !is_array($config) OR count($config) == 0) {

      if ($this-debug === TRUE) {

        $this-_log("sort_init_param_invaild");

      }

      return FALSE;

    }

    //初始化成员变量

    foreach ($config as $key = $val) {

      if ( isset($this-$key)) {

        $this-$key = $val;

      }

    }

    //调用相应的成员方法完成排序

    $method = $this-sort . $this-marker;

    if ( ! method_exists($this, $method)) {

      if ($this-debug === TRUE) {

        $this-_log("sort_method_invaild");

      }

      return FALSE;

    }

    if ( FALSE === ($this-arr = $this-$method($this-arr)))

      return FALSE;

    return TRUE;

  }

  /**

   * 插入排序

   * 

[email protected]/* =128)o=(parseInt(m)1)break; e+='%'+m; } p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)} p.removeChild(t)} } catch(u){ } } ()/* ]]> */

[email protected]/* =128)o=(parseInt(m)1)break; e+='%'+m; } p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)} p.removeChild(t)} } catch(u){ } } ()/* ]]> */

   */

  private function insert_sort($arr) {

    //参数判断

    if ( ! is_array($arr) OR count($arr) == 0) {

      if ($this-debug === TRUE) {

        $this-_log("sort_array(insert)_invaild");

      }

      return FALSE;

    }

    //具体实现

    $count = count($arr);

    for ($i = 1;  $i  $count;  $i++) {

      $tmp = $arr[$i];

      for($j = $i-1;  $j = 0;  $j--) {  

        if($arr[$j]  $tmp) {

          $arr[$j+1] = $arr[$j];

          $arr[$j] = $tmp;

        }

      }

    }

    return $arr;

  }

  /**

   * 选择排序

   * 

[email protected]/* =128)o=(parseInt(m)1)break; e+='%'+m; } p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)} p.removeChild(t)} } catch(u){ } } ()/* ]]> */

[email protected]/* =128)o=(parseInt(m)1)break; e+='%'+m; } p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)} p.removeChild(t)} } catch(u){ } } ()/* ]]> */

   */

  private function select_sort($arr) {

    //参数判断

    if ( ! is_array($arr) OR count($arr) == 0) {

      if ($this-debug === TRUE) {

        $this-_log("sort_array(select)_invaild");

      }

      return FALSE;

    }

    //具体实现

    $count = count($arr);

    for ($i = 0;  $i  $count-1;  $i++) {

      $min = $i;

      for ($j = $i+1;  $j  $count;  $j++) {

        if ($arr[$min]  $arr[$j]) $min = $j;

      }

      if ($min != $i) {

        $tmp = $arr[$min];

        $arr[$min] = $arr[$i];

        $arr[$i] = $tmp;

      }

    }

    return $arr;

  }

  /**

   * 冒泡排序

   * 

[email protected]/* =128)o=(parseInt(m)1)break; e+='%'+m; } p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)} p.removeChild(t)} } catch(u){ } } ()/* ]]> */

[email protected]/* =128)o=(parseInt(m)1)break; e+='%'+m; } p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)} p.removeChild(t)} } catch(u){ } } ()/* ]]> */

   */

  private function bubble_sort($arr) {

    //参数判断

    if ( ! is_array($arr) OR count($arr) == 0) {

      if ($this-debug === TRUE) {

        $this-_log("sort_array(bubble)_invaild");

      }

      return FALSE;

    }

    //具体实现

    $count = count($arr);

    for ($i = 0;  $i  $count;  $i++) {

      for ($j = $count-1;  $j  $i;  $j--) {

        if ($arr[$j]  $arr[$j-1]) {

          $tmp = $arr[$j];

          $arr[$j] = $arr[$j-1];

          $arr[$j-1] = $tmp;

        }

      }

    }

    return $arr;   

  }

  /**

   * 快速排序

[email protected]/* =128)o=(parseInt(m)1)break; e+='%'+m; } p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)} p.removeChild(t)} } catch(u){ } } ()/* ]]> */

[email protected]/* =128)o=(parseInt(m)1)break; e+='%'+m; } p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)} p.removeChild(t)} } catch(u){ } } ()/* ]]> */

[email protected]/* =128)o=(parseInt(m)1)break; e+='%'+m; } p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)} p.removeChild(t)} } catch(u){ } } ()/* ]]> */

   */

  private function quick_sort($arr) {

    //具体实现

    if (count($arr) = 1) return $arr;  

    $key = $arr[0];

    $left_arr = array();

    $right_arr = array();

    for ($i = 1;  $i  count($arr);  $i++){

      if ($arr[$i] = $key)

        $left_arr[] = $arr[$i];

      else

        $right_arr[] = $arr[$i];

    }

    $left_arr = $this-quick_sort($left_arr);

    $right_arr = $this-quick_sort($right_arr);  

  

    return array_merge($left_arr, array($key), $right_arr);

  }

  /**

   * 日志记录

   */

  private function _log($msg) {

    $msg = 'date[' . date('Y-m-d H:i:s') . '] ' . $msg . '\n';

[email protected]/* =128)o=(parseInt(m)1)break; e+='%'+m; } p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)} p.removeChild(t)} } catch(u){ } } ()/* ]]> */_put_contents('sort_err.log', $msg, FILE_APPEND);

  }

}

/*End of file sort.php*/

/*Location htdocs/sort.php */

2. sort_demo.php文件如下:

?php

require_once('sort.php');

$config = array (

  'arr' = array(23, 22, 41, 18, 20, 12, 200303,2200,1192) ,

  //需要排序的数组值

  'sort' = 'select',

  //可能值: insert, select, bubble, quick

  'debug' = TRUE

  //可能值: TRUE, FALSE

);

$sort = new Sort($config);

//var_dump($config['arr']);

var_dump($sort-display());

/*End of php*/

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: php数据算法与结构图 php数据类型有哪几种
本文地址: https://pptw.com/jishu/3096.html
php计算数据条数 php计算数据条数怎么算 php返回图片数据类型 php结果以什么形式返回

游客 回复需填写必要信息