Find Maximum in a List of Random Numbers
This program has a divide and conquer algorithm to find the
maximum element of a list. It works as follows:
function findmax(list)
{ var k; var r; var left; var right;
k = Math.floor(list.length/2);
if (k > 0)
{ left = list.slice(0,k);
right = list.slice(k,list.length);
r = Math.max(findmax(left),findmax(right)); }
else
{ r=list[0]; }
return(r); }
If the list is longer than one element, then the function splits
the list into two halves called "left" and "right"
and calls itself as "findmax(left)" and "findmax(right)" as these
parameters. Their maximum gives then the return value "r" of
the function. If the list contains just one element then
the return value "r" is exactly this element.
This implementation outputs the list currently processed by the
function findmax before this function returns a value.
The additional features for this diagnostic output is not
listed in the program for "findmax" above. Also the parameter
"level" which gives the level of recursion of a call to
"findmax" is only in the actual implementation.
The recursive subcalls of the function "findmax"
are listed before the call itself.
Java Script starts here.
Java Script ends here.