Home  >  Article  >  Backend Development  >  How to sort an array in PHP without using functions

How to sort an array in PHP without using functions

PHPz
PHPzOriginal
2023-04-27 09:03:10641browse

PHP is a widely used programming language with many powerful functions to facilitate array sorting. However, in some cases we may need to sort the array without using a function. In this article, we will discuss how to sort an array in PHP without using functions.

1. Bubble sorting method

Bubble sorting is a simple sorting algorithm. Its basic operation is to compare adjacent elements and exchange them if they are in the wrong order. For an array of length n, at most n-1 rounds of comparison and exchange operations are performed. After each round, the tail elements of the unordered area will become the head elements of the ordered area.

The following is the code for implementing bubble sort using PHP using ascending order as an example:

function bubbleSort(&$arr){
    $len = count($arr); //获取数组长度
    for($i=0;$i<$len-1;$i++){ //需要比较n-1轮
        for($j=0;$j<$len-$i-1;$j++){ //每一轮需要比较n-i-1次
            if($arr[$j]>$arr[$j+1]){ //如果前一个元素大于后一个元素,交换位置
                $tmp = $arr[$j+1];
                $arr[$j+1] = $arr[$j];
                $arr[$j] = $tmp;
            }
        }
    }
}

The time complexity of the above code is O(n^2), which is not the optimal sorting algorithm. . But in some cases, the code implementation of bubble sort is relatively simple and can be used to sort small arrays.

2. Selection sorting method

Selection sorting is a simple sorting algorithm. Its basic operation is to select the smallest (or largest) element from the sequence to be sorted and put it in the sorted sequence. end of sequence. For an array of length n, n-1 rounds of comparison and exchange operations are required. After each round of comparison, the head elements of the unordered area will become the tail elements of the ordered area.

The following is the code to implement selection sorting using PHP, taking ascending order as an example:

function selectSort(&$arr){
    $len = count($arr);//获取数组长度
    for($i=0;$i<$len-1;$i++){//需要比较n-1轮
        $minIndex=$i;//用来存储最小元素的下标
        for($j=$i+1;$j<$len;$j++){
            if($arr[$j]<$arr[$minIndex]){//如果有小于当前最小值的元素,更新minIndex
                $minIndex=$j;
            }
        }
        //将最小元素和无序区域的头部元素交换位置
        $tmp = $arr[$minIndex];
        $arr[$minIndex] = $arr[$i];
        $arr[$i] = $tmp;
    }
}

The time complexity of the above code is O(n^2), which is not the optimal sorting algorithm. However, the selection sort method is simple to implement and easy to understand, and can be used to sort small arrays.

3. Insertion sort method

Insertion sort is a simple sorting algorithm. Its basic operation is to insert a piece of data into an already sorted ordered sequence. For an array of length n, n-1 rounds of comparison and move operations are required. After each round of comparison, the head elements of the unordered area will become the tail elements of the ordered area.

The following is the code to implement insertion sort using PHP, taking ascending order as an example:

function insertSort(&$arr){
    $len = count($arr);//获取数组长度
    for($i=1;$i<$len;$i++){//需要将n-1个元素插入到有序区域
        $tmp = $arr[$i];//用一个临时变量存储待插入的元素
        for($j=$i-1;$j>=0;$j--){//将tmp插入到合适的位置
            if($tmp<$arr[$j]){
                $arr[$j+1]=$arr[$j];//将大于tmp的元素向后移动一位
            }else{
                break;//找到了合适的位置,退出循环
            }
        }
        $arr[$j+1]=$tmp;//将tmp插入到合适的位置
    }
}

The time complexity of the above code is O(n^2), which is not the optimal algorithm. However, the insertion sort method is simple to implement and can be used to sort small arrays and partially ordered arrays.

4. Quick sorting method

Quick sorting is an efficient sorting algorithm. Its basic idea is to divide the array into two subsequences through one sorting, in which the elements of the left subsequence are smaller than The base element, the elements of the right subsequence are all greater than the base element, then the left and right subsequences are sorted recursively, and finally the two ordered subsequences are merged into an ordered sequence. The time complexity of quick sort is O(nlogn).

Taking ascending order as an example, the following is the code to implement quick sorting using PHP:

function quickSort(&$arr,$left,$right){
    if($left<$right){
        $i=$left;$j=$right;//选择一个基准元素,初始化左右指针
        $pivot=$arr[$i];//将基准元素存储到临时变量pivot
        while($i<$j){
            while($i<$j && $arr[$j]>=$pivot){//逆序查找比基准元素小的元素
                $j--;
            }
            if($i<$j){
                $arr[$i++]=$arr[$j];
            }
            while($i<$j && $arr[$i]<=$pivot){//顺序查找比基准元素大的元素
                $i++;
            }
            if($i<$j){
                $arr[$j--]=$arr[$i];
            }
        }
        $arr[$i]=$pivot;//将基准元素插入到左右子序列的交界处
        quickSort($arr,$left,$i-1);//对左子序列递归排序
        quickSort($arr,$i+1,$right);//对右子序列递归排序
    }
}

The time complexity of the above code is O (nlogn), which is a better sorting algorithm. The implementation process of quick sort is more complicated, but its performance is the best and can be used to sort arrays of various sizes.

Summary:

The above introduces four methods for sorting arrays without using PHP functions. They are bubble sort, selection sort, insertion sort and quick sort. In practical applications, we can choose an appropriate sorting algorithm based on the size of the array and the sorting requirements. It should be noted that although these algorithms do not rely on PHP's built-in functions, issues such as execution time and memory usage need to be considered in actual use.

The above is the detailed content of How to sort an array in PHP without using functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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