Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is a java question Sorting Create 3 text files, one with the 50000 integers

ID: 3887964 • Letter: T

Question

This is a java question

Sorting Create 3 text files, one with the 50000 integers 1 through 50000 in order, one with the 50000 integers 1 through 50000 in reverse order, and one with 50000 random integers (between 1 and 50000). Write a program to

        Read the name of a file

        Store the contents of the file into a 50000 element array,

        Sort the array using an Insertion Sort, and

        Print the actual running time (without any server overhead) of the sort.

        Print the number of comparisons used in the sort.

        Print just the first 30 values in the resulting array

Turn in a printed copy of the program, along with a printout of the output from three separate runs (using the three text files).  

Explanation / Answer

Here is the code for you:

import java.io.*;
import java.util.*;
class InsertionSortTimes
{
    public static double insertionSort(String fileName) throws FileNotFoundException
{
   int[] list = new int[50000];
       double t1, t2;
       //Store the contents of the file into a 50000 element array
       Scanner sc = new Scanner(new File(fileName));
       for(int i = 0; i < 50000; i++)
           list[i] = sc.nextInt();
       // Sort the array using an Insertion Sort
       t1 = System.currentTimeMillis();
      
       int temp;
       int comparisons = 0;
   for(int i = 1; i < list.length; i++)
   {
   temp = list[i];
   int j;
   for(j = i-1; j >= 0 && list[j] > temp; j--)
   {
   comparisons++;
   list[j+1] = list[j];
   }
   list[j+1] = temp;
   }
  
   t2 = System.currentTimeMillis();
   //Print the number of comparisons used in the sort.
   System.out.println("The number of comparisons required here is: " + comparisons);
   //Print just the first 30 values in the resulting array
   for(int i = 0; i < 30; i++)
       System.out.print(list[i] + " ");
   System.out.println();  
   return t2-t1;
    }
    public static void main(String[] args) throws FileNotFoundException
    {
      
       //Read the name of a file
       Scanner console = new Scanner(System.in);
       System.out.print("Enter the name of the sorted file: ");
       String fileName = console.next();
       //Print the actual running time (without any server overhead) of the sort.
       System.out.println( "Time to sort the readily sorted file: " + insertionSort(fileName) + " sec ");  
      
       System.out.print("Enter the name of the reverse sorted file: ");
       fileName = console.next();
       System.out.println( "Time to sort the reverse sorted file: " + insertionSort(fileName) + " sec ");  
      
       System.out.print("Enter the name of the randomly sorted file: ");
       fileName = console.next();
       System.out.println( "Time to sort the randomly sorted file: " + insertionSort(fileName) + " sec ");  
    }
}

If you need any refinements, just get back to me.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote