Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a program named sort.java that reads in primes1.txt. Sort these numbers using bubble, selection, merge, and radix sorts. Each sort should list the elapsed

Create a program named sort.java that reads in primes1.txt. Sort these numbers using bubble, selection, merge, and radix sorts. Each sort should list the elapsed time, iterations, and print the first 10 and last 10 numbers in the sorted array.

Example output This program compares the bubble, selection, merge, and radix sorts.

The data set is 78498 unsorted integers

1. Bubble Sort Seconds to sort = ???

Number of iterations = ???

First 10 - 2 3 5 7 11 17 23 29 31 37

Last 10 - 999883 999863 999907 999917 999931 999953 999959 999961 999979 999983

2. Selection Sort Seconds to sort = ???

Number of iterations = ???

First 10 - 2 3 5 7 11 17 23 29 31 37

Last 10 - 999883 999863 999907 999917 999931 999953 999959 999961 999979 999983

3. Merge Sort Seconds to sort = ???

Number of iterations = ???

First 10 - 2 3 5 7 11 17 23 29 31 37

Last 10 - 999883 999863 999907 999917 999931 999953 999959 999961 999979 999983

4. Radix Sort Seconds to sort = ???

Number of iterations = ???

First 10 - 2 3 5 7 11 17 23 29 31 37

Last 10 - 999883 999863 999907 999917 999931 999953 999959 999961 999979 999983

My program so far is this

package sort;

import java.io.*;

import java.util.*;

public class Sort

{

static int MergeIterations = 0;

public static void main(String[] args) throws IOException

{

File file = new File("primes1.txt");

Scanner infile2 = new Scanner(file);

Scanner infile = new Scanner(file);

//count prime numbers in file

int N = 0;

while (infile2.hasNextInt())

{

infile2.nextInt();

N++;

}

System.out.println("Counted " + N + " primes numbers in file");

int[] data1 = new int[N]; //use for bubble

int[] data2 = new int[N]; //use for selection

ArrayList data3 = new ArrayList();

ArrayList data4 = new ArrayList();

//read N numbers into array

int Q;

for (int i=0; i

{

Q = infile.nextInt();

data1[i] = Q;

data2[i] = Q;

data3.add(Q);

data4.add(Q);

}

System.out.println("The dataset is " + N + " unsorted integers");

data3 = MergeSort(data3);

//print 1st 10 sorted numbers

System.out.println("First 10 Merge sorted numbers: ");

for (int i=0; i<10; i++)

{

System.out.print(data3.get(i) + " ");

}

System.out.println(" ");

//radix sort

System.out.println("Beginning Radix Sort");

data4 = RadixSort(data4);

System.out.println("First 10 Radix sorted numbers: ");

for (int i=0; i<10; i++)

{

System.out.println(data4.get(i) + " ");

}

}

static ArrayList RadixSort(ArrayList dataset)

{

ArrayList dataset2 = new ArrayList();

for (int k=1; k<=100000; k*=10)

{

for (int i=0; i<=9; i++)

{

for (int j=0; j

{

if (dataset.get(j)/k%10 == i)

dataset2.add(dataset.get(j));

}

}

dataset.clear();

for (int m=0; m

{

dataset.add(dataset2.get(m));

}

dataset2.clear();

}

return dataset;

}

static ArrayList MergeSort(ArrayList dataset)

{

MergeIterations++;

if (dataset.size() == 1) return dataset;

else

{

//split data into 2 parts

ArrayList LeftData = new ArrayList (dataset.subList(0, dataset.size()/2));

ArrayList RightData = new ArrayList (dataset.subList(dataset.size()/2, dataset.size()));

dataset = Merge(MergeSort(LeftData), MergeSort(RightData));

}

return dataset;

}

//merge the left and the right lists together

static ArrayList Merge(ArrayList LeftData, ArrayList RightData)

{

ArrayList MergedData = new ArrayList();

while (LeftData.size() > 0 || RightData.size() > 0)

{

MergeIterations++;

if (RightData.size() == 0)

{

MergedData.add(LeftData.get(0));

LeftData.remove(0);

}

else if (LeftData.size() == 0)

{

MergedData.add(RightData.get(0));

RightData.remove(0);

}

else if (LeftData.get(0) < RightData.get(0))

{

MergedData.add(LeftData.get(0));

LeftData.remove(0);

}

else

{

MergedData.add(RightData.get(0));

RightData.remove(0);

}

}

return MergedData;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Fred R. McFadden, Jeffrey Slater, Mary B. Prescott

5th Edition

0805360549, 978-0805360547

More Books

Students also viewed these Databases questions

Question

1. Define the nature of interviews

Answered: 1 week ago

Question

2. Outline the different types of interviews

Answered: 1 week ago