Home > Article > Backend Development > PHP Program for Median of two Sorted Arrays of Same Size
PHP (Hypertext Preprocessor) is a popular scripting language designed for web development. It is widely used for creating dynamic and interactive web pages. PHP code can be embedded directly into HTML, allowing developers to mix PHP and HTML seamlessly. PHP can connect to databases, process form data, generate dynamic content, handle file uploads, interact with servers, and perform various server-side tasks.
PHP supports a wide range of web development frameworks, such as Laravel, Symfony, and CodeIgniter, which provide additional tools and features for building web applications. PHP is an open-source language with a large community, extensive documentation, and a rich ecosystem of libraries and extensions.
The median is a value that separates the higher half from the lower half of the data set. To calculate the median of an array, you need to consider the middle element(s) of the sorted array.
<?php // A Simple Merge based O(n) solution // to find median of two sorted arrays // This function returns median of // ar1[] and ar2[]. Assumptions in // this function: Both ar1[] and ar2[] // are sorted arrays Both have n elements function getMedian($ar1, $ar2, $n) { // Current index of i/p array ar1[] $i = 0; // Current index of i/p array ar2[] $j = 0; $count; $m1 = -1; $m2 = -1; // Since there are 2n elements, // median will be average of elements // at index n-1 and n in the array // obtained after merging ar1 and ar2 for ($count = 0; $count <= $n; $count++) { // Below is to handle case where // all elements of ar1[] are smaller // than smallest(or first) element of ar2[] if ($i == $n) { $m1 = $m2; $m2 = $ar2[0]; break; } // Below is to handle case where all // elements of ar2[] are smaller than // smallest(or first) element of ar1[] else if ($j == $n) { $m1 = $m2; $m2 = $ar1[0]; break; } if ($ar1[$i] < $ar2[$j]) { // Store the prev median $m1 = $m2; $m2 = $ar1[$i]; $i++; } else { // Store the prev median $m1 = $m2; $m2 = $ar2[$j]; $j++; } } return ($m1 + $m2) / 2; } // Driver Code $ar1 = array(1, 3, 5, 7, 9, 11); $ar2 = array(12, 10 ,8 ,6 ,4, 2); $n1 = sizeof($ar1); $n2 = sizeof($ar2); if ($n1 == $n2) echo("Median is " . getMedian($ar1, $ar2, $n1)); else echo("Doesn't work for arrays". "of unequal size"); ?>
It will produce the following output:
Median is 11.5
The provided code implements a simple merge-based solution to find the median of two sorted arrays, $ar1 and $ar2, of the same size. The getMedian function takes the two input arrays and the size n as parameters. It initializes variables to keep track of the current indices, counters, and previous medians. It iterates count from 0 to n, comparing elements from both arrays. It updates the previous and current medians accordingly based on the comparison results.
The function handles cases where one array's elements are smaller than the other array's elements. Finally, it returns the calculated median by averaging the previous and current medians. In the example provided, the driver code creates two arrays, $ar1 and $ar2, and calculates their sizes. It calls the getMedian function to find the median of the arrays and prints the result. If the arrays have unequal sizes, an error message is displayed.
In the example, $ar1 contains [1, 3, 5, 7, 9, 11], and $ar2 contains [12, 10, 8, 6, 4, 2]. Both arrays have the same length, so the algorithm can proceed. The iteration progresses by comparing the elements in the arrays, and the median is updated accordingly. Finally, the median is calculated as (11 + 12) / 2, resulting in a median value of 11.5. Therefore, the output of the code will be "Median is 11.5".
PHP offers a merge-based approach to find the median of two sorted arrays of the same size. By merging the arrays and considering the middle two elements, the program accurately determines the median. It utilizes two indices to traverse the arrays, comparing elements and updating the median variables accordingly.
The resulting median is the average of the middle elements if the array length is even or the middle element if it's odd. This efficient O(n) solution provides a reliable and straightforward method for computing the median of two sorted arrays of the same size in PHP.
The above is the detailed content of PHP Program for Median of two Sorted Arrays of Same Size. For more information, please follow other related articles on the PHP Chinese website!