Computer Science - Java Explain the code step by step (in detailed order). Also
ID: 3745348 • Letter: C
Question
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 -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; nExplanation / Answer
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 :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.