Question

Given two sorted lists, L1 and L2, write a procedure to computeL1 ∪ L2 using only...

Given two sorted lists, L1 and L2, write a procedure to computeL1 ∪ L2 using only the basic list operations.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

function merge (list1, list2)

{

    arr = create new list of size (list1.length + list2.length)

   

    i = 0;

    j = 0;

    index = 0;

   

    // loop until either of the list is completely

// iterated

    while i < list1.length && j < list2.length

    {

        // if the current element of list1 is smaller

        if list1[i] <= list2[j]

        {

            arr[index++] = list1[i]

            i++

        }

        // if the current element of list2 is smaller

        else

        {

            arr[index++] = list2[j]

            j++

        }

    }

   

// traverse through the left over list1

    while i < list1.length

    {

        arr[index++] = list1[i]

        i++

    }

   

// traverse through the left over list2

    while( j < list2.length )

    {

        arr[index++] = list2[j]

        j++

    }

   

    return arr

}

Add a comment
Know the answer?
Add Answer to:
Given two sorted lists, L1 and L2, write a procedure to computeL1 ∪ L2 using only...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT