Question
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
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 { 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 { MergeIterations++; if (dataset.size() == 1) return dataset; else { //split data into 2 parts ArrayList ArrayList dataset = Merge(MergeSort(LeftData), MergeSort(RightData)); } return dataset; } //merge the left and the right lists together static ArrayList { 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started