Answered Essay: 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:

Sorting.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package sorting;

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

/**
*
* @author Akshay Bisht
*/
public class Sorting {

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
Sort st =new Sort();
st.getData();
System.out.println(“=================Sorting Algorithms=================”);
System.out.println(“1. Bubble Sort”);
System.out.println(“2. Selection Sort”);
System.out.println(“3. Insertion Sort”);
System.out.println(“Enter your choice”);
Scanner scan = new Scanner(System.in);
int choice = scan.nextInt();
if(choice == 1)
st.bubble();
else if(choice == 2)
st.select();
else if(choice ==3)
st.insert();
st.print();
}
}

Sort.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package sorting;

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

/**
*
* @author Akshay Bisht
*/
public class Sort {
int arr1[],temp,i,j,num,count = 0;
Scanner sc = new Scanner(System.in);
public void getData()throws IOException {

System.out.println(“Enter the number of elements to be sorted: “);
num = sc.nextInt();
System.out.println(“Enter the numbers: “);
arr1 = new int[50];
for(i=0;i<num;i++)
{
arr1[i] = sc.nextInt();
}
}

public void bubble()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 number of swaps are as follows: “+count);
}
}

public void select()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 small = arr1[a];
arr1[a] = arr1[i];
arr1[i] = small;
count++;
}
System.out.println(“The number of swaps are as follows: “+count);
}
}

public void insert()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 number of swaps are as follows: “+count);
}

public void print()throws IOException {
System.out.println(“The array after sorting is: “);
for(i=0;i<num;i++)
{
System.out.println(arr1[i]);
}
}
}

Output:

Buy Essay
Calculate your paper price
Pages (550 words)
Approximate price: -

Help Me Write My Essay - Reasons:

Best Online Essay Writing Service

We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.

Our Writers are Experienced and Professional

Our essay writing service is founded on professional writers who are on stand by to help you any time.

Free Revision Fo all Essays

Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.

Timely Essay(s)

We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.

Customised Essays &100% Confidential

Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.

24/7 Customer Support

Our agents are ready to help you around the clock. Please feel free to reach out and enquire about anything.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

HOW OUR ONLINE ESSAY WRITING SERVICE WORKS

Let us write that nagging essay.

STEP 1

Submit Your Essay/Homework Instructions

By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.

STEP 2

Pick A & Writer

Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.

STEP 3

Editing (OUR PART)

At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.

STEP 4

Receive your Paper

After Editing, your paper will be sent to you via email.

× How can I help you?