Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
PHP
class MergeSort {
    
    
		function divideElements($arr, $l, $r, $container) {
			
			if ($l < $r) {
				$m = (int) (($l + $r) / 2);
				$this->divideElements($arr, $l, $m, $container);
				$this->divideElements($arr, ($m + 1), $r, $container);
				$this->merge($arr, $l, $m, ($m + 1), $r, $container);			
			}
			return 0;
		}
    
       function merge ($arr, $l1, $r1, $l2, $r2, $container) {
			$i = $l1;
			$j = $l2;
			$k = count($container);
			
			while ($i <= $r1 && $j <= $r2) {
				if ($arr[$i] > $arr[$j]) {
					$container[$k++] = $arr[$j++];				
				} else {
					$container[$k++] = $arr[$i++];				
				}			
			}
			
			while ($j <= $r2) 
				$container[$k++] = $arr[$j++];
			
			while ($i <= $r1) 
				$container[$k++] = $arr[$i++];
		}
    
    }
    
    
    $ms = new MergeSort();
    $arr = [12,11,13,5,6,7];
    $n = count($arr) - 1;
    $container = array();
    $sortedA = $ms->divideElements($arr, 0, $n, $container);


What I have tried:

I have debug the code and also take help from this website : Program for Merge Sort in C - The Crazy Programmer[^]
Posted
Updated 12-Aug-17 16:53pm
v2
Comments
Richard MacCutchan 12-Aug-17 9:49am    
Maybe you should ask The Crazy Programmer.
Patrice T 12-Aug-17 13:07pm    
"code is not working" is not informative.
Give details

1 solution

This code is plain wrong because you removed parts from Crazy programmer code. The problem is that everything matters.

Compare your code with original, you will see what is missing.
Quote:
I have debug the code

You don't know how to use debugger, because otherwise you would have seen the problem.
Debugger is a tool and you have to learn how to use it.

It looks like HomeWork, so only hints.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900