Question
Please answer this and leave copyable code and output for this question along with the question it is asking, thank you
(30 points) Here is an SML mergesort program: fun merge ([], ys) -ys I merge (xs, [)xs merge (x: xs, y: ys) if x< y then x: :merge (xs, y::ys) else y: :merge (x: :xs, ys) fun split [] I split [a] I split (a::b::cs) let val (M, N)split cs in end fun mergesort [[ merge sort [a] [a] I mergesort [a,bif a <b then [a,b] else [b, a] I mergesort L - let val (M, N) = split L ln merge (mergesort M, mergesort N) end
media%2F8cd%2F8cdc373a-28ff-4789-9ff3-99
0 0
Add a comment Improve this question Transcribed image text
Answer #1

According to me, if we delete both third and second line, then the function goes in an infinite loop for every input except for an empty array input.

Execution is shown below:
- use "ms.sml";
[opening ms.sml]
val merge = fn : int list * int list -> int list
val split = fn : 'a list -> 'a list * 'a list
val mergesort = fn : 'a list -> int list
val it = () : unit
- mergesort([]);
val it = [] : int list
- mergesort([1]);

(* infinite loop *)

This is due to fact - everytime when mergesort reaches at single element level i.e., [a], it calls split function which splits it into [a] and []. [] is handled correctly by mergesort() but the same thing written above happens for [a] i.e., single element level. Hence repititive calls of mergesort() and split() lead to infinite loop.

Another observation, if we just remove the second line (case [a]) but keep the third line (case [a,b]) intact, then the inifinite loop thing doesnt occur and gives correct output for power of 2-sized arrays except 1 (size = 2,4,8,..) . Since [a,b] is handled correctly by third line.

-
Thanks.

Add a comment
Know the answer?
Add Answer to:
Please answer this and leave copyable code and output for this question along with the question...
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
  • How would I be able to get a Merge Sort to run in this code? MY...

    How would I be able to get a Merge Sort to run in this code? MY CODE: #include <iostream> #include <fstream> #include <stdlib.h> #include <stdio.h> #include <time.h> using namespace std; class mergersorter { private:    float array[1000] ;    int n = 1000;    int i=0; public:    void fillArray()    {        for(i=1;i<=n;i++)        {            array[i-1]= ( rand() % ( 1000) )+1;        }    }    void arrayout ()    {   ...

  • can someone help me answer the following #Fsharp multiple choice questions along with explanations for each...

    can someone help me answer the following #Fsharp multiple choice questions along with explanations for each question please. 7. How does F# interpret the type int * bool -> string list? Select one: a. (int * (bool -> string)) list b. ((int * bool) -> string) list c. int * (bool -> (string list)) d. (int * bool) -> (string list) 8. Let F# function foo be defined as follows: let rec foo = function | (xs, []) -> xs...

  • Hi All, Can someone please help me correct the selection sort method in my program. the...

    Hi All, Can someone please help me correct the selection sort method in my program. the output for the first and last sorted integers are wrong compared with the bubble and merge sort. and for the radix i have no idea. See program below import java.io.*; import java.util.ArrayList; import java.util.Scanner; public class Sort { static ArrayList<Integer> Data1 = new ArrayList<Integer>(); static ArrayList<Integer> Data2 = new ArrayList<Integer>(); static ArrayList<Integer> Data3 = new ArrayList<Integer>(); static ArrayList<Integer> Data4 = new ArrayList<Integer>(); static int...

  • Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Jav...

    Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Java program provided: // Student Name Today's Date import java.util.Arrays; import java.util.Random; public class SortTimer {    // Please expand method main() to meet the lab requirements.       // You have the following sorting methods available:    // insertionSort(int[] a);    // selectionSort(int[] a);    // mergeSort(int[] a);    // quickSort(int[] a);    // The array will be in sorted order after the routines are called!   ...

  • Please give a output Linux/Ubuntu and how to run it (compile) this program ,my assigment is below...

    Please give a output Linux/Ubuntu and how to run it (compile) this program ,my assigment is below : : Merge Sort algorithm using 2 processes a.) Define an integer array of 100 integers. Populate this array with random numbers. You can use int rand(void); function. Do not forget to initialize this function. You will sort the numbers in the array using merge-sort algorithm. In merge sort algorithm the half of the array will be sorted by one process and second...

  • Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In...

    Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In this project, we combine the concepts of Recursion and Merge Sorting. Please note that the focus of this project is on Merging and don't forget the following constraint: Programming Steps: 1) Create a class called Art that implements Comparable interface. 2) Read part of the file and use Merge Sort to sort the array of Art and then write them to a file. 3)...

  • Hello, I want to check if my C++ code is correct and follows the requeriments described...

    Hello, I want to check if my C++ code is correct and follows the requeriments described thanks. Requeriments: Assignment Sorting Benchmark each of the sorting methods listed below. Insertion Sort Bubble Sort Selection Sort Heap Sort. Quick Sort. Merge Sort. Benchmark each of the above sorting methods for data sizes of 10000, 20000, 30000, 40000 and 50000. Display the results in a table as shown below. The table should have rows and columns. However, the rows and columns need not...

  • Demo Your instructor will now debug the below faulty QuickSort example int size 8,W makes code...

    Demo Your instructor will now debug the below faulty QuickSort example int size 8,W makes code simpler void swapint "xp, int yp) yp-semp int partitionlint intArry int left, int right, int pivot) t int left Pointer left in righPointer right; printfi-Calling quick sort, index %d to %d with pivot %d、m"lea, righe, pivot while) N korp increasing left pointer until run into something greater thon pivet W keep decrcasing right pointer until nun into something smaller than piv 1 else t...

  • Modify the given code below which uses Simpson's 3/8 method with 6 subintervals to answer the...

    Modify the given code below which uses Simpson's 3/8 method with 6 subintervals to answer the question above. if a==b I=0 else N=3; h=(b-a)/N; x=a:h:b; y=Fun(x); I=3*h/8*(y(1)+2*sum(y(4:3:(N-2)))+y(N+1)); N=2*N; check=0; % Calculating subsequent valus of I while check==0; h=(b-a)/N; x=a:h:b; y=Fun(x); % Composite Simpson's 3/8 method, Eq. (9.22) I_new=3*h/8*(y(1)+2*sum(y(4:3:(N-2)))+y(N+1)); I_new=I_new+3*h/8*3*(sum(y(2:3:(N-1)))+sum(y(3:3:N))); % Compare solution with that calculated in the previous iteration error=abs((I-I_new)/I)*100; % (*) if error>0.1 % continue iteration check=0; N=N*2; I=I_new; elseif error<=0.1 % end iteration check=1; I=I_new; end end end...

  • 1) can any one please givem the code for this a) If n is a power...

    1) can any one please givem the code for this a) If n is a power of 2, as it is in Figure 9-3, you would merge pairs of individual entries, starting at the beginning of the array. Then you would return to the beginning of the array and merge pairs of twoentry subarrays. Finally, you would merge one pair of four-entry subarrays. Notice that the subarrays in each pair of subarrays contain the same number of entries. In general,...

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
ADVERTISEMENT