Home > Backend Development > PHP Problem > How to find the median from an unordered array in php

How to find the median from an unordered array in php

PHPz
Release: 2023-04-23 16:56:01
Original
746 people have browsed it

In PHP, array is a very powerful data structure. With PHP arrays, we can easily store and manipulate large amounts of data. But what do we do when we need to find the median from an unordered array?

The median (also called the median) is the middle value in an array. It divides the array into two parts. All values ​​on the left are smaller than it and all values ​​on the right are larger. . If the array has an even number of elements, the median is the average of the middle two elements.

Although PHP provides the sort() function, which can be used to sort arrays, when sorting large arrays, the time complexity of this function will reach O(nlogn) and the sorting of the array will be modified. order, this is not what we want.

So, how do we find the median of an unordered array in PHP without changing the order of the array? Let us take a look below.

  1. Use quick sort to find the median number

The quick sort algorithm is a comparison-based sorting algorithm, and its time complexity under average circumstances is O(nlogn) , and does not require additional memory space. Therefore, we can use quick sort to sort the array and find the median.

The main idea of ​​the quick sort algorithm is to select a benchmark element in the array, and then divide the array into two parts. One part contains all values ​​that are smaller than the benchmark element, and the other part contains all values ​​that are greater than the benchmark element. . These two parts are then sorted recursively until the entire array is sorted.

In order to find the median of an unordered array, we can use the quick sort algorithm to sort it first, and then determine the median based on the length of the sorted array.

The following is an example of PHP code that uses quick sort to find the middle number:

function quickSort($arr){
    $len = count($arr);
    if($len <= 1){
        return $arr;
    }
    $pivot = $arr[0];
    $left_arr = array();
    $right_arr = array();
    for($i=1; $i<$len; $i++){
        if($arr[$i] <= $pivot){
            $left_arr[] = $arr[$i];
        }else{
            $right_arr[] = $arr[$i];
        }
    }
    $left_arr = quickSort($left_arr);
    $right_arr = quickSort($right_arr);
    return array_merge($left_arr, array($pivot), $right_arr);
}

$arr = array(3, 9, 2, 7, 1, 5, 6);
$sorted_arr = quickSort($arr);
$len = count($sorted_arr);
if($len % 2 == 0){
    $middle = ($sorted_arr[$len/2-1] + $sorted_arr[$len/2])/2;
}else{
    $middle = $sorted_arr[($len-1)/2];
}
echo "中数为:".$middle;
Copy after login
  1. Uses heap sort to find the middle number

The heap sort algorithm is a Sorting algorithm for heap-based tree data structures. In heap sort, we sort the array by building a max-heap or min-heap. We can use max heap to find the median of an unordered array.

Maximum heap is a binary tree that satisfies the following properties:

1. The value of each node of the heap is greater than or equal to the value of its left and right child nodes.

2. The heap is always a complete binary tree.

For an unordered array, we can sort it by building a max heap. Then, we can find the median based on the properties of the maximum heap.

The following is an example of PHP code that uses heap sort to find the median:

function buildMaxHeap(&$arr, $index, $heapSize){
    $left = 2*$index+1;
    $right = 2*$index+2;
    $max = $index;
    if($left<$heapSize && $arr[$left]>$arr[$max]){
        $max = $left;
    }
    if($right<$heapSize && $arr[$right]>$arr[$max]){
        $max = $right;
    }
    if($max != $index){
        $temp = $arr[$index];
        $arr[$index] = $arr[$max];
        $arr[$max] = $temp;
        buildMaxHeap($arr, $max, $heapSize);
    }
}

function heapSort(&$arr){
    $heapSize = count($arr);
    for($i=intval($heapSize/2)-1; $i>=0; $i--){
        buildMaxHeap($arr, $i, $heapSize);
    }
    for($i=$heapSize-1; $i>=1; $i--){
        $temp = $arr[0];
        $arr[0] = $arr[$i];
        $arr[$i] = $temp;
        $heapSize--;
        buildMaxHeap($arr, 0, $heapSize);
    }
}

$arr = array(3, 9, 2, 7, 1, 5, 6);
heapSort($arr);
$len = count($arr);
if($len % 2 == 0){
    $middle = ($arr[$len/2-1] + $arr[$len/2])/2;
}else{
    $middle = $arr[($len-1)/2];
}
echo "中数为:".$middle;
Copy after login

Summary

The above are two methods of finding the median in an unordered array in PHP. Although their time complexity is different, they can all implement the functions of quickly sorting unordered arrays and finding the median. If you need to sort a large number of unordered arrays and find the median, it is recommended that you use the heap sort algorithm, which has better time complexity performance.

The above is the detailed content of How to find the median from an unordered array in php. 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