A Collection of Code Snippets in as Many Programming Languages as Possible

This project is maintained by TheRenegadeCoder

Welcome to the Merge Sort in Php page! Here, you'll find the source code for this program as well as a description of how the program works.

```
<?php
function merge_sort($my_array)
{
if (count($my_array) == 1) {
return $my_array;
}
$mid = count($my_array) / 2;
$left = array_slice($my_array, 0, $mid);
$right = array_slice($my_array, $mid);
$left = merge_sort($left);
$right = merge_sort($right);
return merge($left, $right);
}
function merge($left, $right)
{
$res = array();
while (count($left) > 0 && count($right) > 0) {
if ($left[0] > $right[0]) {
$res[] = $right[0];
$right = array_slice($right, 1);
} else {
$res[] = $left[0];
$left = array_slice($left, 1);
}
}
while (count($left) > 0) {
$res[] = $left[0];
$left = array_slice($left, 1);
}
while (count($right) > 0) {
$res[] = $right[0];
$right = array_slice($right, 1);
}
return $res;
}
if (empty($argv[1])) {
exit('Usage: please provide a list of at least two integers to sort in the format "1, 2, 3, 4, 5"');
}
$test_array = array_map('intval', explode(',', $argv[1]));
$array_size = count($test_array);
if ($array_size <= 1) {
exit('Usage: please provide a list of at least two integers to sort in the format "1, 2, 3, 4, 5"');
}
echo implode(', ', merge_sort($test_array)) . "\n";
```

Merge Sort in Php was written by:

- Ankit kumar
- Jeremy Grifski
- Parker Johansen

If you see anything you'd like to change or update, please consider contributing.

**Note**: The solution shown above is the current solution in the Sample Programs repository as of May 09 2022 21:44:19. The solution was first committed on Oct 10 2019 02:24:20. As a result, documentation below may be outdated.

No 'How to Implement the Solution' section available. Please consider contributing.

No 'How to Run the Solution' section available. Please consider contributing.