Computer Science - Java Help Here\'s the assignment description: My code won\'t
ID: 3749962 • Letter: C
Question
Computer Science - Java Help
Here's the assignment description:
My code won't run....it has bugs. Please fix code and do #3. I can't attach the txt files, but there are 7 txt files each with random numbers to be sorted using merge sort and insertion sort. Please follow assignment and give formatted code. Thanks
Code that won't run:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class mergeSort {
public static void main(String args[]) throws FileNotFoundException {
int arr[],temp[];
String Filename[] = new String[]{"input_100.txt" , "input_1000.txt","input_5000.txt","input_10000.txt","input_50000.txt","input_100000.txt", "input_500000.txt"};
//Save filenames in a variable
double times[] = new double [Filename.length];
int file1=0,file2=0,file3=0,file4=0,file5=0,file6=0,file7=0;
Scanner number;
for (int n = 0; n < Filename.length; n++) {
int mySum = 0;
number = new Scanner(new File (Filename[n]));
while (number.hasNextInt()) {
int x=number.nextInt();
// save values from file
mySum++;
//increments the value by 1 for each new value
}
//close the scanner
if( n== 0){
file1=mySum;
}
else if (n == 1){
file2=mySum;
}
else if (n == 2) {
file3=mySum;
}
else if (n == 3) {
file4=mySum;
}
else if (n == 4) {
file5=mySum;
}
else if (n == 5) {
file6=mySum;
}
else if (n == 6) {
file7=mySum;
}
number.close();
}
int index[] = {file1, file2, file3, file4, file5, file6, file7};
for (int n = 0; n < Filename.length; n++) {
arr = new int[index[n]];
temp=new int[index[n]];
long beginTime, finishTime, totalTime;
// declare variables to save time
System.out.println("File");
number = new Scanner (new File(Filename[n]));
if(n==0){
for(int i1=0; i1 < file1; i1++)
arr[i1]= Integer.parseInt(number.next());
}
else if (n==1) {
for(int i2=0; i2 < file2; i2++)
arr[i2]= Integer.parseInt(number.next());
}
else if (n==3) {
for(int i3=0; i3 < file3; i3++)
arr[i3]= Integer.parseInt(number.next());
}
else if (n==4) {
for(int i4=0; i4 < file4; i4++)
arr[i4]= Integer.parseInt(number.next());
}
else if (n==4) {
for(int i5=0; i5 < file5; i5++)
arr[i5]= Integer.parseInt(number.next());
}
else if (n==5) {
for(int i6=0; i6 < file6; i6++)
arr[i6]= Integer.parseInt(number.next());
}
else if (n==6) {
for(int i7=0; i7 < file7; i7++)
arr[i7]= Integer.parseInt(number.next());
}
beginTime = System.nanoTime();
finishTime = System.nanoTime();
totalTime = finishTime - beginTime;
times[n]=totalTime;
System.out.println("Recorded time : "+times[n]+" nanosecond to sort file "+Filename[n]);
number.close();
merge_sorting(arr, temp, 0 ,index[n]-1);
// runs insertion sort algorithm
for(int i=0; i<arr.length; i++)
{
System.out.print(arr[i]+" ");
}
System.out.println(" ");
}
System.out.println(" Sorting complete");
}
//function to sort array using merge sort
public static void merge_sorting(int arr[],int temp[],int low,int high){//lower index and higher index as argument to the function
if(low<high){//calculating if low>high then the array will be sorted else sort the array
int mid=low+(high-low)/2;//calculate mid index to break the array into two halves
merge_sorting(arr,temp,low,mid);//calling sorting function to sort the left subarray
merge_sorting(arr,temp,mid+1,high);//calling sorting function to sort the right subarray
merge(arr,temp,low,mid,high);//calling function to merge two subarrays
}
}
public static void merge(int arr[],int temp[],int low,int m, int high){//merging function
for(int i=0;i<=high;i++){//copying the value of the array in a temporary array ar1
temp[i]=arr[i];
}
int i=low,j=m+1,k=low;//initializing variable
while(i<=m && j<=high){//checking if the value of i is less than mid index and value of j is less than higher index
if(temp[i]<=temp[j]){//swap the values
arr[k]=temp[i];//put the sorted value in the original array
i++;//increment loop variable
}
else{
arr[k]=temp[j];//copy content of temporary variable to original array
j++;//increment loop variable
}
k++;
}
while(i<=m){//if value of i is less than the mid index then copy value of temporary array to the original array
arr[k]=temp[i];
k++;//increment loop variable
i++;
}
int file1= 0;
int file2 = 1;
int file3 = 2;
int file4 = 3;
int file5 = 4;
int file6 = 5;
int file7 = 6;
int index[] = {file1, file2, file3, file4, file5, file6, file7};
merge_sorting(arr, temp, 0 ,index[n]-1);
for(int i69=0; i69 < arr.length; i++)
{
System.out.print(arr[i]+" ");
}
System.out.println(" ");
System.out.println(" Sorting complete");
}
//function to sort array using merge sort
/*
public static void merge_sorting(int arr[],int temp[],int low,int high){//lower index and higher index as argument to the function
if(low<high){//calculating if low>high then the array will be sorted else sort the array
int mid=low+(high-low)/2;//calculate mid index to break the array into two halves
merge_sorting(arr,temp,low,mid);//calling sorting function to sort the left subarray
merge_sorting(arr,temp,mid+1,high);//calling sorting function to sort the right subarray
merge(arr,temp,low,mid,high);//calling function to merge two subarrays
}
}
*/
/*
public static void merge(int ar[],int temp[],int low,int m, int high){//merging function
for(int i=0;i<=high;i++){//copying the value of the array in a temporary array ar1
temp[i]=ar[i];
}
int i=low,j=m+1,k=low;//initializing variable
while(i<=m && j<=high){//checking if the value of i is less than mid index and value of j is less than higher index
if(temp[i]<=temp[j]){//swap the values
ar[k]=temp[i];//put the sorted value in the original array
i++;//increment loop variable
}
else{
ar[k]=temp[j];//copy content of temporary variable to original array
j++;//increment loop variable
}
k++;
}
while(i<=m){//if value of i is less than the mid index then copy value of temporary array to the original array
ar[k]=temp[i];
k++;//increment loop variable
i++;
}
}
*/
public static void insertionSort(int array[]) {
int n = array.length;
// declare array length
for (int j = 1; j < n; j++) {
// intialize the loop to sort
int key = array[j];
int i = j-1;
while ( (i > -1) && ( array [i] > key ) ) {
array [i+1] = array [i];
i--;
}
array[i+1] = key;
//the first element is given the smallest value
}
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class mergeSort {
public static void main(String args[]) throws FileNotFoundException {
int arr[],temp[];
String Filename[] = new String[]{"input_100.txt" , "input_1000.txt","input_5000.txt","input_10000.txt","input_50000.txt","input_100000.txt", "input_500000.txt"};
//Save filenames in a variable
double times[] = new double [Filename.length];
int file1=0,file2=0,file3=0,file4=0,file5=0,file6=0,file7=0;
Scanner number;
for (int n = 0; n < Filename.length; n++) {
int mySum = 0;
number = new Scanner(new File (Filename[n]));
while (number.hasNextInt()) {
int x=number.nextInt();
// save values from file
mySum++;
//increments the value by 1 for each new value
}
//close the scanner
if( n== 0){
file1=mySum;
}
else if (n == 1){
file2=mySum;
}
else if (n == 2) {
file3=mySum;
}
else if (n == 3) {
file4=mySum;
}
else if (n == 4) {
file5=mySum;
}
else if (n == 5) {
file6=mySum;
}
else if (n == 6) {
file7=mySum;
}
number.close();
}
int index[] = {file1, file2, file3, file4, file5, file6, file7};
for (int n = 0; n < Filename.length; n++) {
arr = new int[index[n]];
temp=new int[index[n]];
long beginTime, finishTime, totalTime;
// declare variables to save time
System.out.println("File");
number = new Scanner (new File(Filename[n]));
if(n==0){
for(int i1=0; i1 < file1; i1++)
arr[i1]= Integer.parseInt(number.next());
}
else if (n==1) {
for(int i2=0; i2 < file2; i2++)
arr[i2]= Integer.parseInt(number.next());
}
else if (n==3) {
for(int i3=0; i3 < file3; i3++)
arr[i3]= Integer.parseInt(number.next());
}
else if (n==4) {
for(int i4=0; i4 < file4; i4++)
arr[i4]= Integer.parseInt(number.next());
}
else if (n==4) {
for(int i5=0; i5 < file5; i5++)
arr[i5]= Integer.parseInt(number.next());
}
else if (n==5) {
for(int i6=0; i6 < file6; i6++)
arr[i6]= Integer.parseInt(number.next());
}
else if (n==6) {
for(int i7=0; i7 < file7; i7++)
arr[i7]= Integer.parseInt(number.next());
}
beginTime = System.nanoTime();
finishTime = System.nanoTime();
totalTime = finishTime - beginTime;
times[n]=totalTime;
System.out.println("Recorded time : "+times[n]+" nanosecond to sort file "+Filename[n]);
number.close();
merge_sorting(arr, temp, 0 ,index[n]-1);
// runs insertion sort algorithm
for(int i=0; i<arr.length; i++)
{
System.out.print(arr[i]+" ");
}
System.out.println(" ");
}
System.out.println(" Sorting complete");
}
//function to sort array using merge sort
public static void merge_sorting(int arr[],int temp[],int low,int high){//lower index and higher index as argument to the function
if(low<high){//calculating if low>high then the array will be sorted else sort the array
int mid=low+(high-low)/2;//calculate mid index to break the array into two halves
merge_sorting(arr,temp,low,mid);//calling sorting function to sort the left subarray
merge_sorting(arr,temp,mid+1,high);//calling sorting function to sort the right subarray
merge(arr,temp,low,mid,high);//calling function to merge two subarrays
}
}
public static void merge(int arr[],int temp[],int low,int m, int high){//merging function
for(int i=0;i<=high;i++){//copying the value of the array in a temporary array ar1
temp[i]=arr[i];
}
int i=low,j=m+1,k=low;//initializing variable
while(i<=m && j<=high){//checking if the value of i is less than mid index and value of j is less than higher index
if(temp[i]<=temp[j]){//swap the values
arr[k]=temp[i];//put the sorted value in the original array
i++;//increment loop variable
}
else{
arr[k]=temp[j];//copy content of temporary variable to original array
j++;//increment loop variable
}
k++;
}
while(i<=m){//if value of i is less than the mid index then copy value of temporary array to the original array
arr[k]=temp[i];
k++;//increment loop variable
i++;
}
int file1= 0;
int file2 = 1;
int file3 = 2;
int file4 = 3;
int file5 = 4;
int file6 = 5;
int file7 = 6;
int index[] = {file1, file2, file3, file4, file5, file6, file7};
merge_sorting(arr, temp, 0 ,index[n]-1);
for(int i69=0; i69 < arr.length; i++)
{
System.out.print(arr[i]+" ");
}
System.out.println(" ");
System.out.println(" Sorting complete");
}
//function to sort array using merge sort
/*
public static void merge_sorting(int arr[],int temp[],int low,int high){//lower index and higher index as argument to the function
if(low<high){//calculating if low>high then the array will be sorted else sort the array
int mid=low+(high-low)/2;//calculate mid index to break the array into two halves
merge_sorting(arr,temp,low,mid);//calling sorting function to sort the left subarray
merge_sorting(arr,temp,mid+1,high);//calling sorting function to sort the right subarray
merge(arr,temp,low,mid,high);//calling function to merge two subarrays
}
}
*/
/*
public static void merge(int ar[],int temp[],int low,int m, int high){//merging function
for(int i=0;i<=high;i++){//copying the value of the array in a temporary array ar1
temp[i]=ar[i];
}
int i=low,j=m+1,k=low;//initializing variable
while(i<=m && j<=high){//checking if the value of i is less than mid index and value of j is less than higher index
if(temp[i]<=temp[j]){//swap the values
ar[k]=temp[i];//put the sorted value in the original array
i++;//increment loop variable
}
else{
ar[k]=temp[j];//copy content of temporary variable to original array
j++;//increment loop variable
}
k++;
}
while(i<=m){//if value of i is less than the mid index then copy value of temporary array to the original array
ar[k]=temp[i];
k++;//increment loop variable
i++;
}
}
*/
public static void insertionSort(int array[]) {
int n = array.length;
// declare array length
for (int j = 1; j < n; j++) {
// intialize the loop to sort
int key = array[j];
int i = j-1;
while ( (i > -1) && ( array [i] > key ) ) {
array [i+1] = array [i];
i--;
}
array[i+1] = key;
//the first element is given the smallest value
}
}
}
Explanation / Answer
If you have any doubts, please give me comment...
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class mergeSort {
public static void main(String args[]) throws FileNotFoundException {
int arr[], temp[];
String Filename[] =
new String[] {"input_100.txt", "input_1000.txt", "input_5000.txt", "input_10000.txt", "input_50000.txt", "input_100000.txt", "input_500000.txt"};
// Save filenames in a variable
double times[] = new double[Filename.length];
int file1 = 0, file2 = 0, file3 = 0, file4 = 0, file5 = 0, file6 = 0,
file7 = 0;
Scanner number;
for (int n = 0; n < Filename.length; n++) {
int mySum = 0;
number = new Scanner(new File(Filename[n]));
while (number.hasNextInt()) {
int x = number.nextInt();
// save values from file
mySum++;
// increments the value by 1 for each new value
}
// close the scanner
if (n == 0) {
file1 = mySum;
}
else if (n == 1) {
file2 = mySum;
}
else if (n == 2) {
file3 = mySum;
}
else if (n == 3) {
file4 = mySum;
}
else if (n == 4) {
file5 = mySum;
}
else if (n == 5) {
file6 = mySum;
}
else if (n == 6) {
file7 = mySum;
}
number.close();
}
int index[] = {file1, file2, file3, file4, file5, file6, file7};
for (int n = 0; n < Filename.length; n++) {
arr = new int[index[n]];
temp = new int[index[n]];
long beginTime, finishTime, totalTime;
// declare variables to save time
System.out.println("File");
number = new Scanner(new File(Filename[n]));
if (n == 0) {
for (int i1 = 0; i1 < file1; i1++)
arr[i1] = Integer.parseInt(number.next());
}
else if (n == 1) {
for (int i2 = 0; i2 < file2; i2++)
arr[i2] = Integer.parseInt(number.next());
} else if (n == 3) {
for (int i3 = 0; i3 < file3; i3++)
arr[i3] = Integer.parseInt(number.next());
} else if (n == 4) {
for (int i4 = 0; i4 < file4; i4++)
arr[i4] = Integer.parseInt(number.next());
} else if (n == 4) {
for (int i5 = 0; i5 < file5; i5++)
arr[i5] = Integer.parseInt(number.next());
} else if (n == 5) {
for (int i6 = 0; i6 < file6; i6++)
arr[i6] = Integer.parseInt(number.next());
} else if (n == 6) {
for (int i7 = 0; i7 < file7; i7++)
arr[i7] = Integer.parseInt(number.next());
}
beginTime = System.nanoTime();
finishTime = System.nanoTime();
totalTime = finishTime - beginTime;
times[n] = totalTime;
System.out.println("Recorded time : " + times[n] +
" nanosecond to sort file " + Filename[n]);
number.close();
merge_sorting(arr, temp, 0, index[n] - 1);
// runs insertion sort algorithm
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + " ");
}
System.out.println(" ");
}
System.out.println(" Sorting complete");
}
// function to sort array using merge sort
public static void merge_sorting(
int arr[], int temp[], int low,
int high) { // lower index and higher index as argument to the function
if (low < high) { // calculating if low>high then the array will be sorted
// else sort the array
int mid = low + (high - low) / 2; // calculate mid index to break the array into two halves
merge_sorting(arr, temp, low, mid); // calling sorting function to sort the left subarray
merge_sorting(arr, temp, mid + 1,high); // calling sorting function to sort the right subarray
merge(arr, temp, low, mid,high); // calling function to merge two subarrays
}
}
public static void merge(int arr[], int temp[], int low, int m,
int high) { // merging function
for (int i = 0; i <= high; i++) { // copying the value of the array in a temporary array ar1
temp[i] = arr[i];
}
int i = low, j = m + 1, k = low; // initializing variable
while (i <= m && j <= high) { // checking if the value of i is less than mid index and value of j is less than higher index
if (temp[i] <= temp[j]) { // swap the values
arr[k] = temp[i]; // put the sorted value in the original array
i++; // increment loop variable
}
else {
arr[k] = temp[j]; // copy content of temporary variable to original
// array
j++; // increment loop variable
}
k++;
}
while (i <= m) { // if value of i is less than the mid index then copy value of temporary array to the original array
arr[k] = temp[i];
k++; // increment loop variable
i++;
}
}
public static void insertionSort(int array[]) {
int n = array.length;
// declare array length
for (int j = 1; j < n; j++) {
// intialize the loop to sort
int key = array[j];
int i = j - 1;
while ((i > -1) && (array[i] > key)) {
array[i + 1] = array[i];
i--;
}
array[i + 1] = key;
// the first element is given the smallest value
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.