Question

On the following code there is an error bc you are not reverting back to the...

On the following code there is an error bc you are not reverting back to the original order after a sort. For the next sort you are passing the same reference variable to the next method. But that will point to the same (already sorted) array on the memory. Hence after the first sorting method, all three sorting methods are working on the already sorted array.

Do the following : Just copy each data set to 4 different arrays - one for each sorting technique and pass that to the sorting method.


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class SortAll {

private int[] sorArr;
private int[] MergArr;
private int length;

public static int[] SelectionSort(int[] OrigArr) {
for (int i = 0; i < OrigArr.length - 1; i++) {
int loc = i;
for (int j = i + 1; j < OrigArr.length; j++) {
if (OrigArr[j] < OrigArr[loc]) {
loc = j;
}
}

int small = OrigArr[loc];
OrigArr[loc] = OrigArr[i];
OrigArr[i] = small;
}
return OrigArr;
}

public static int[] InsertionSort(int[] OrigArr) {
int temp;
for (int i = 1; i < OrigArr.length; i++) {
for (int j = i; j > 0; j--) {
if (OrigArr[j] < OrigArr[j - 1]) {
temp = OrigArr[j];
OrigArr[j] = OrigArr[j - 1];
OrigArr[j - 1] = temp;
}
}
}
return OrigArr;
}

public void sort(int originalArr[]) {
this.sorArr = originalArr;
this.length = originalArr.length;
this.MergArr = new int[length];
MergeSorting(0, length - 1);
}

private void MergeSorting(int low, int high) {

if (low < high) {
int mid = low + (high - low) / 2;
// Sorting left side of sorArr in the below step
MergeSorting(low, mid);
// Sorting right side of sorArr in the below step
MergeSorting(mid + 1, high);
// Merging both parts
MergingParts(low, mid, high);
}
}

private void MergingParts(int low, int mid, int high) {

for (int i = low; i <= high; i++) {
MergArr[i] = sorArr[i];
}
int i = low;
int j = mid + 1;
int k = low;
while (i <= mid && j <= high) {
if (MergArr[i] <= MergArr[j]) {
sorArr[k] = MergArr[i];
i++;
} else {
sorArr[k] = MergArr[j];
j++;
}
k++;
}
while (i <= mid) {
sorArr[k] = MergArr[i];
k++;
i++;
}
}

public void quick(int[] inputArr) {

if (inputArr == null || inputArr.length == 0) {
return;
}
this.sorArr = inputArr;
length = inputArr.length;
quickSort(0, length - 1);
}

private void quickSort(int low, int high) {

int i = low;
int j = high;
//In the below step, we are calculating pivot number
int pivot = sorArr[low + (high - low) / 2];
// Dividing the Array into two Arrays.
while (i <= j) {
while (sorArr[i] < pivot) {
i++;
}
while (sorArr[j] > pivot) {
j--;
}
if (i <= j) {
exchNum(i, j);
//Moving index to next position of the array
i++;
j--;
}
}
// calling quickSort() recursively
if (low < j) {
quickSort(low, j);
}
if (i < high) {
quickSort(i, high);
}
}

private void exchNum(int i, int j) {
int temp = sorArr[i];
sorArr[i] = sorArr[j];
sorArr[j] = temp;
}

public static void main(String[] args) throws FileNotFoundException {
SortAll sortArr = new SortAll();
long start;
long end;
long total;
int[] outputArr;
int[] randomNumArr = new int[200];
int[] equalNumArr = new int[200];
int[] sortedIncArr = new int[200];
int[] sortedDecArr = new int[200];
String[] one = new String[200];
String[] two = new String[200];
String[] three = new String[200];
String[] four = new String[200];
/// Scanner should go here
Scanner sc = new Scanner(System.in);
System.out.println("Enter file location: ");
String filename = sc.nextLine();
File file = new File(filename);
Scanner reader = new Scanner(file);

try
{

//while (reader.hasNextLine()) {
//{
String lineJustFetched = reader.nextLine();
{
one = lineJustFetched.split(", ");
for(int i=0;i randomNumArr[i] = Integer.parseInt(one[i]);
}

}
{
two = lineJustFetched.split(", ");
for(int i=0;i equalNumArr[i] = Integer.parseInt(two[i]);
}
}
{
three = lineJustFetched.split(", ");
for(int i=0;i sortedIncArr[i] = Integer.parseInt(three[i]);
}

}
{
four = lineJustFetched.split(", ");
for(int i=0;i sortedDecArr[i] = Integer.parseInt(four[i]);
}
}
}
//}
//}
finally
{
reader.close();
}

///// Manually inputed the number from datafile
//int[] randomNumArr = {492, 501, 257, 390, 478, 77, 386, 140, 318, 383, 471, 317, 483, 191, 48, 189, 458, 482, 254, 142, 31, 27, 470, 312, 416, 327, 316, 222, 358, 464, 98, 130, 52, 18, 291, 201, 138, 81, 264, 255, 400, 346, 85, 144, 393, 100, 7, 351, 218, 282, 38, 124, 448, 495, 397, 409, 318, 131, 147, 446, 292, 413, 339, 271, 325, 277, 124, 376, 321, 142, 20, 43, 173, 88, 285, 314, 58, 1, 318, 131, 147, 446, 292, 413, 339, 271, 325, 277, 124, 376, 321, 142, 20, 43, 173, 88, 285, 314, 58, 1, 240, 184, 247, 158, 299, 15, 102, 124, 14, 153, 207, 180, 22, 55, 121, 294, 467, 160, 168, 411, 228, 400, 415, 36, 130, 161, 26, 248, 183, 15, 108, 367, 447, 372, 338, 131, 142, 139, 233, 164, 302, 203, 308, 60, 455, 386, 30, 312, 164, 143};
//int[] equalNumArr = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
//int[] sortedIncArr = {9, 13, 15, 17, 18, 23, 23, 30, 33, 41, 48, 48, 56, 57, 66, 71, 71, 72, 76, 77, 79, 83, 88, 91, 104, 105, 114, 115, 115, 119, 123, 141, 142, 143, 147, 148, 150, 159, 167, 168, 172, 183, 184, 191, 197, 202, 206, 207, 210, 210, 222, 227, 231, 232, 232, 234, 238, 243, 253, 256, 257, 264, 269, 279, 279, 279, 284, 290, 301, 309, 311, 313, 314, 315, 316, 317, 318, 326, 327, 328, 328, 331, 331, 335, 342, 349, 355, 362, 366, 367, 367, 376, 377, 380, 386, 389, 390, 393, 396, 397, 400, 402, 411, 412, 413, 414, 415, 417, 418, 426, 433, 433, 447, 457, 464, 467, 468, 472, 476, 477, 479, 482, 484, 485, 487, 488, 490, 501};
// int[] sortedDecArr = {501, 490, 488, 487, 485, 484, 482, 479, 477, 476, 472, 468, 467, 464, 457, 447, 433, 433, 426, 418, 417, 415, 414, 413, 412, 411, 402, 400, 397, 396, 393, 390, 389, 386, 380, 377, 376, 367, 367, 366, 362, 355, 349, 342, 335, 331, 331, 328, 328, 327, 326, 318, 317, 316, 315, 314, 313, 311, 309, 301, 290, 284, 279, 279, 279, 269, 264, 257, 256, 253, 243, 238, 234, 232, 232, 231, 227, 222, 210, 210, 207, 206, 202, 197, 191, 184, 183, 172, 168, 167, 159, 150, 148, 147, 143, 142, 141, 123, 119, 115, 115, 114, 105, 104, 91, 88, 83, 79, 77, 76, 72, 71, 71, 66, 57, 56, 48, 48, 41, 33, 30, 23, 23, 18, 17, 15, 13, 9};

//Running Selection Sort on Random Array
start = System.nanoTime();

outputArr = SelectionSort(randomNumArr);
end = System.nanoTime();
total = end - start;
System.out.println("Runtime For SELECTION SORT :\n");
for (int i : outputArr) {
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.print("--Random Array : " + total + " nanoseconds.\n");

//Running Selection Sort on Equal Array
start = System.nanoTime();
outputArr = SelectionSort(equalNumArr);
end = System.nanoTime();
total = end - start;
for (int i : outputArr) {
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.print("--Equal Array : " + total + " nanoseconds.\n");

//Running Selection Sort on Sorted Array Increasing order
start = System.nanoTime();
outputArr = SelectionSort(sortedIncArr);
end = System.nanoTime();
total = end - start;
for (int i : outputArr) {
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.print("--Sorted in Increasing Order Array : " + total + " nanoseconds.\n");

//Running Selection Sort on Sorted Array Decreasing order
start = System.nanoTime();
outputArr = SelectionSort(sortedDecArr);= System.nanoTime();
total = end - start;

for (int i : outputArr) {
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.print("--Sorted in Decreasing Order Array : " + total + " nanoseconds.\n");

//Running Insertion Sort
start = System.nanoTime();
outputArr = InsertionSort(randomNumArr);
end = System.nanoTime();
total = end - start;

System.out.println("\nRuntime For Insertion Sort :\n");
for (int i : outputArr) {
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.print("--Random Array : " + total + " nanoseconds.\n");

//Running Selection Sort on Equal Array
start = System.nanoTime();
outputArr = InsertionSort(equalNumArr);
end = System.nanoTime();
total = end - start;
for (int i : outputArr) {
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.print("--Equal Array : " + total + " nanoseconds.\n");

//Running Selection Sort on Sorted Array Increasing order
start = System.nanoTime();
outputArr = InsertionSort(sortedIncArr);
end = System.nanoTime();
total = end - start;
for (int i : outputArr) {
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.print("--Sorted in Increasing Order Array : " + total + " nanoseconds.\n");

//Running Selection Sort on Sorted Array Decreasing order
start = System.nanoTime();
outputArr = InsertionSort(sortedDecArr);
end = System.nanoTime();
total = end - start;

for (int i : outputArr) {
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.print("--Sorted in Decreasing Order Array : " + total + " nanoseconds.\n");

//Runtime for Merge Sort
System.out.println("\nRuntime For Merge Sort :\n");
outputArr = randomNumArr;
start = System.nanoTime();
sortArr.sort(outputArr);
end = System.nanoTime();
total = end - start;

for (int i : outputArr) {
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.print("--Random Array : " + total + " nanoseconds.\n");

outputArr = randomNumArr;
start = System.nanoTime();
sortArr.sort(outputArr);
end = System.nanoTime();
total = end - start;

for (int i : outputArr) {
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.print("--Equal Array : " + total + " nanoseconds.\n");

outputArr = randomNumArr;
start = System.nanoTime();
sortArr.sort(outputArr);
end = System.nanoTime();
total = end - start;

for (int i : outputArr) {
System.out.print(i);
System.out.print(" ");
}.out.println();
System.out.print("--Sorted in Increasing Order Array : " + total + " nanoseconds.\n");

outputArr = randomNumArr;
start = System.nanoTime();
sortArr.sort(outputArr);
end = System.nanoTime();
total = end - start;

for (int i : outputArr) {
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.print("--Sorted in Decreasing Order Array : " + total + " nanoseconds.\n");

//Runtime FOr QuickSort
System.out.println("\nRuntime For Quick Sort :\n");
outputArr = randomNumArr;
start = System.nanoTime();.quick(outputArr);
end = System.nanoTime();
total = end - start;

for (int i : outputArr) {
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.print("--Random Array : " + total + " nanoseconds.\n");

outputArr = randomNumArr;
start = System.nanoTime();
sortArr.quick(outputArr);
end = System.nanoTime();
total = end - start;

for (int i : outputArr) {
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.print("--Equal Array : " + total + " nanoseconds.\n");

outputArr = randomNumArr;
start = System.nanoTime();
sortArr.quick(outputArr);
end = System.nanoTime();
total = end - start;

for (int i : outputArr) {
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.print("--Sorted in Increasing Order Array : " + total + " nanoseconds.\n");

outputArr = randomNumArr;
start = System.nanoTime();
sortArr.quick(outputArr);
end = System.nanoTime();
total = end - start;

for (int i : outputArr) {
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.print("--Sorted in Decreasing Order Array : " + total + " nanoseconds.\n");

}

}

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
On the following code there is an error bc you are not reverting back to the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • 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...

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

  • The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also...

    The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also please explain why the seg fault is happening. Thank you #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> // @Name loadMusicFile // @Brief Load the music database // 'size' is the size of the database. char** loadMusicFile(const char* fileName, int size){ FILE *myFile = fopen(fileName,"r"); // Allocate memory for each character-string pointer char** database = malloc(sizeof(char*)*size); unsigned int song=0; for(song =0; song < size;...

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

  • please illistrate a UML diagram for the following code bellow, it should have 3 rows, 1...

    please illistrate a UML diagram for the following code bellow, it should have 3 rows, 1 colum like the one bellow, first box should have the class name, second box is for class attributes, and third box should have the class operations/methods and please put a (+) for public and a (-) for private example: class +-attributes +-Operations Also make a JAVADOCs, but not generated by a compiler, to explain what is going on in the code import java.util.Random; public...

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

  • Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative perfo...

    Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative performance of the algorithms for a variety of data sets. Need Help With this Sorting Algorithm task for C++ Base Code for sorting.cpp is given. The header file is not included in this. Help would be much appreciated as I have not started on this due to personal reasons #include <cstdlib> #include <iostream> #include <getopt.h> using namespace std; long compares; // for counting...

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

  • I need to program 3 and add to program 2 bellows: Add the merge sort and...

    I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into 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