Home > Backend Development > PHP Problem > PHP array sorting without function

PHP array sorting without function

WBOY
Release: 2023-05-19 09:13:37
Original
613 people have browsed it

In PHP, an array is a common data type that can store multiple values ​​and access these values ​​through indexes. When we need to sort an array, we usually use PHP's built-in functions, such as sort(), rsort(), asort(), etc., which allow us to sort the array easily.

However, sometimes we may need to sort the array without using the sort function. This may happen in the following situations:

  1. Custom sorting of an array, using the callback function of the sort function is inefficient.
  2. Want to avoid using PHP built-in functions to improve code readability and performance.

So, in this case, how do we sort the array in PHP? This article will introduce you to some array sorting methods based on native PHP language.

  1. Bubble sort

Bubble sort is a basic sorting algorithm that sorts arrays by comparing and exchanging adjacent elements. The specific implementation is as follows:

function array_bubble_sort($arr){
    $len=count($arr);
    for($i=0;$i<$len-1;$i++){
        for($j=0;$j<$len-1-$i;$j++){
            if($arr[$j]>$arr[$j+1]){
                $temp=$arr[$j+1];
                $arr[$j+1]=$arr[$j];
                $arr[$j]=$temp;
            }
        }
    }
    return $arr;
}
Copy after login

In the above code, we use two for loops. The outer loop controls the number of comparisons, and the inner loop is used to compare and exchange adjacent elements. In this way we can sort the array without using sort function.

  1. Insertion sort

Insertion sort is a simple and effective sorting algorithm that can perform operations on an array by inserting each element into a sorted array one by one. Sort. The specific implementation is as follows:

function array_insertion_sort($arr){
    $len=count($arr);
    for($i=1;$i<$len;$i++){
        $temp=$arr[$i];
        $j=$i-1;
        while($j>=0&&$arr[$j]>$temp){
            $arr[$j+1]=$arr[$j];
            $j--;
        }
        $arr[$j+1]=$temp;
    }
    return $arr;
}
Copy after login

In the above code, we use a for loop and a while loop. The for loop is used to select the element to be inserted, and the while loop is used to insert it into the correct position. Also in this way we can sort the array without using sort function.

  1. Quick Sort

Quick sort is a commonly used sorting algorithm that sorts an array by dividing it into two sub-arrays. The specific implementation is as follows:

function array_quick_sort($arr){
    $len=count($arr);
    if($len<=1){
        return $arr;
    }
    $pivot=$arr[0];
    $left=[];
    $right=[];
    for($i=1;$i<$len;$i++){
        if($arr[$i]<=$pivot){
            $left[]=$arr[$i];
        }else{
            $right[]=$arr[$i];
        }
    }
    return array_merge(array_quick_sort($left),[$pivot],array_quick_sort($right));
}
Copy after login

In the above code, we use recursion to implement quick sorting. First, we select a pivot element as the pivot, then iterate through the array and put the smaller element into the left subarray and the larger element into the right subarray, then sort the left and right subarrays by recursion, and finally put They are merged with the base element. Likewise, it is possible to sort an array without using the sort function.

Summary

The above three sorting algorithms can sort PHP arrays without using a sorting function. Although these algorithms may not be as efficient as PHP's built-in sorting functions, they can improve the readability and understanding of our code. If you need a custom sorting method or avoid using PHP built-in functions, try using these algorithms to sort your array.

The above is the detailed content of PHP array sorting without function. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template