Java Programming Sort the text files (100_input.txt and 1000_input.txt). Each te
ID: 3746985 • Letter: J
Question
Java Programming
Sort the text files (100_input.txt and 1000_input.txt). Each text file contains random numbers.
Here's the code:
import java.io.File;
import java.util.Scanner;
import java.util.Arrays;
import java.io.FileNotFoundException;
public class mergeSort
{
// sorting the merge
public static void merge_sort(int[] arr, int [] temp, int p, int r) {
if (p < r) {
int q = (p + r)/2;
merge_sort(arr, temp, p, q);
merge_sort(arr,temp, q + 1, r);
merge(arr,temp,p,q,r);
}
}
// merge the two arrays
public static void merge(int[] arr,int[] temp, int p,int q,int r) {
// merge A[p..q] with A[q+1..r]
//int k[] array
temp = new int[r];
//temporary value for the first array
int i = p;
//temporary value for the second array
int j = q + 1;
//copy arr[p..r] to temp[p...r]
for (int k = p; k < r; k++) {
temp[k] = arr[k];
}
//merge back to A[p...r]
for (int k = p; k < r; k++) {
//left half empty copy from the right
if (i > q ) {
arr[k] = temp [j];
j = j + 1;
}
//right half empty, copy from the left
else if (j >= r ) {
arr[k] = temp[i];
i = i + 1;
}
//copy from the right
else if (temp[j] < temp[i]) {
arr[k] = temp[j];
j = j +1;
}
//copy from the left
else {
arr[k] = temp[i];
i = i + 1;
}
}
}
public static void main (String[] args) throws FileNotFoundException {
//Constructor
Scanner in = new Scanner(System.in);
//File locator (edit if file changes!)
//String Filename[] = new String[]{"input_100.txt", "input_1000.txt"};
String Filename[] = new String[]{"input_100.txt" , "input_1000.txt"};
//Save filenames in a variable
double times[] = new double [Filename.length];
for (int n = 0; n < Filename.length; n++) {
Scanner number = new Scanner(new File (Filename[n]));
long beginTime, finishTime, totalTime;
int mySum = 0;
// declare variables to save time
while (number.hasNextInt()) {
int x=number.nextInt();
// save values from file
mySum++;
//increments the value by 1 for each new value
}
number.close();
//close the scanner
int [] arr = new int[mySum];
int [] temp = new int [mySum];
//initialize an array with "mySum" elements
number = new Scanner (new File(Filename[n]));
for(int i=0; i < mySum; ++i)
arr[i]= Integer.parseInt(number.next());
number.close();
beginTime = System.nanoTime();
merge_sort(arr, temp, 0 ,mySum);
// runs insertion sort algorithm
//for(int i=0; i<arr.length; i++)
//{
// System.out.print(arr[i]+" ");
//}
finishTime = System.nanoTime();
totalTime = finishTime - beginTime;
times[n]=totalTime;
System.out.println("Recorded time : "+times[n]+" nanosecond to sort file "+Filename[n]);
}
}
}
Explanation / Answer
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[],index=0;
//String Filename[] = new String[]{"input_100.txt", "input_1000.txt"};
String Filename[] = new String[]{"C:\Users\SuNiL\Documents\NetBeansProjects\Solution\src\merge\input_100.txt" , "C:\Users\SuNiL\Documents\NetBeansProjects\Solution\src\merge\input_1000.txt"};
//Save filenames in a variable
double times[] = new double [Filename.length];
int file1=0,file2=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{
file2=mySum;
}
number.close();
}
index=file1+file2;
arr=new int[index];
temp=new int[index];
for (int n = 0; n < Filename.length; 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 i=0; i < file1; i++)
arr[i]= Integer.parseInt(number.next());
}
else{
for(int k=file1; k < (file1+file2); k++){
arr[k]= 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-1);
// runs insertion sort algorithm
for(int i=0; i<arr.length; i++)
{
System.out.print(arr[i]+" ");
}
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++;
}
}
}
Output:
run:
9
File
Recorded time : 394.0 nanosecond to sort file C:UsersSuNiLDocumentsNetBeansProjectsSolutionsrcmergeinput_100.txt
File
Recorded time : 0.0 nanosecond to sort file C:UsersSuNiLDocumentsNetBeansProjectsSolutionsrcmergeinput_1000.txt
1 2 3 4 4 5 7 8 9 Sorting complete
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.