Question

In: Computer Science

JAVA - Quick Sort .txt and Return ----------------------------------------------------------------------------- The program should input a .txt file like...

JAVA - Quick Sort .txt and Return

-----------------------------------------------------------------------------

The program should input a .txt file like below and must use a Quick sort Algorithm

================

6 10

4 19 10 12 8 6

0 1 2

3

================

The first number "6" represents the index of the element to return after sort

the second number on the top "10" represents the number of elements or size of array.

The following numbers and lines are the elements that need to go into the array and be sorted.

-----------------------------------------------------------------------------

It should then:

1) Sort using the natural variant of quick sort algorithm and return the "6"th element

the "6" comes from the text file and should work with whatever number the text file has as long as its less than or equal to the second number, in this case "10".

Example Output given above .txt file:

Quicksort finds 8.

Solutions

Expert Solution

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class QuickSortAlgo {
  
private static final String FILENAME = "a.txt";
  
public static void main(String[] args)
{
Scanner fileReader;
int nIndex = 0;
int n = 0;
int[] arr = null;
  
try
{
fileReader = new Scanner(new File(FILENAME));
nIndex = fileReader.nextInt();
n = fileReader.nextInt();
if(nIndex > n)
{
System.out.println("We cannot get element at index " + nIndex + ", where number of elements is " + n);
System.exit(0);
}
arr = new int[n];
int count = 0;
  
while(fileReader.hasNext())
{
arr[count++] = fileReader.nextInt();
}
fileReader.close();
}catch(FileNotFoundException fnfe){
System.out.println(FILENAME + " could not be found!");
System.exit(0);
}
  
System.out.print("Original array: [ ");
for(int i = 0; i < n; i++)
{
System.out.print(arr[i] + " ");
}
System.out.println(" ]");
  
// sort the array
implementQuickSort(arr, 0, n - 1);
  
System.out.print("Sorted array: [ ");
for(int i = 0; i < n; i++)
{
System.out.print(arr[i] + " ");
}
System.out.println(" ]");
  
System.out.println("Quicksort finds " + arr[nIndex]);
}
  
private static int determinePartitionIndex(int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low - 1);
for(int j = low; j < high; j++)
{
if(arr[j] < pivot)
{
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
  
return (i + 1);
}
  
private static void implementQuickSort(int arr[], int low, int high)
{
if(low < high)
{
int partitionIndex = determinePartitionIndex(arr, low, high);
implementQuickSort(arr, low, partitionIndex - 1);
implementQuickSort(arr, partitionIndex + 1, high);
}
}
}

******************************************************************** SCREENSHOT *******************************************************


Related Solutions

[In Python] Write a program that takes a .txt file as input. This .txt file contains...
[In Python] Write a program that takes a .txt file as input. This .txt file contains 10,000 points (i.e 10,000 lines) with three co-ordinates (x,y,z) each. From this input, use relevant libraries and compute the convex hull. Now, using all the points of the newly constructed convex hull, find the 50 points that are furthest away from each other, hence giving us an evenly distributed set of points.
Write a program in Java to sort the given array using merge sort, quick sort, insertion...
Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.
I have this program, it sorts a file using shell sort and quick sort then prints...
I have this program, it sorts a file using shell sort and quick sort then prints amount of comparisons and swaps. I need to add the insertion algorithm. Here is the code. The language is Java. import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class Sort {    public static int numOfComps = 0,numOfSwaps = 0;     public static void main(String[] args)    {         try{        Scanner scanner = new Scanner(new File("a.txt"));//your text file here          ...
I have this program, it sorts a file using shell sort and quick sort then prints...
I have this program, it sorts a file using shell sort and quick sort then prints amount of comparisons and swaps. I need to add the bubble sort algorithm. Here is the code. The language is Java. import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class Sort {    public static int numOfComps = 0,numOfSwaps = 0;     public static void main(String[] args)    {         try{        Scanner scanner = new Scanner(new File("a.txt"));//your text file here       ...
1.   Bubble Sort Implement a bubble sort program that will read from a file “pp2.txt” from...
1.   Bubble Sort Implement a bubble sort program that will read from a file “pp2.txt” from the current directory a list of intergers (10 numbers to be exact), and the sort them, and print them to the screen. You can use redirection to read data from a given file through standard input, as opposed to reading the data from the file with the read API (similar to Lab #1). You can assume the input data will only have 10 numbers...
Create a c++ program with this requirements: Create an input file using notepad ( .txt )...
Create a c++ program with this requirements: Create an input file using notepad ( .txt ) . When testing your program using different input files, you must change the filename inside your program otherwise there will be syntax errors. There are a finite number of lines to be read from the data file. But we can’t assume to know how many before the program executes; so, the standard tactic is to keep reading until you find the “End of File”...
Create a c++ program that: Create an input file using notepad ( .txt ). When testing...
Create a c++ program that: Create an input file using notepad ( .txt ). When testing your program using different input files, you must change the filename inside your program otherwise there will be syntax errors. There are a finite number of lines to be read from the data file. But we can’t assume to know how many before the program executes; so, the standard tactic is to keep reading until you find the “End of File” marker. Input date...
Write a python program: There is a file called file 2. File2 is a txt file...
Write a python program: There is a file called file 2. File2 is a txt file and I have written the contents of file 2 below in the exact format it was in notepad. # This comment does not make sense # It is just to make it harder # The job description starts after this comment, notice that it has 4 lines. # This job description has 700150 hay system points\\ the incumbent will administer the spending of kindergarden...
Implement Heap Sort and Quick Sort in different programs with the following specifications: 1. The input...
Implement Heap Sort and Quick Sort in different programs with the following specifications: 1. The input to the programs should be ASCII characters 2. Your program should be able to handle upper and lower case letters both 3. The sort should be done in a descending manner 4.Note: Please use array-based representation for these sorting algorithms Please write comment n explain each step clearly as well your program should show what you are taking as input array and what your...
Write a MIPS program that uses an implementation of quick sort to sort an array of...
Write a MIPS program that uses an implementation of quick sort to sort an array of numbers (Translate the following code into (Mars Assembly language). Quicksort Implementation - C int partition(int arr [] , int left , int right) { int i=left, j=right; int tmp; int pivot = arr[(left + right) / 2]; while (i <= j) { while (arr [ i ] < pivot) i ++; while (arr [ j ] > pivot) j −−; if (i <= j)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT