Question

In: Computer Science

Please answer this multi-part question. Thank you! For this assignment you must write the following functions...

Please answer this multi-part question. Thank you!

For this assignment you must write the following functions in Scheme:

4 Write a recursive function called mergesort that sorts a list by doing the following:
(a) Use split to split the list into two roughly equal-sized partitions.
(b) Recursively sort both partitions.
(c) Use merge to merge the sorted partitions together.
Once again you will need two base cases, one for the empty list and the other for a single-element list.
> (mergesort '())

()

> (mergesort '(9))

(9)

> (mergesort '(8 6 7 5 3 0 9))

(0 3 5 6 7 8 9)

Solutions

Expert Solution

#include<iostream>
using namespace std;

void merge_partition(int arr[], int l, int m, int r)
{
    int i, j, k;
    int n1 = m - l + 1;
    int n2 = r - m;

    int L[n1], R[n2];

    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1 + j];

    i = 0;
    j = 0;
    k = l;
    while (i < n1 && j < n2) {
        if (L[i] <= R[j]) {
            arr[k] = L[i];
            i++;
        }
        else {
            arr[k] = R[j];
            j++;
        }
        k++;
    }

    while (i < n1) {
        arr[k] = L[i];
        i++;
        k++;
    }

    while (j < n2) {
        arr[k] = R[j];
        j++;
        k++;
    }
}

void mergeSort(int arr[], int l, int r)
{
    if (l < r)    // base condition
 {
        int m = l + (r - l) / 2;
        mergeSort(arr, l, m);
        mergeSort(arr, m + 1, r);

        merge_partition(arr, l, m, r);
    }
}

int main()
{
    int n;
    cin>>n;
    int arr[n];
    for(int i=0;i<n;i++)
        cin>>arr[i];

    mergeSort(arr, 0, n-1);

  for(int i=0;i<n;i++)
    cout<<arr[i]<<" ";

   return 0;
}

PS:If this answer is helpful for you then please give an upvote.


Related Solutions

Please answer this multi-part question. Thank you! For this assignment you must write the following function...
Please answer this multi-part question. Thank you! For this assignment you must write the following function in Scheme: 4 Write a recursive function called mergesort that sorts a list by doing the following: (a) Use split to split the list into two roughly equal-sized partitions. (b) Recursively sort both partitions. (c) Use merge to merge the sorted partitions together. Once again you will need two base cases, one for the empty list and the other for a single-element list. >...
Please answer this two part question. Thank you! For this assignment you must write the following...
Please answer this two part question. Thank you! For this assignment you must write the following functions in Scheme: 2.1 Write a recursive function called eval-poly that takes a list of numbers representing the coefficients of a polynomial and a value for ? and evaluates the polynomial for the given value of ?. The list of coefficients should start with the term of lowest degree and end with the term of highest degree. If any term of intermediate degree is...
Please answer part a) through part d) of the question below. Thank you. Question 3 A...
Please answer part a) through part d) of the question below. Thank you. Question 3 A tobacco refinery has four methods of measuring pH. To test the four methods, a supervisor randomly assigns each of 32 tobacco samples with known pH to one of the four methods, so that each method is applied to exactly eight samples. The difference between measured pH and the known pH is recorded, and the data is below. Method Sample Response A 1 -0.307 A...
This question has two parts. Please answer both, thank you! Part A.) On the first day...
This question has two parts. Please answer both, thank you! Part A.) On the first day of the fiscal year, a company issues a $8,200,000, 9%, 9-year bond that pays semiannual interest of $369,000 ($8,200,000 × 9% × ½), receiving cash of $6,868,206. Journalize the first interest payment and the amortization of the related bond discount. Round to the nearest dollar. If an amount box does not require an entry, leave it blank. Interest Expense Discount on Bonds Payable Cash...
please answer the following question on the bottom. thank you. 4. Why is the mix of...
please answer the following question on the bottom. thank you. 4. Why is the mix of output produced in competitive markets more desirable than that in monopolistically competitive markets? 5. Prior to 1982, AT&T kept local phone rates low by subsidizing them from long-distance profits. Was such cross-subsidization in the public interest? Explain. 6. Why is there resistance to (a) local phone companies providing video and data services and (b) mergers of local cable and telephone companies?
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as possible: What is a checked exception, and what is an unchecked exception? What is NullPointerException? Which of the following statements (if any) will throw an exception? If no exception is thrown, what is the output? 1: System.out.println( 1 / 0 ); 2: System.out.println( 1.0 / 0 ); Point out the problem in the following code. Does the code throw any exceptions? 1: long value...
In Java please. Thank you! Recursion For this assignment you are going to write six different...
In Java please. Thank you! Recursion For this assignment you are going to write six different methods. Each method is to be written recursively. Any method that is written iteratively will not receive any credit, even if it is correct and produces the same results or output. You will be given a starter file. You are not allowed to change the signatures of any of the given methods. You are not allowed to add any methods to your solutions. Write...
Each question has 6-7 parts, depending on the work. Please answer every part. Thank you. -...
Each question has 6-7 parts, depending on the work. Please answer every part. Thank you. - What is the formula for the Average Propensity to Consume (APC)? Group of answer choices consumption divided by income the change in consumption divided by a change in income income divided by consumption the change in income to a change in consumption None of the above - How does the size of the Marginal Propensity to Consume (MPC) affect the size of the multiplier...
Each question has 8-9 parts, depending on the work. Please answer every part. Thank you. -...
Each question has 8-9 parts, depending on the work. Please answer every part. Thank you. - The government spending multiplier is Group of answer choices the ratio of the change in the equilibrium level of output to a change in government spending the difference between the new and old levels of government spending. the ratio of the change in government spending to the change in the equilibrium level of output the difference between the old level of equilibrium output and...
Assignment Write each of the following functions. The function header must be implemented exactly as specified....
Assignment Write each of the following functions. The function header must be implemented exactly as specified. Write a main function that tests each of your functions. Specifics In the main function ask for a filename and fill a list with the values from the file. Each file should have one numeric value per line. This has been done numerous times in class. You can create the data file using a text editor or the example given in class – do...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT