Dazzler Dazzler - 6 months ago 12
Node.js Question

How to display output of one function after another

I have written a Javascript file of two algorithms. As shown in the code below, I am using a

for loop
to generate random values which are used by both algorithms as input.

At present, I am displaying output of the
binarySearch
and
SearchSorted
alternatively.

The problem I am facing is I have to pass the same array values generated by
randomlyGenerateArray
in the main program to both the algorithms for a meaningful comparison. But I don't know how to change the output format.

I have thought of adding them in different loops, but as I have explained above i need to use the same
randomArray
values for both the algorithms.

i.e., The below code produces output as shown below -

Binary Search Successful 1
Search Sorted Successful 5
Binary Search Successful 3
Search Sorted Successful 10


How do I display the output of Binary Search First and then display output of Search Sorted? it's something like this. Any help will be greatly appreciated.

Binary Search Successful 1
Binary Search Successful 3
Search Sorted Successful 5
Search Sorted Successful 10


// Binary Search Algorithm
function binarySearch(A,K)
{
var l = 0; // min
var r = A.length - 1; //max
var n = A.length;
var operations = 0;

while(l <= r)
{
var m = Math.floor((l + r)/2);
operations++;

if(K == A[m])
{
console.log('Binary Search Successful %d',operations);
return m;
}
else if(K < A[m])
{
r = m - 1;
}
else
{
l = m + 1;
}
}
operations++;
console.log('Binary Search Unsuccessful %d',operations);
return -1;
}

// Search Sorted Algorithm
function searchSorted(A, K)
{
var n = A.length;
var i = 0;
var operations = 0;

while (i < n)
{
operations++;
if (K < A[i])
{
return -1;
}
else if (K == A[i])
{
console.log('Search Sorted Successful %d', operations);
return i;
}
else
{
i = i + 1;
}
}
operations++;
console.log('Search Sorted Unsuccessful %d', operations);
return -1;
}

// Random Array generator
var randomlyGenerateArray = function(size)
{
var array = [];
for (var i = 0; i < size; i++)
{
var temp = Math.floor(Math.random() * maxArrayValue);
var final = array.splice(5, 0, 30);
array.push(final);
}
return array;
}

//Sort the Array
var sortNumber = function(a, b)
{
return a - b;
}

// Main Program
var program = function()
{
var incrementSize = largestArray / numberOfArrays;
for (var i = smallestArray; i <= largestArray; i += incrementSize)
{
var randomArray = randomlyGenerateArray(i);
var sort = randomArray.sort(sortNumber);
var randomKey = 30;
binarySearch(sort, randomKey);
searchSorted(sort, randomKey);
}
}

var smallestArray = 10;
var largestArray = 10000;
var numberOfArrays = 1000;
var minArrayValue = 1;
var maxArrayValue = 1000;

program();

Answer

You could store the sorted randomArrays in an array (which I've called sortedRandomArrays), then run a for loop for each search.

The Main Program would then look like:

// Main Program
var program = function()
{
    var incrementSize = largestArray / numberOfArrays;
    var sortedRandomArrays = [];
    for (var i = smallestArray; i <= largestArray; i += incrementSize)
    {
        var randomArray = randomlyGenerateArray(i));
        var sort = randomArray.sort(sortNumber);
        sortedRandomArrays.push(sort);
        var randomKey = 30;
    }

    for (var i = 0; i < sortedRandomArrays.length; i++)
    {
        binarySearch(sortedRandomArrays[i], randomKey);
    }

    for (var i = 0; i < sortedRandomArrays.length; i++)
    {
        searchSorted(sortedRandomArrays[i], randomKey);
    }
}

Note: I've written this to make as few changes as possible to your original code to emphasize the solution, rather than making it as clean as possible.

Comments