java的选择排序写到一半,突然想用php来写。开始动手的时候才发现php真的生疏了好多(捂脸,明明完全都不记得了)!开始动手的时候只好疯狂查阅手册。花了一个小时才写出来。发现php的类居然不能定义!!!先贴php代码。
<?php
/**
* Created by PhpStorm.
* User: lzh
* Date: 2018/2/10
* Time: 下午10:50
*/
class selectionSortData {
private $var = array();
/**
* selectionSortData constructor.
* @param $in
*/
public function __construct($in)
{
if (is_array($in)) {
$this->var = $in;
}
if (is_numeric($in)) {
for ($count = 0; $count < $in; $count ++) {
$random = mt_rand(1, 100);
array_push($this->var, $random);
print_r($this->var);
echo '<br />';
}
}
}
public function swap($left, $right) {
$temp = $left;
$left = $right;
$right = $temp;
}
public function sort() {
$temp = $this->var[0];
for ($i = 0; $i < count($this->var); $i ++) {
if ($this->var[$i] > $temp) {
$this->swap($temp, $this->var[$i]);
}
}
return $this->var;
}
}
$selection = new selectionSortData(10);
echo '<br />';
print_r($selection);
不留神就12点了,明天上java版的选择排序