Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

this is my java code : import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.io.BufferedReader; import java.io.FileReader; public class Main { static void readFile(String fileName, int

this is my java code :

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

import java.io.BufferedReader;

import java.io.FileReader;

public class Main {

static void readFile(String fileName, int array[]) {

int c = 0;

try {

File reader = new File(fileName);

Scanner sc = new Scanner(reader);

while(sc.hasNextDouble()) {

array[c] = sc.nextInt();

c++;

}

sc.close();

}

catch(FileNotFoundException e) {

System.out.println("File Not found");

e.printStackTrace();

}

}

public static void main(String[] args) {

int array[] = new int[20];

for(int c = 8; c <= 12; c++) {

// Insertion sorting

readFile("number" + c + ".txt", array);

System.out.println(" Before Insertion sorting file " + "number" + c + ".txt" + " contents: ");

for(int x = 0; x < array.length; x++)

System.out.print(array[x]+" ");

System.out.println();

insertionSort(array);

System.out.println(" After Insertion sorting file " + "number" + c + ".txt" + " contents: ");

for(int x = 0; x < array.length; x++)

System.out.print(array[x]+" ");

System.out.println();

// Merge sorting

readFile("number" + c + ".txt", array);

System.out.println(" Before Merge sorting file " + "number" + c + ".txt" + " contents: ");

for(int x = 0; x < array.length; x++)

System.out.print(array[x]+" ");

System.out.println();

mainSort(array, 0, array.length - 1);

System.out.println(" After Merge sorting file " + "number" + c + ".txt" + " contents: ");

for(int x = 0; x < array.length; x++)

System.out.print(array[x]+" ");

System.out.println();

// Heap sorting

readFile("number" + c + ".txt", array);

System.out.println(" Before Heap sorting file " + "number" + c + ".txt" + " contents: ");

for(int x = 0; x < array.length; x++)

System.out.print(array[x]+" ");

System.out.println();

heapSort(array);

System.out.println(" After Heap sorting file " + "number" + c + ".txt" + " contents: ");

for(int x = 0; x < array.length; x++)

System.out.print(array[x]+" ");

System.out.println();

// Quick sorting

readFile("number" + c + ".txt", array);

System.out.println(" Before Quick sorting file " + "number" + c + ".txt" + " contents: ");

for(int x = 0; x < array.length; x++)

System.out.print(array[x]+" ");

System.out.println();

System.out.println();

quickSort(array, 0, array.length - 1);

System.out.println(" After Quick sorting file " + "number" + c + ".txt" + " contents: ");

for(int x = 0; x < array.length; x++)

System.out.print(array[x]+" ");

System.out.println();

}

}

public static void heapSort(int array[]) {

//store the array length

int L = array.length;

//build heap(rearrange array contents)

//loops from half of the length of the array to zero index position

for (int c = L / 2 - 1 ; c >= 0; c--)

heapify(array,L,c);

// One by one extract an element from heap

// Loops from length of the array to zero index position

for (int c = L - 1; c >= 0; c--) {

// Swapping process

int temp = array[0];

array[0] = array[c];

array[c] = temp;

// Call max heapify method on the reduced heap

heapify(array, c, 0);

}

}

static void heapify(int array[], int len, int c) {

int largest = c; // Initialize largest as root

int left = 2 * c + 1; // left = 2 * c + 1

int right = 2 * c + 2; // right = 2 * c + 2

// If left child is larger than root

if (left < len && array[left] > array[largest])

largest = left;

// If right child is larger than largest so far

if (right < len && array[right] > array[largest])

largest = right;

// If largest is not root

if (largest != c) {

// Swapping process

int temp = array[c];

array[c] = array[largest];

array[largest] = temp;

// Recursively heapify the affected sub-tree

heapify(array, len, largest);

}

}

static void insertionSort(int array[]) {

// Stores the length

int L = array.length;

// Loops till length of the array

for (int c = 1; c < L; ++c) {

// Stores the current position of the array in A

int A = array[c];

int d = c - 1;

// Move elements of array[0..i-1], that are greater than key, to one position ahead of their current position

while (d >= 0 && array[d] > A) {

array[d + 1] = array[d];

d = d - 1;

}

array[d+1] = A;

}

}

public static void mergeSort(int array[], int l, int m, int r) {

// Find sizes of two sub arrays to be merged

int n1 = m - l + 1;

int n2 = r - m;

//Create two temporary left and right arrays

int Left[] = new int [n1];

int Right[] = new int [n2];

//Copy data to temporary arrays

for (int c = 0; c < n1; ++c)

Left[c] = array[l + c];

for (int d = 0; d < n2; ++d)

Right[d] = array[m + 1+ d];

//Merge the temporary arrays

// Initial indexes of first and second sub arrays

int c = 0, d = 0;

// Initial index of merged sub array

int k = l;

// Loops till end of either left or right array end

while (c < n1 && d < n2) {

//Checks if left array c index position value is less than or equals to right array d index position

if (Left[c] <= Right[d]) {

//Store the left c index position value in array k index position

array[k] = Left[c];

//Increase the counter for left array

c++;

}

else {

//Store the right d index position value in array k index position

array[k] = Right[d];

//Increase the counter for right array

d++;

}

//Increase the counter for main array

k++;

}

//Copy remaining elements of Left[] if any

while (c < n1) {

array[k] = Left[c];

// Increase c value by one

c++;

// Increase k value by one

k++;

}

//Copy remaining elements of Right[] if any

while (d < n2) {

array[k] = Right[d];

// Increase d value by one

d++;

// Increase k value by one

k++;

}

}

public static void mainSort(int array[], int l, int n) {

if (l < n) {

// Find the middle point

int mid = (l + n)/2;

// Sort first and second halves

mainSort(array, l, mid);

mainSort(array, mid + 1, n);

// Merge the sorted halves

mergeSort(array, l, mid, n);

}

}

public static int partition(int array[], int low, int high) {

int pivot = array[high];

// index of smaller element

int i = (low-1);

//Loops till low is less than high

for (int c = low; c < high; c++) {

// If current element is smaller than or equal to pivot

if (array[c] <= pivot) {

i++;

// Swap array[i] and array[c]

int temp = array[i];

array[i] = array[c];

array[c] = temp;

}

}

// Swap array[i+1] and array[high] (or pivot)

int temp = array[i+1];

array[i+1] = array[high];

array[high] = temp;

return i+1;

}

public static void quickSort(int array[], int low, int high) {

if (low < high) {

// part is partitioning index, array[part] is now at right place

int part = partition(array, low, high);

// Recursively sort elements before partition and after partition

quickSort(array, low, part-1);

quickSort(array, part+1, high);

}

}

}

and this is my output:

java.io.FileNotFoundException: number8.txt (The system cannot find the file specified)

File Not found

at java.io.FileInputStream.open0(Native Method)

at java.io.FileInputStream.open(Unknown Source)

at java.io.FileInputStream.(Unknown Source)

at java.util.Scanner.(Unknown Source)

at Main.readFile(Main.java:13)

at Main.main(Main.java:31)

java.io.FileNotFoundException: number8.txt (The system cannot find the file specified)

at java.io.FileInputStream.open0(Native Method)

at java.io.FileInputStream.open(Unknown Source)

at java.io.FileInputStream.(Unknown Source)

at java.util.Scanner.(Unknown Source)

at Main.readFile(Main.java:13)

at Main.main(Main.java:44)

Before Insertion sorting file number8.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

After Insertion sorting file number8.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

File Not found

Before Merge sorting file number8.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

After Merge sorting file number8.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

File Not found

java.io.FileNotFoundException: number8.txt (The system cannot find the file specified)

at java.io.FileInputStream.open0(Native Method)

at java.io.FileInputStream.open(Unknown Source)

at java.io.FileInputStream.(Unknown Source)

at java.util.Scanner.(Unknown Source)

at Main.readFile(Main.java:13)

at Main.main(Main.java:57)

Before Heap sorting file number8.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

After Heap sorting file number8.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

java.io.FileNotFoundException: number8.txt (The system cannot find the file specified)

File Not found

at java.io.FileInputStream.open0(Native Method)

at java.io.FileInputStream.open(Unknown Source)

at java.io.FileInputStream.(Unknown Source)

at java.util.Scanner.(Unknown Source)

at Main.readFile(Main.java:13)

at Main.main(Main.java:70)

Before Quick sorting file number8.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

After Quick sorting file number8.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

File Not found

java.io.FileNotFoundException: number9.txt (The system cannot find the file specified)

at java.io.FileInputStream.open0(Native Method)

at java.io.FileInputStream.open(Unknown Source)

at java.io.FileInputStream.(Unknown Source)

at java.util.Scanner.(Unknown Source)

at Main.readFile(Main.java:13)

at Main.main(Main.java:31)

Before Insertion sorting file number9.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

After Insertion sorting file number9.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

File Not found

java.io.FileNotFoundException: number9.txt (The system cannot find the file specified)

at java.io.FileInputStream.open0(Native Method)

at java.io.FileInputStream.open(Unknown Source)

at java.io.FileInputStream.(Unknown Source)

at java.util.Scanner.(Unknown Source)

at Main.readFile(Main.java:13)

at Main.main(Main.java:44)

Before Merge sorting file number9.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

After Merge sorting file number9.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

java.io.FileNotFoundException: number9.txt (The system cannot find the file specified)

File Not found

at java.io.FileInputStream.open0(Native Method)

at java.io.FileInputStream.open(Unknown Source)

at java.io.FileInputStream.(Unknown Source)

at java.util.Scanner.(Unknown Source)

at Main.readFile(Main.java:13)

at Main.main(Main.java:57)

Before Heap sorting file number9.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

After Heap sorting file number9.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

File Not found

java.io.FileNotFoundException: number9.txt (The system cannot find the file specified)

at java.io.FileInputStream.open0(Native Method)

at java.io.FileInputStream.open(Unknown Source)

at java.io.FileInputStream.(Unknown Source)

at java.util.Scanner.(Unknown Source)

at Main.readFile(Main.java:13)

at Main.main(Main.java:70)

Before Quick sorting file number9.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

After Quick sorting file number9.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

File Not found

java.io.FileNotFoundException: number10.txt (The system cannot find the file specified)

at java.io.FileInputStream.open0(Native Method)

at java.io.FileInputStream.open(Unknown Source)

at java.io.FileInputStream.(Unknown Source)

at java.util.Scanner.(Unknown Source)

at Main.readFile(Main.java:13)

at Main.main(Main.java:31)

Before Insertion sorting file number10.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

After Insertion sorting file number10.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

File Not found

java.io.FileNotFoundException: number10.txt (The system cannot find the file specified)

at java.io.FileInputStream.open0(Native Method)

at java.io.FileInputStream.open(Unknown Source)

at java.io.FileInputStream.(Unknown Source)

at java.util.Scanner.(Unknown Source)

at Main.readFile(Main.java:13)

at Main.main(Main.java:44)

Before Merge sorting file number10.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

After Merge sorting file number10.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

File Not found

java.io.FileNotFoundException: number10.txt (The system cannot find the file specified)

at java.io.FileInputStream.open0(Native Method)

at java.io.FileInputStream.open(Unknown Source)

at java.io.FileInputStream.(Unknown Source)

at java.util.Scanner.(Unknown Source)

at Main.readFile(Main.java:13)

Before Heap sorting file number10.txt contents: at Main.main(Main.java:57)

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

After Heap sorting file number10.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

File Not found

java.io.FileNotFoundException: number10.txt (The system cannot find the file specified)

at java.io.FileInputStream.open0(Native Method)

at java.io.FileInputStream.open(Unknown Source)

at java.io.FileInputStream.(Unknown Source)

at java.util.Scanner.(Unknown Source)

at Main.readFile(Main.java:13)

Before Quick sorting file number10.txt contents: at Main.main(Main.java:70)

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

After Quick sorting file number10.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

File Not found

java.io.FileNotFoundException: number11.txt (The system cannot find the file specified)

at java.io.FileInputStream.open0(Native Method)

at java.io.FileInputStream.open(Unknown Source)

at java.io.FileInputStream.(Unknown Source)

at java.util.Scanner.(Unknown Source)

at Main.readFile(Main.java:13)

at Main.main(Main.java:31)

Before Insertion sorting file number11.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

After Insertion sorting file number11.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

File Not found

java.io.FileNotFoundException: number11.txt (The system cannot find the file specified)

at java.io.FileInputStream.open0(Native Method)

at java.io.FileInputStream.open(Unknown Source)

at java.io.FileInputStream.(Unknown Source)

at java.util.Scanner.(Unknown Source)

at Main.readFile(Main.java:13)

at Main.main(Main.java:44)

Before Merge sorting file number11.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

After Merge sorting file number11.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

File Not found

java.io.FileNotFoundException: number11.txt (The system cannot find the file specified)

at java.io.FileInputStream.open0(Native Method)

at java.io.FileInputStream.open(Unknown Source)

at java.io.FileInputStream.(Unknown Source)

at java.util.Scanner.(Unknown Source)

at Main.readFile(Main.java:13)

at Main.main(Main.java:57)

Before Heap sorting file number11.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

After Heap sorting file number11.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

File Not found

java.io.FileNotFoundException: number11.txt (The system cannot find the file specified)

at java.io.FileInputStream.open0(Native Method)

at java.io.FileInputStream.open(Unknown Source)

at java.io.FileInputStream.(Unknown Source)

at java.util.Scanner.(Unknown Source)

at Main.readFile(Main.java:13)

at Main.main(Main.java:70)

Before Quick sorting file number11.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

After Quick sorting file number11.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

File Not found

java.io.FileNotFoundException: number12.txt (The system cannot find the file specified)

at java.io.FileInputStream.open0(Native Method)

at java.io.FileInputStream.open(Unknown Source)

at java.io.FileInputStream.(Unknown Source)

at java.util.Scanner.(Unknown Source)

at Main.readFile(Main.java:13)

at Main.main(Main.java:31)

Before Insertion sorting file number12.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

After Insertion sorting file number12.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

File Not found

java.io.FileNotFoundException: number12.txt (The system cannot find the file specified)

at java.io.FileInputStream.open0(Native Method)

at java.io.FileInputStream.open(Unknown Source)

at java.io.FileInputStream.(Unknown Source)

at java.util.Scanner.(Unknown Source)

at Main.readFile(Main.java:13)

at Main.main(Main.java:44)

Before Merge sorting file number12.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

After Merge sorting file number12.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

File Not found

java.io.FileNotFoundException: number12.txt (The system cannot find the file specified)

at java.io.FileInputStream.open0(Native Method)

at java.io.FileInputStream.open(Unknown Source)

at java.io.FileInputStream.(Unknown Source)

at java.util.Scanner.(Unknown Source)

at Main.readFile(Main.java:13)

at Main.main(Main.java:57)

Before Heap sorting file number12.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

After Heap sorting file number12.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

File Not found

java.io.FileNotFoundException: number12.txt (The system cannot find the file specified)

at java.io.FileInputStream.open0(Native Method)

at java.io.FileInputStream.open(Unknown Source)

at java.io.FileInputStream.(Unknown Source)

at java.util.Scanner.(Unknown Source)

at Main.readFile(Main.java:13)

at Main.main(Main.java:70)

Before Quick sorting file number12.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

After Quick sorting file number12.txt contents:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

please help me with this file reader. what should i do to solve this ? i link all the text file under the same file project folder.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions