本文目录一览:
- 1、PHP查询并排序
- 2、PHP实现常见的排序算法
- 3、php几种排序算法实例详解
- 4、PHP 查询排序问题
- 5、php 数据库搜索和排序同时哪里错了,排序运行不了,搜索也不行
- 6、PHP的几种排序方法
PHP查询并排序
从你要输出的结果来看,你这样实现法不太科学,代码不高效,数据量少时还算说得过去,若上万数据的话,服务器会崩溃。建议你在数据库结构上进行改进,对于排序的字段设计可以采用人为可控制的数值,这样你要实现的话,一个sql语句搞定到数组后,你想输出哪种格式都是可以的了。你去参考下那些知名的CMS系统的数据库设计结构,我想你会受到启发的。
记得给分!!!
PHP实现常见的排序算法
注:为方便描述,下面的排序全为正序(从小到大排序)
假设有一个数组[a,b,c,d]
冒泡排序依次比较相邻的两个元素,如果前面的元素大于后面的元素,则两元素交换位置;否则,位置不变。具体步骤:
1,比较a,b这两个元素,如果ab,则交换位置,数组变为:[b,a,c,d]
2,比较a,c这两个元素,如果ac,则位置不变,数组变为:[b,a,c,d]
3,比较c,d这两个元素,如果cd,则交换位置,数组变为:[b,a,d,c]
完成第一轮比较后,可以发现最大的数c已经排(冒)在最后面了,接着再进行第二轮比较,但第二轮比较不必比较最后一个元素了,因为最后一个元素已经是最大的了。
第二轮比较结束后,第二大的数也会冒到倒数第二的位置。
依次类推,再进行第三轮,,,
就这样最大的数一直往后排(冒),最后完成排序。所以我们称这种排序算法为冒泡排序。
选择排序是一种直观的算法,每一轮会选出列中最小的值,把最小值排到前面。具体步骤如下:
插入排序步骤大致如下:
快速排序是由东尼·霍尔所发展的一种排序算法。在平均状况下,排序 n 个项目要Ο(n log n)次比较。在最坏状况下则需要Ο(n2)次比较,但这种状况并不常见。事实上,快速排序通常明显比其他Ο(n log n) 算法更快,因为它的内部循环(inner loop)可以在大部分的架构上很有效率地被实现出来,且在大部分真实世界的数据,可以决定设计的选择,减少所需时间的二次方项之可能性。-php搜索排序
步骤:
从数列中挑出一个元素,称为 “基准”(pivot),
重新排序数列,所有元素比基准值小的摆放在基准前面,所有元素比基准值大的摆在基准的后面(相同的数可以到任一边)。在这个分区退出之后,该基准就处于数列的中间位置。这个称为分区(partition)操作。-php搜索排序
递归地(recursive)把小于基准值元素的子数列和大于基准值元素的子数列排序。
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;
/**
* 构造函数
*
* @param array 例如:
$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;
}
/**
* 初始化
*
* @param array
* @return bool
*/
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;
}
/**
* 插入排序
*
* @param array
* @return bool
*/
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;
}
/**
* 选择排序
*
* @param array
* @return bool
*/
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;
}
/**
* 冒泡排序
*
* @param array
* @return bool
*/
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;
}
/**
* 快速排序
* @by
* @param array
* @return bool
*/
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';
return @file_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*/
PHP 查询排序问题
sql为什么要这样些呢?这样些不可以吗?
Select arc.*,tp.typedir,tp.typename,tp.corank,tp.isdefault,tp.defaultname,tp.namerule,
tp.namerule2,tp.ispart,tp.moresite,tp.siteurl,tp.sitepath
from `#@__archives` arc left join `#@__arctype` tp on arc.typeid=tp.id
where (arc.title like '%$ww%' or arc.title like '%$ww%' or arc.title like '%$pp%' ) $typeid limit 0, $row";-php搜索排序
php 数据库搜索和排序同时哪里错了,排序运行不了,搜索也不行
?php
$link=mysql_connect("localhost","root","000000");//链接服务器
if(!$link){die("链接服务器失败".mysql_error());}//判断服务器链接是否成功
$db=mysql_query("use guest");//链接数据库
if(!$db){die("数据库不存在");}//判断数据库是否存在
mysql_query('set names utf8');//确认字符集为utf8(编码格式)
$order=$_GET['order'];
if($order){
$x=" order by $order desc";
}else{
$x=" order by G_ID asc";
}
$ss=$_GET['ss'];
if($ss){
$where=" where G_UserName like '%".$ss."%' or G_sex like '%".$ss."%'";
}else{
$where="";
}
$sql="select * from g_users".$where.$x; //准备查询语句
$res=mysql_query($sql); //执行语句,获取结果集
?
body
h1 align="center"数据/h1
div align="center" class="cx"
form id="form1" name="form1" method="get" action="test2.php"
input type="text" name="ss" id="ss" /
input type="submit" name="tj" id="tj" value="搜索" /
/form
/div
table width="1300"
tbody
tr
th width="60" align="center" scope="col"
a href="?order=G_UserNamess=?php echo $ss;?"G_ID/a/th
th width="151" align="center"
a href="?order=G_UserNamess=?php echo $ss;?"G_UserName /a/th
th width="70" align="center" scope="col"G_Sex/th
th width="82" align="center" scope="col"G_Face/th
th width="196" align="center" scope="col"G_Email/th
th width="72" align="center" scope="col"G_QQ/th
th width="68" align="center" scope="col"G_Url/th
th width="107" align="center" scope="col"G_Flower/th
th width="124" align="center" scope="col"G_Date/th
th width="90" align="center" scope="col"相关操作/th
/tr
?php
//遍历结果集,收取每个用户的详细信息
while($fetch=mysql_fetch_array($res)){?
tr
td?php echo $fetch[0]?/td
td?php echo $fetch[1]?/td
td?php echo $fetch[5]?/td
tdimg src="?php echo $fetch['G_Face']?"/td
td?php echo $fetch[7]?/td
td?php echo $fetch['G_QQ']?/td
tda href="?php echo $fetch['G_Url']?"?php echo $fetch['G_Url']?/a/td
td?php echo $fetch['G_Flower']?/td
td?php echo $fetch['G_Date']?/td
td?php echo '删除 重置' ?/td
/tr ?php } ?
/tbody
/table
PHP的几种排序方法
1冒泡排序法
2选择排序法
3插入排序法
4快速排序法