Home Backend Development PHP Tutorial PHP数组排序算法小结

PHP数组排序算法小结

Jun 20, 2016 pm 01:05 PM
php array

PHP中对数组的元素进行排序,这个是很经常用到的,之前的项目中也有,而且对于几种排序我们都是用的是asort  arsort 等PHP原生函数,没有自己去实现,所以就对一下的几个函数进行总结,这个会不断的进行补充,自己也可以好好的复习和总结。

/*
 * 插入排序(一维数组)
 * 每次将一个待排序的数据元素,插入到前面已经排好序的数列中的适当的位置,使数列依然有序;直到待排序的数据元素全部插入完成为止。
 */
function insertSort($arr){
    if(!is_array($arr) ||count($arr)==0){
        return $arr;
    }
    $count =count($arr);
    for($i=1;$i<$count;$i++){
        if(isset($arr[$i])){
       $tmp =$arr[$i];//获取后一个元素的值
       $j =$i - 1;//获取前面的下标
       while($arr[$j] >$tmp){//如果前面一个比后面一个大, 这里是从小到大
           $arr[$j+1] =$arr[$j];//把小的元素和前面的对换,直到移动到合适的位置,在移动下一个
           $arr[$j] =$tmp;
           $j--;
        }
        }
    }
    return $arr;
}
/*
 * 选择排序(一维数组)
 * 每一趟从待排序的数据元素中选出最小(最大)的一个元素,顺序放在已排好序的数列的最后,直到全部待排序的数据元素排完。
 */
function selectSort($arr){
    if(!is_array($arr) ||count($arr) == 0)
    {
        return $arr;
    }
    $count =count($arr);
    for($i=0;$i<$count;$i++){
        $k =$i;
        for($j=$i+1;$j<$count;$j++){
      if ($arr[$k] >$arr[$j])
        $k =$j;//找出最小的
       if ($k !=$i){
           $tmp =$arr[$i];
           $arr[$i] =$arr[$k];
           $arr[$k] =$tmp;
          }
       }
    }
    return $arr;
}
 
/*  
 * 冒泡排序(一维数组)
 * 两两比较待排序数据元素的大小,发现两个数据元素的次序相反即进行交换,直到没有反序的数据元素为止
 */
function bubbleSort($array){
    $count =count($array);
    if ($count <= 0) {
        return false;
    }
    for($i=0;$i<$count;$i++){
        for($j=$count-1;$j>$i;$j--){
           if ($array[$j] <$array[$j-1]){//比较找到的数进行交换
            $tmp =$array[$j];
            $array[$j] =$array[$j-1];
            $array[$j-1] =$tmp;
           }
        }
    }
    return $array;
}
/*
 * 快速排序(一维数组)
 *
 */
function quickSort($array){
    if (count($array) <= 1){
        return $array;
    }
    $key =$array[0];
    $left_arr =array();
    $right_arr =array();
    for ($i=1;$i<count($array);$i++){
      if ($array[$i] <=$key){
           $left_arr[] =$array[$i];
      }else{
         $right_arr[] =$array[$i];
        }
    }
    $left_arr = quickSort($left_arr);
    $right_arr = quickSort($right_arr);
    return array_merge($left_arr,array($key),$right_arr);
}
 
/**
  * 按照元素的值进行排序
  * strOrder 为排列的顺序 asc 升序  desc 降序
  */
function sortByVal($arr,$strOrder=&#39;asc&#39;)
{
    if(!is_array($arr) ||count($arr)==0)
    {
        return $arr;
    }
 
    $arrReturn =array();
    foreach($arr as $key=>$val)
    {
        $arrKey[] =$key;
        $arrVal[] =$val;
    }
 
    $count =count($arrVal);
    if($count)
    {
        //创建key的顺序数组
        for($key=0;$key<$count;$key++)
        {
            $arrKeyMap[$key] =$key; 
        }
        //对值进行排序
        for($i=0;$i<$count;$i++)
        {  
             
            for($j =$count-1;$j>$i;$j--)
            {
                //<从小到大排列 升降在这修改
                $bol =$strOrder ==&#39;asc&#39; ?$arrVal[$j]<$arrVal[$j-1] :$arrVal[$j]>$arrVal[$j-1];
                if($bol){
                    $tmp =$arrVal[$j];
                    $arrVal[$j] =$arrVal[$j-1];
                    $arrVal[$j-1] =$tmp;
                    //值的冒泡排序,引起key的数组的交互   
                    $keytmp =$arrKeyMap[$j];
                    $arrKeyMap[$j] =$arrKeyMap[$j-1];
                    $arrKeyMap[$j-1] =$keytmp;
                }
            }
        }
        if(count($arrKeyMap))
        {
            foreach ($arrKeyMap as $val)
            {
                    $arrReturn[] =$arrKey[$val];
            }
        }
        return $arrReturn;
    }
}
 
 
/**
  * 使用原生的函数进行数组按照值进行排列
  */
function arraySortByVal($arr,$keys,$type=&#39;asc&#39;){
    $keysvalue =$new_array =array();
    foreach ($arr as $k=>$v){
        $keysvalue[$k] =$v[$keys];
    }
    if($type ==&#39;asc&#39;){
        asort($keysvalue);
    }else{
        arsort($keysvalue);
    }
    reset($keysvalue);
    foreach ($keysvalue as $k=>$v){
        $new_array[$k] =$arr[$k];
    }
    return $new_array;
Copy after login

 

对于下面的2个对于array的值进行排序的方法一个是自己实现的一个是使用了原生的PHP函数的,其实排序对于少量数据一般就单页的数据量的数据还是可以的,如果涉及到大量的数据的排序,建议可以整合到MYSQL的基础类中来进行。

 


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

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use PHP arrays to generate and display charts and statistical graphs How to use PHP arrays to generate and display charts and statistical graphs Jul 15, 2023 pm 12:24 PM

How to use PHP arrays to generate and display charts and statistical graphs

How to use PHP arrays to generate dynamic slideshows and image displays How to use PHP arrays to generate dynamic slideshows and image displays Jul 15, 2023 pm 01:17 PM

How to use PHP arrays to generate dynamic slideshows and image displays

How to use PHP arrays to implement user login and permission management functions How to use PHP arrays to implement user login and permission management functions Jul 15, 2023 pm 08:55 PM

How to use PHP arrays to implement user login and permission management functions

How to determine how many arrays there are in php How to determine how many arrays there are in php Aug 04, 2023 pm 05:40 PM

How to determine how many arrays there are in php

What are php array key-value pairs? What are php array key-value pairs? Aug 03, 2023 pm 02:20 PM

What are php array key-value pairs?

An exploration of performance optimization techniques for PHP arrays An exploration of performance optimization techniques for PHP arrays Mar 13, 2024 pm 03:03 PM

An exploration of performance optimization techniques for PHP arrays

Effective implementation of array union in PHP Effective implementation of array union in PHP Apr 30, 2024 pm 01:03 PM

Effective implementation of array union in PHP

What is the function in PHP to determine if an array is empty? What is the function in PHP to determine if an array is empty? Aug 03, 2023 pm 05:15 PM

What is the function in PHP to determine if an array is empty?

See all articles