Question

In: Computer Science

Implement a version of merge() that copies the second half of a[] to aux[] in decreasing...

Implement a version of merge() that copies the second half of a[] to aux[] in decreasing order and then does the merge back to a[]. This change allows you to remove the code to test that each of the halves has been exhausted from the inner loop. Note: the resulting sort is not stable.

Algorithms Fourth Edition Exercise 2.2.10

Solutions

Expert Solution

void merge(int a[],int init,int final,int mid)

{

    int pointer1=init;

    int pointer2=final;

    int aux[final-mid+1];

    int aux2[final-init+1];

    int pointer3=0;

    for(int x=final;x>=mid;x--)

   {

        aux[pointer3++]=a[x];

}

   j=0;

   int sol_pt=0;

   while(i<mid && j<pointer3)

   {

         if(a[i]<aux[j])

        {

              aux2[sol_pt++]=a[i];

              i++;

        }

        else

        {

            aux2[sol_pt++]=aux[j];

             j++;

        }

}

while(i<mid)

{

      aux2[sol_pt++]=a[i];

       i++;

}

while(j<pointer3)

{

    aux2[sol_pt++]=aux[j];

     j++;

}

for(int k=0;k<pointer3;k++)

{

      a[init+k]=aux2[k];

}

}


Related Solutions

Language: Java Implement Merge Sort
Language: Java Implement Merge Sort
I've answered the first half, but im stuck on the second half with multilanes and i...
I've answered the first half, but im stuck on the second half with multilanes and i need help with it . Highway Capacity and Level-of-Service Analysis Freeways A new segment of freeway is being built to connect two existing parallel freeway facilities, in an urban area. The following traffic and roadway characteristics are expected: Traffic Characteristics • AADT = 92000 veh/day • K = 13% • D = 50% • PHF = 0.92 • 8% trucks and buses • 3%...
java please        * implement zip method in Q1 class to merge two linkedlists into...
java please        * implement zip method in Q1 class to merge two linkedlists into one.        * The elements in the result are made by concatenating elements in different        * linkedlists one after one. The order of the elements matters.        * For example, merge(list1, list2) should return [1,5,2,4,3,3,4,2,5,1].        * You can assume that arr1 and arr2 has the same size.        * HINT: You should use ListIterator to make it...
IN MATLAB!! Q6. Create a 1D array of numbers and implement ‘Merge Sort’ in MATLAB to...
IN MATLAB!! Q6. Create a 1D array of numbers and implement ‘Merge Sort’ in MATLAB to sort it in ascending order
Java program to implement the merge sort your own and test it to sort a list...
Java program to implement the merge sort your own and test it to sort a list of names based on the frequency.
You'll implement a completely generic version of an algorithm to find the maximum of an array....
You'll implement a completely generic version of an algorithm to find the maximum of an array. Unlike in the past, when our algorithm only worked for int[] or double[], this version will work on any Java objects that are comparable, specifically any Java object that implements the Comparable interface. Create a public class named Max with a single class method named max. max should accept an array of Objects that implement Comparable and return the maximum. If the array is...
you will create a program with Java to implement a simplified version of RSA cryptosystems. To...
you will create a program with Java to implement a simplified version of RSA cryptosystems. To complete this project, you may follow the steps listed below (demonstrated in Java code) to guide yourself through the difficulties. Step I Key-gen: distinguish a prime number (20 pts) The generation of RSA's public/private keys depends on finding two large prime numbers, thus our program should be able to tell if a given number is a prime number or not. For simplicity, we define...
“The outlook for the market in the second half of the year is starting to look...
“The outlook for the market in the second half of the year is starting to look more encouraging as the global economy recovers, said Mohammad Barkindo, secretary-general of the Organization of Petroleum Exporting Countries. The cartel and its allies are rapidly implementing their production cuts, he said.” (Blooberg News: “OPEC Chief Optimistic That the Worst of Oil Crisis is Over” by Vonnie Quinn, Annmarie Hordern, and Grant Smith, May 15, 2020) Applying the model of supply and demand, explain briefly...
Implement Library Sort in Java which is a version of Insertion Sort with gaps to speed...
Implement Library Sort in Java which is a version of Insertion Sort with gaps to speed up the computation. If the pseudocode in Wikipedia (https://en.wikipedia.org/wiki/Library_sort) is not enough, you can download the research paper from (https://arxiv. org/pdf/cs/0407003.pdf). Below is the algorithm and pseudo code. Implementation Algorithm Let us say we have an array of n elements. We choose the gap we intend to give. Then we would have a final array of size (1 + ε)n. The algorithm works in...
Implement Library Sort which is a version of Insertion Sort with gaps to speed up the...
Implement Library Sort which is a version of Insertion Sort with gaps to speed up the computation. If the pseudocode in Wikipedia (https://en.wikipedia.org/wiki/Library_sort) is not enough, you can download the research paper from (https://arxiv. org/pdf/cs/0407003.pdf).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT