Homework Solution: For this assignment you will write your own version of a merge sort and a b…

    Instructions… For this assignment you will write your own version of a merge sort and a bubble sort -- sorting files of numbers. Your programs should read a file integers into an array of integers, sort them from least to greatest, then print the sorted list out. (note: you do not have to sort "in-place" -- you can sort into a newly allocated array) For you program to allocate a large enough array up front, you may want to have the first number in your data files be the count of how many random numbers are in the file. …Or it could be a command line parameter to your program. Your version of these programs should include keeping count every time your program compares two items from the list. Print the count of comparisons when the programs ends. It is up to you how you keep the "count of comparisons" separated from the sorted output list. On Linux, you might use stdout and stderr… …or you might give filenames as command line args to your programs, and the only stdout form your program will be the stats on how many comparison steps were used to sort the      list. …or you might have a command line arg with a filename specifically to **append** stats to Write automated script(s) to run the tests of your programs against files of various sizes. keep in mind the point of these scripts is to make it easier to re-run tests as needed and collect data in an organized, easy-to-use form make your scripts as useful as you need them to be to save yourself time Example: perhaps give the script command line args to control the range of file sizes to tests… …so you you can re-run only tests that need to be re-run. Input file sizes should be every power of two from 21 to at least 226 numbers. NOTE: your bubble sort will probably be too slow for the largest file sizes. Once your tests start taking close to a half hour or an hour, maybe it is time to stop, unless you can let it run over night… File sizes (number of lines, number of random numbers in the list) should be: 2, 4, 8, 16, 32, 64….. 1024, 2048, 4K, 8K, 16K….. 1048576, 2M, 4M, 8M… 32M, 64M that is the number of lines -- the number of random numbers in each list… …files sizes might each be one line longer ifyou put the size on the first line Try to go larger if your environment can handle it and the sorts are not taking too long Create Plots: (see tip on controlling axes in Octave) # of Comparison Steps: Merge vs. Bubble -- Small Files choose a range for x & y axes that lets you easily compare the two while they are still somewhat close x from 0..32 or 64 probably about right choose a range for y that fits Merge vs. Bubble -- large files now do the full range of file sizes (can still start at x axis at 0) at this point you should see that one of them almost disappears compared to the other Time: Merge v Bubble, small range Same basic idea as with the # of comparison steps For the smallest files, the true time is likely too small to easily measure Choose a large enough range a that at least a few results of both methods are > 1 ms Merge v Bubble, large range Same basic idea as with the # of comparison steps

    Expert Answer

     
    The sorting for the merge and bubble sort inclusive of insertion sort are as follows:

    Instructions…

    Ce this provision you earn transcribe your admit rendering of a add order and a dream order — ordering rasps of aggregate.

    Your programs should perauthentication a rasp integers into an rank of integers, order them from lowest to highest, then imimstereotype the ordered schedule quenched.

    (note: you do referable accept to order “in-place” — you can order into a upstartlightlightly allocated rank)

    Ce you program to allocate a catholic ample rank up front, you may deficiency to accept the pristine reckon in your postulates rasps be the reckon of how multifarious purposeless aggregate are in the rasp.

    …Or it could be a enjoin direction parameter to your program.

    Your rendering of these programs should involve maintenance reckon total occasion your program collates span items from the schedule.

    Imstereotype the reckon of similaritys when the programs ends.

    It is up to you how you conceal the “reckon of similaritys” divided from the ordered quenchedput schedule.

    On Linux, you potentiality authentication stdquenched and stderr…

    …or you potentiality furnish raspnames as enjoin direction args to your programs, and the merely stdquenched constitute your program earn be the stats on how multifarious similarity steps were authenticationd to order the      schedule.

    …or you potentiality accept a enjoin direction arg with a raspname specifically to **append** stats to

    Transcribe automated script(s) to blend the tests of your programs opposing rasps of manifold greatnesss.

    conceal in impetus the subject-matter of these scripts is to compel it easier to re-blend tests as demanded and assemble postulates in an unconfused, easy-to-authentication constitute

    compel your scripts as authenticationful as you demand them to be to catch yourself occasion

    Example: possibly furnish the script enjoin direction args to repress the perfect of rasp greatnesss to tests…

    …so you you can re-blend merely tests that demand to be re-run.

    Input rasp greatnesss should be total dominion of span from 21 to at lowest 226 aggregate.

    NOTE: your dream order earn probably be so dull ce the catholicst rasp greatnesss.

    Once your tests set-on-foot entrance hinder to a half hour or an hour, possibly it is occasion to bung, eventual you can allow it blend aggravate night…

    Rasp greatnesss (reckon of directions, reckon of purposeless aggregate in the schedule) should be:

    2, 4, 8, 16, 32, 64….. 1024, 2048, 4K, 8K, 16K….. 1048576, 2M, 4M, 8M… 32M, 64M

    that is the reckon of directions — the reckon of purposeless aggregate in each schedule…

    …files greatnesss potentiality each be single direction longer ifyou put the greatness on the pristine direction

    Try to go catholicr if your environment can touch it and the orders are referable entrance so long

    Create Plots: (perceive extremity on repressling axes in Octave)

    # of Similarity Steps:

    Add vs. Dream — Fine Rasps

    pick-quenched a perfect ce x & y axes that allows you amply collate the span opportunity they are stationary subordinately hinder

    x from 0..32 or 64 probably abquenched right

    pick-quenched a perfect ce y that fits

    Add vs. Dream — catholic rasps

    now do the liberal perfect of rasp greatnesss (can stationary set-on-foot at x axis at 0)

    at this subject-matter you should perceive that single of them closely disappears collated to the other

    Time:

    Add v Dream, fine perfect

    Same basic subject as with the # of similarity steps

    Ce the fineest rasps, the gentleman occasion is slight so fine to amply measure

    Pick-quenched a catholic ample perfect a that at lowest a lacking results of twain methods are > 1 ms

    Add v Dream, catholic perfect

    Same basic subject as with the # of similarity steps

    Expert Reckoner-argument

     

    The ordering ce the add and dream order implied of implantation order are as follows:

    Sorting.java

    /*
    * To qualify this indulge header, pick-quenched Indulge Headers in Project Properties.
    * To qualify this template rasp, pick-quenched Sols | Templates
    * and notorious the template in the editor.
    */
    package ordering;

    import java.io.IOException;
    import java.util.Scanner;

    /**
    *
    * @author Akshay Bisht
    */
    public adjust Ordering {

    /**
    * @param args the enjoin direction arguments
    */
    public static nugatory main(String[] args) throws IOException {
    Order st =newlight Order();
    st.getData();
    System.out.println(“=================Sorting Algorithms=================”);
    System.out.println(“1. Dream Order”);
    System.out.println(“2. Excellention Order”);
    System.out.println(“3. Implantation Order”);
    System.out.println(“Enter your rare”);
    Scanner scrutinize = upstartlightlightlight Scrutinizener(System.in);
    int rare = scrutinize.nextInt();
    if(rare == 1)
    st.bubble();
    else if(rare == 2)
    st.select();
    else if(rare ==3)
    st.insert();
    st.print();
    }
    }

    Sort.java

    /*
    * To qualify this indulge header, pick-quenched Indulge Headers in Project Properties.
    * To qualify this template rasp, pick-quenched Sols | Templates
    * and notorious the template in the editor.
    */
    package ordering;

    import java.io.IOException;
    import java.util.Scanner;

    /**
    *
    * @author Akshay Bisht
    */
    public adjust Order {
    int arr1[],temp,i,j,num,reckon = 0;
    Scanner sc = upstartlightlightlight Scrutinizener(System.in);
    public nugatory getData()throws IOException {

    System.out.println(“Enter the reckon of elements to be ordered: “);
    num = sc.nextInt();
    System.out.println(“Enter the aggregate: “);
    arr1 = upstartlightlightlight int[50];
    for(i=0;i<num;i++)
    {
    arr1[i] = sc.nextInt();
    }
    }

    public nugatory dream()throws IOException {
    for(i=0;i<num;i++)
    {
    for(j=0;j<num-1;j++)
    {
    if(arr1[j]>arr1[i])
    {
    temp = arr1[j];
    arr1[j]=arr1[i];
    arr1[i]=temp;
    count++;
    }
    }
    System.out.println(“The reckon of swaps are as follows: “+count);
    }
    }

    public nugatory excellent()throws IOException{

    for(i=0;i<num;i++)
    {
    int a = i;
    for(j= i+1;j<num;j++)
    {
    if(arr1[j]<arr1[a])
    a = j;
    int fine = arr1[a];
    arr1[a] = arr1[i];
    arr1[i] = fine;
    count++;
    }
    System.out.println(“The reckon of swaps are as follows: “+count);
    }
    }

    public nugatory implant()throws IOException {
    for(i=0;i<num;i++)
    {
    j=i;
    int b =arr1[i];
    while((j>0)&&(arr1[j-1]>b))
    {
    arr1[j] = arr1[j-1];
    j–;
    }
    arr1[j] = b;
    count++;
    }
    System.out.println(“The reckon of swaps are as follows: “+count);
    }

    public nugatory imprint()throws IOException {
    System.out.println(“The rank succeeding ordering is: “);
    for(i=0;i<num;i++)
    {
    System.out.println(arr1[i]);
    }
    }
    }

    Output: