Question

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 Iterations1 = 0, Iterations2 = 0, Iterations3 = 0, Iterations4 =0;

public static void main(String[] args) throws IOException
{
int A = 0, Temp = 0;

System.out.println("This program compares the bubble, selection, merge, and radix sorts ");
System.out.println("The data set is 78498 unsorted integers ");
System.out.println("Begining bubble sort, Please wait! ");

//reading text file
File file = new File("primes1.txt");
Scanner infile = new Scanner(file);

//reading numbers from file and storing back to arraylists
while (infile.hasNextInt())
{
A = infile.nextInt();
Data1.add(A);
Data2.add(A);
Data3.add(A);
Data4.add(A);
}

// Bubble Sort
long startTime = System.nanoTime();
for (int x = 0; x < Data1.size(); x++)
{
//System.out.println(x);
//bubble sorting
for (int y = 0; y < Data1.size() - 1 - x; y++)
{
if (Data1.get(y) > Data1.get(y + 1))
{
Iterations1++;
Temp = Data1.get(y);
Data1.set(y, Data1.get(y + 1));
Data1.set(y + 1, Temp);
}
}
}
//printing results
System.out.println("..................................");
long endTime = System.nanoTime();
System.out.println("Bubble Sort Seconds = " + (double) ((endTime - startTime) / 1000000000.0));
System.out.println("Iterations: " + Iterations1);
System.out.print("First Ten numbers: ");

for (int x = 0; x < 10; x++)
{
System.out.print(Data1.get(x) + " ");
}

System.out.println();

//Print last 10
System.out.print("Last Ten numbers: ");
for (int x = Data1.size() - 10; x < Data1.size(); x++)
{
System.out.print(Data1.get(x) + " ");
}
System.out.println();

  
//selection sorting
startTime = System.nanoTime();
for (int i = 0; i < Data2.size() - 1; i++)
{
int index = i;
for (int j = i + 1; j < Data2.size(); j++)
{
if (Data2.get(j) < Data2.get(index))
{
index = j;
}
}
Iterations2++;
int smallerNumber = Data2.get(index);
Data2.set(index, i);
Data2.set(i, smallerNumber);
}
endTime = System.nanoTime();

//printing results
System.out.println("..................................");
System.out.println("Selection Sort Seconds = " + (double) ((endTime - startTime) / 1000000000.0));
System.out.println("Iterations: "+Iterations2);
System.out.print("First Ten numbers: ");
for (int x = 0; x < 10; x++)
{
System.out.print(Data2.get(x) + " ");
}
System.out.println();

System.out.print("Last Ten numbers: ");
for (int x = Data2.size()-10; x < Data2.size(); x++)
{
System.out.print(Data2.get(x) + " ");
}
System.out.println();
  
//Merge sorting
startTime = System.nanoTime();
//calling method
Data3 = MergeSort(Data3);
endTime = System.nanoTime();
//Printing results
System.out.println("..................................");
System.out.println("Merge Sort Seconds = " + (double) ((endTime - startTime) / 1000000000.0));
System.out.println("Iterations: "+Iterations3);
System.out.print("First Ten numbers: ");
for (int x = 0; x < 10; x++)
{
System.out.print(Data3.get(x) + " ");
}
System.out.println();

System.out.print("Last Ten numbers: ");
for (int x = Data3.size()-10; x < Data3.size(); x++)
{
System.out.print(Data3.get(x) + " ");
}
System.out.println();
}

//merge sorting methods
static ArrayList<Integer> MergeSort(ArrayList<Integer> dataset)
{
Iterations3++;
if (dataset.size() == 1) {
return dataset;
} else
{

// split data into 2 parts
ArrayList<Integer> LeftData = new ArrayList<Integer>(dataset.subList(0, dataset.size() / 2));
ArrayList<Integer> RightData = new ArrayList<Integer>(dataset.subList(dataset.size() / 2, dataset.size()));
dataset = Merge(MergeSort(LeftData), MergeSort(RightData));
}
return dataset;
}

// merge the left and right lists together
static ArrayList<Integer> Merge(ArrayList<Integer> LeftData, ArrayList<Integer> RightData)
{
ArrayList<Integer> MergedData = new ArrayList<Integer>();
while (LeftData.size() > 0 || RightData.size() > 0) {
Iterations3++;
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;


}


}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

HI I modified  your and ran the output for 100 numbers and its showing the correct output now, modified code in bold.

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 Iterations1 = 0, Iterations2 = 0, Iterations3 = 0, Iterations4 =0;

public static void main(String[] args) throws IOException
{
int A = 0, Temp = 0;

System.out.println("This program compares the bubble, selection, merge, and radix sorts ");
System.out.println("The data set is 78498 unsorted integers ");
System.out.println("Begining bubble sort, Please wait! ");

//reading text file
File file = new File("primes1.txt");
Scanner infile = new Scanner(file);

//reading numbers from file and storing back to arraylists
while (infile.hasNextInt())
{
A = infile.nextInt();
Data1.add(A);
Data2.add(A);
Data3.add(A);
Data4.add(A);
}

// Bubble Sort
long startTime = System.nanoTime();
for (int x = 0; x < Data1.size(); x++)
{
//System.out.println(x);
//bubble sorting
for (int y = 0; y < Data1.size() - 1 - x; y++)
{
if (Data1.get(y) > Data1.get(y + 1))
{
Iterations1++;
Temp = Data1.get(y);
Data1.set(y, Data1.get(y + 1));
Data1.set(y + 1, Temp);
}
}
}
//printing results
System.out.println("..................................");
long endTime = System.nanoTime();
System.out.println("Bubble Sort Seconds = " + (double) ((endTime - startTime) / 1000000000.0));
System.out.println("Iterations: " + Iterations1);
System.out.print("First Ten numbers: ");

for (int x = 0; x < 10; x++)
{
System.out.print(Data1.get(x) + " ");
}

System.out.println();

//Print last 10
System.out.print("Last Ten numbers: ");
for (int x = Data1.size() - 10; x < Data1.size(); x++)
{
System.out.print(Data1.get(x) + " ");
}
System.out.println();

  
//selection sorting
startTime = System.nanoTime();
for (int i = 0; i < Data2.size() - 1; i++)
{
int index = i;
for (int j = i + 1; j < Data2.size(); j++)
{
if (Data2.get(j) < Data2.get(index))
{
index = j;
}
}
Iterations2++;
int smallerNumber = Data2.get(index);
Data2.set(index, Data2.get(i));
Data2.set(i, smallerNumber);
}
endTime = System.nanoTime();

//printing results
System.out.println("..................................");
System.out.println("Selection Sort Seconds = " + (double) ((endTime - startTime) / 1000000000.0));
System.out.println("Iterations: "+Iterations2);
System.out.print("First Ten numbers: ");
for (int x = 0; x < 10; x++)
{
System.out.print(Data2.get(x) + " ");
}
System.out.println();

System.out.print("Last Ten numbers: ");
for (int x = Data2.size()-10; x < Data2.size(); x++)
{
System.out.print(Data2.get(x) + " ");
}
System.out.println();
  
//Merge sorting
startTime = System.nanoTime();
//calling method
Data3 = MergeSort(Data3);
endTime = System.nanoTime();
//Printing results
System.out.println("..................................");
System.out.println("Merge Sort Seconds = " + (double) ((endTime - startTime) / 1000000000.0));
System.out.println("Iterations: "+Iterations3);
System.out.print("First Ten numbers: ");
for (int x = 0; x < 10; x++)
{
System.out.print(Data3.get(x) + " ");
}
System.out.println();

System.out.print("Last Ten numbers: ");
for (int x = Data3.size()-10; x < Data3.size(); x++)
{
System.out.print(Data3.get(x) + " ");
}
System.out.println();
}

//merge sorting methods
static ArrayList<Integer> MergeSort(ArrayList<Integer> dataset)
{
Iterations3++;
if (dataset.size() == 1) {
return dataset;
} else
{

// split data into 2 parts
ArrayList<Integer> LeftData = new ArrayList<Integer>(dataset.subList(0, dataset.size() / 2));
ArrayList<Integer> RightData = new ArrayList<Integer>(dataset.subList(dataset.size() / 2, dataset.size()));
dataset = Merge(MergeSort(LeftData), MergeSort(RightData));
}
return dataset;
}

// merge the left and right lists together
static ArrayList<Integer> Merge(ArrayList<Integer> LeftData, ArrayList<Integer> RightData)
{
ArrayList<Integer> MergedData = new ArrayList<Integer>();
while (LeftData.size() > 0 || RightData.size() > 0) {
Iterations3++;
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;


}


}

Output:

*Feel free to ask(comment) if you have any doubts, I will be happy to answer you, please upvote if you like the solution

Add a comment
Know the answer?
Add Answer to:
Hi All, Can someone please help me correct the selection sort method in my program. the...
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 can I make this program sort strings that are in a text file and not...

    How can I make this program sort strings that are in a text file and not have the user type them in? I need this done by 5:00 AM EST tommorow please. import java.util.*; public class MergeDemo { public static void main(String args[]) { Scanner input=new Scanner(System.in); System.out.print("How many lines to be sorted:"); int size=input.nextInt(); String[] lines=new String[size]; lines[0]=input.nextLine(); System.out.println("please enter lines..."); for(int i=0;i { lines[i]=input.nextLine(); } System.out.println(); System.out.println("Lines Before Sorting:"); System.out.println(Arrays.toString(lines)); mergeSort(lines); System.out.println(); System.out.println("Lines after Sorting:"); System.out.println(Arrays.toString(lines)); } public...

  • PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java,...

    PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java, Write a computer program that prompts the user for one number, n for the number of items in the array to sort, and create and sort 1000 arrays of this size timing the run to get an average time to sort an array of this size. Then do the following: Initiate a variable running_time to 0 Create a for loop that iterates 1000 times....

  • Please help me to solve the problem with java language! An implementation of the Merge Sort...

    Please help me to solve the problem with java language! An implementation of the Merge Sort algorithm. Modify the algorithm so that it splits the list into 3 sublists (instead of two). Each sublist should contain about n/3 items. The algorithm should sort each sublist recursively and merge the three sorted sublists. The traditional merge sort algorithm has an average and worst-case performance of O(n log2 n). What is the performance of the 3-way Merge Sort algorithm? Merge Sort algorithm...

  • 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!   ...

  • use the same code. but the code needs some modifications. so use this same code and...

    use the same code. but the code needs some modifications. so use this same code and modify it and provide a output Java Program to Implement Merge Sort import java.util.Scanner Class MergeSort public class MergeSort Merge Sort function / public static yoid sortfintfl a, int low, int high) int N-high-low; if (N1) return; int mid- low +N/2; Il recursively sort sort(a, low, mid); sort(a, mid, high); I/ merge two sorted subarrays int] temp new int[N]; int i- low, j-mid; for...

  • How to measure the average performance of sorting algorithms with different sizes and numbers(with arrayList<Integer>) and...

    How to measure the average performance of sorting algorithms with different sizes and numbers(with arrayList<Integer>) and compare with a graph? I have these methods: //This one receives one size(n) parameter    public static ArrayList<Integer> RandomArray(int n){               ArrayList<Integer> randomArray = new ArrayList<Integer>(n);               Random rand = new Random(); //--- random number        rand.setSeed(System.currentTimeMillis());               for(int i = 0; i < n; i++ ) {            //loop for creating...

  • I have a multithreaded java sorting program that works as follows: 1. A list of double...

    I have a multithreaded java sorting program that works as follows: 1. A list of double values is divided into two smaller lists of equal size 2. Two separate threads (which we will term sorting threads) sort each sublist using a sorting algorithm of your choice 3. The two sublists are then merged by a third thread merging thread that merges the two sublists into a single sorted list. SIMPLE EXECUTION >java SortParallel 1000 Sorting is done in 8.172561ms when...

  • 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,...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • Objective: in Java Write a program that implements 3 sorting algorithms and times them in real ti...

    Objective: in Java Write a program that implements 3 sorting algorithms and times them in real time. These algorithms will sort Cylinders by their volume. First, download the driver and include it in your project. Write a class Cylinder with the following properties baseRadius: a non-negative number that corresponds to the Cylinder’s base’s radius. height: a non-negative number that corresponds to the Cylinder’s height. Next, write a class Sorter with the following bubbleSort: This static method takes in an array...

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