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
Implement a version with the outer loop, with a while loop, and the inner loop with...
Implement a version with the outer loop, with a while loop, and the inner loop with a do / while loop. Modify this program as he ask ↑↑↑ C++ // This program averages test scores. It asks the user for the 2 // number of students and the number of test scores per student. 3 #include <iostream> 4 #include <iomanip> 5 using namespace std; 6 7 int main() 8 { 9 int numStudents, // Number of students 10 numTests; //...
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...
Stack and Queue In this question, you will implement a modified version of stackArray class. This...
Stack and Queue In this question, you will implement a modified version of stackArray class. This modified class requires you to: • Implement two stacks using a single array. One stack uses the beginning of the array and the other uses the other end. • Your code should allowing pushes to and pops from each stack by passing the 0 and 1 to any function to identify the needed stack. • All stack functions should be be applied on both...
“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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT