Computer Science - Java
Explain the code step by step (in detailed order). Also explain what the program required. Thanks
![7 import java.io.File; 8 import java.util.Scanner; 9 import java.util.Arrays; 10 import java.io.FileNotFoundException; 12 public class insertionSort 13 14 public static void insertionSort (double array]) 15 int n -array.length; for (int j-1; j < n; j++) 17 18 19 20 21 double key - array[j]; int i-_1 while ( (i > -1) && ( array [i] > key array [i+1] - array [i]; 23 2 4 25 26 27 public static void main (String[ ] args) throws FileNotFoundException( 28 Scanner in - new Scanner(System.in); 29 String Filename []= new String[] {input_100.txt, input-1000 (1).txt , input-5000.txt ,in 30 double time s []=new double[Filename.length]; 31 for(int n=0; n<Filename length;n++) 32 L 33 Scanner num - new Scanner (new File(Filename[n])); 34 long startTime, endTime,totalTime; 35 int sum - 0 3 6 37 while (num.hasNextDouble()) 38 double x-num.nextDouble(); 39 sum++ 40 41 num.close(); 42 double [ arr new double[sum]; 43 num - new Scanner (new File (Filename[n])); arrayli+1] -key;](http://img.homeworklib.com/questions/d49470f0-5c3c-11ec-8430-d391d5f01a8c.png?x-oss-process=image/resize,w_560)

I am putting comments under everyline to explain what the line/ command means.
CODE :
import java.io.File;
// inluding the File functions
import java.util.Scanner;
// including the Scanner definition
import java.util.Arrays;
// including the array definition
import java.io.FileNotFoundException;
// including the functions for error handling
public class InsertionSort
{
public static void insertionSort(double
array[])
{
int n =
array.length;
// declaring the length
of the array given
for(int j = 1; j < n;
j++)
// initialising the for
loop for the sorting to begin
{
double key = array[j];
// assigning the value to a variable
int i = j-1;
// declaring a variable that will take the index of the previous
element
while( (i > -1) && ( array[i] > key) )
// declaring a while loop for sorting the array for each
index
{
array[i+1] = array[i];
// if the previous element if greater than the next one.. swap
them
i--;
// decrease the index by one and repeat
}
array[i+1] = key;
// the first element is given the smallest value
}
}
public static void main(String[] args)
{
Scanner in = new
Scanner(System.in);
// defining a new
scanner for scanning text
String Filename[] = new
String[]{"file1.txt"};
// saving already known
filenames in a variable;
double times[] = new
double [Filename.length];
// declaring a variable
to save the length of the Filename array
for(int n = 0;
n<Filename.length; n++)
// initialing the loop
to be used for different inputs
{
Scanner num = new Scanner(new File(Filename[n]));
// making the program read from a file
long startTime, endTime, totalTime;
// declaring variable that will save the time of the code run
int sum = 0;
// stores the number of variables in a file
while(num.hasNextDouble())
// this loop will run till the input exists in the file
{
double x = num.nextDouble();
// saves the value from the file in a variable
sum ++;
// incrementing the value by 1 for each new value in the file
}
num.close();
// closing the scanner
double [] arr = new double[sum];
// initialising an array with "sum" elements
num = new Scanner(new File(Filename[n]));
// making it read from the file
for(int i = 0; i<sum; ++i)
// initialing the loop to read the values from the file to the
array
arr[i] = num.nextDouble();
// inputting the values from the file to the array
num.close();
// closing the Scanner
startTime = System.nanoTime();
// saving the time of the system at this instant
insertionSort(arr);
// runs the sorting algorithm
endTime = System.nanoTime();
// saving the time of the system at this instant
totalTime = endTime - startTime;
// finding the exact time taken to run the algorithm
times[n] = totalTime;
// saving the values of the runTime of the insertionSort for its
correspoding input
System.out.println("Time take : " + times[n] + "nanosecond to fort
file " + Filename[n]);
// printing out the time
}
}
}
Explanation of the complete code :
The insertion sort starts from the
first index, and keeps pushing the smallest element to its correct
place.
It reads in a number ( jth element ) from the
array and goes to the starting of the array from its place one
element at a time till it finds its correct place(
ith element ) and then it puts the value
there.
The main function takes in a bunch of filenames with supposedly different inputs/ different number of inputs. It then reads each file, sorts all those elements from by insertionSort and saves the time taken to sort the elements of all the files then shows them to the user. So, we can compare and examine the time taken by insertionSort on different number of inputs.
*************NOTE*************
If there is any doubt or you need more explanation, Please reply
in the comments.
If everything is good rate accordingly :)
Computer Science - Java Explain the code step by step (in detailed order). Also explain what...
Language Java Step 1: Design a class called Student. The Student class should contain the following data: firstName lastName studentID totalCredits gpa Your class should implement the “sets” and “gets” for the class. Include methods: equals, compareTo, toString. Change the toString method (from class) to put each member variable on a new line. Step 2: Write a driver program to test that Student works correctly. Test is with 3 students as given in class. Step 3: Write a “registration” program...
Hello this is my java sorting algorithm program i need all of my errors corrected so I can run it. thank you!! import java.util.Scanner; public class SortingAlogs { public static void main(String[]args){ int array [] = {9,11,15,34,1}; Scanner KB = new Scanner(System.in); int ch; while (true) { System.out.println("1 Bubble sort\n2 Insertion sort\n3 Selection sort\n"); ch = KB.nextInt(); if (ch==1) bubbleSort(array); if (ch==2) insertion(array); if (ch==3) Selection(array); if (ch==4) break; print(array); System.out.println(); } }...
Java : Please help me correct my code: create a single class (Program11.java) with a main method and some auxiliary methods to input a 2-D array from a disk file, input some “transactions” to change the 2-D array and output the changed 2-D array to another file. Your main method will be minimal (see below). Most of the work will go on in your methods. In main, declare (but do not instantiate) 2-D array. Then call the three methods. The...
Help check why the exception exist do some change but be sure to use the printwriter and scanner and make the code more readability Input.txt format like this: Joe sam, thd, 9, 4, 20 import java.io.File; import java.io.PrintWriter; import java.io.IOException; import java.io.FileNotFoundException; import java.io.FileWriter; import java.util.Scanner; public class Main1 { private static final Scanner scan = new Scanner(System.in); private static String[] player = new String[622]; private static String DATA = " "; private static int COUNTS = 0; public static...
Problem: Use the code I have provided to start writing a program that accepts a large file as input (given to you) and takes all of these numbers and enters them into an array. Once all of the numbers are in your array your job is to sort them. You must use either the insertion or selection sort to accomplish this. Input: Each line of input will be one item to be added to your array. Output: Your output will...
In the code shown above are
two parts of two different exercises. HOW WE CAN HAVE BOTH OF THEM
ON THE SAME CLASS BUT SO WE CAN RECALL them threw different
methods. Thank you.
1-First exercise
import java.util.Scanner;
public class first {
public static int average(int[] array){
int total = 0;
for(int x: array)
total += x;
return total / array.length;
}
public static double average(double[] array){
double total = 0;
for(double x: array)
total += x;
return total /...
Need to code the HeapSort Algorithm in java. Most of the code is already done just need to code the "heapify" part of the heapsort algorithm. My heapify code is in bold below called "sink", but when I run it it gives me 2 errors with the string compare part saying: WordCountHeap.java:61: error: cannot find symbol if(l < k && String.Compare(pq[l], pq[n]) < 0) ^ symbol: method Compare(String,String) location: class String Please fix the code and the "heapify"...
Get doubles from input file. Output the biggest. Answer the comments throughout the code. import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Lab8Num1 { public static void main(String[] args) { //Declaring variable to be used for storing and for output double biggest,temp; //Creating file to read numbers File inFile = new File("lab8.txt"); //Stream to read data from file Scanner fileInput = null; try { fileInput = new Scanner(inFile); } catch (FileNotFoundException ex) { //Logger.getLogger(Lab10.class.getName()).log(Level.SEVERE, null, ex); } //get first number...
how would i use test.add because i would like to use add import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class checkFruit { private List<String> tests; public List<String> getFruit(String fruits) { tests = new ArrayList<String>(Arrays.asList(fruits.split(","))); for (String test : tests) { System.out.println(test); tests.add()//how would i use add here } return tests; } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter file name: "); File file = new File(in.nextLine()); try { checkFruit...
I'm attempting to convert ch object array to double array public void calculateSalesPrice() { double salePrice[] = new double[ch.length]; int p = 0; for (int i = 0; i < ch.length; i += 3) { p = i + 1; for(int j = 0;j } } error message:items.java:16: error: cannot find symbol for(int j = 0;j ^ symbol: variable length location: class Object items.java:17: error: array required, but Object found salePrice[j] = (double full program import java.io.File; import java.util.Scanner; import...