Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I just need some help debugging this program. What do these error messages mean? import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException;

Hello, I just need some help debugging this program. What do these error messages mean?

import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Arrays; import java.util.Stack;

/** * * @author */ public class QuickSort1Java {

private static int[] input;

/** * @param args the command line arguments * @throws java.io.FileNotFoundException */ public static void main(String[] args) throws FileNotFoundException, IOException{ // read from text file Unsorted. Variable will look like '4 6 8 12' with //one space between them and will be stored in unordered fashion. int n=0; int [] unsorted = new int[n]; System.out.println("This Program will sort a file called 'unsorted.txt'. "); //System.out.println("Sorted array: " + Arrays.toString(unsorted)); //FileNotFoundParameters File input_file = new File("unsorted.txt"); if (input_file.exists()) { System.out.println("Yes, input file exists"); int total = 0; // will read from unsorted file BufferedReader b; b = new BufferedReader(new FileReader("unsorted.txt")); String line = ""; //will remove any invisible characters. while((line=b.readLine()) != null) { line = line.trim(); unsorted =Arrays.copyOf(unsorted, unsorted.length+1); unsorted[unsorted.length-1]=Integer.parseInt(line); total++; } System.out.println("unsorted array: " + Arrays.toString(unsorted)); // calculate time for Iterative quick sort to run. This will be outputted //to the console. long start = System.nanoTime(); iterativeQsort(unsorted); System.out.println("start:" + start); long end = System.nanoTime(); System.out.println("Total Time: " + (end - start) + "nano seconds. "); } else { System.out.println("No input file found. "); } } //iteratively sorts data in QSort fashion. public static void iterativeQsort(int[] numbers) { Stack stack = new Stack(); stack.push(0); stack.push(numbers.length); while(!stack.isEmpty()) { int end = (int) stack.pop(); int start = (int) stack.pop(); if(end-start <2) { continue; } int p = start + ((end - start)/2); p = partition(numbers, p, start, end); stack.push(p+1); stack.push(end); stack.push(p); } } //will create a smaller array and rearrange data via quicksort method. private static int partition (int[] input, int position, int start, int end ) { int l = start; int h = end-2; int piv = input[position]; swap(input, position, end-1); while(l= piv) { h--; } else { swap(input, l, h); } } int idx = h; if (input[h] < piv) { idx++; } swap(input, end-1, idx); return idx; //swap two numbers in array } private static void swap (int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Next Generation Databases NoSQLand Big Data

Authors: Guy Harrison

1st Edition

1484213300, 978-1484213308

More Books

Students also viewed these Databases questions

Question

Task 3 - Assembly programming

Answered: 1 week ago