Question

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

JAVA - Natural 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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package sort;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.util.Scanner;

public class ReadFromFileAndSort {

static int index;

private static int[] readFromFile(String filename) throws IOException {

File file = new File(filename); //make new files

String res="";

Scanner sc = null;

String firstLine="";

int size=0;

int ar[]=new int[0];

int i=0;

try {

sc = new Scanner(file); //read file using scanner

while (sc.hasNext()) {

firstLine=sc.nextLine();

index=Integer.parseInt(firstLine.split(" ")[0]);

size=Integer.parseInt(firstLine.split(" ")[1]);

break;

}

ar=new int[size];

while(sc.hasNext()){

String temp[]=sc.nextLine().split(" ");

for(int j=0;j<temp.length;j++){

ar[i++]=Integer.parseInt(temp[j]);

}

}

} catch (FileNotFoundException e) {

System.err.println(e.getMessage());

} finally {

if (sc != null) {

sc.close();

}

}

return ar;

}

public static int part(int ar[], int l, int h)

{

int p = ar[l]; //to first element for array as pivote

int j=h; //j as highest

int i = (h+1); //after highest index

while (j>l) //run loop j greater the low

{

if (ar[j]>= p) { //if jth element greater then p

i--; //swap ith and jth element

int t = ar[i];

ar[i] = ar[j];

ar[j] = t;

}

j--; //go to lower element

}

int t = ar[i-1]; //swap pivot if it is not at right place

ar[i-1] = ar[l];

ar[l] = t;

return i-1;

}

public static void sort(int ar[], int l, int h)

{

if (l < h)

{

//using divide and concor

int pi = part(ar, l, h); //find pivote index

sort(ar, l, pi-1); //sort array before pivot

sort(ar, pi+1, h); //after pivot

}

}

public static void main(String[] args) throws IOException {

String file="/Users/snehkuma/Documents/HomeworkLib/HomeworkLib/Sort.txt";

int ar[]=readFromFile(file);

sort(ar, 0, ar.length-1);

System.out.print(ar[index]+" ");

}

}

Add a comment
Know the answer?
Add Answer to:
JAVA - Natural Quick Sort .txt and Return ----------------------------------------------------------------------------- The program should input a .txt file...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • Assume that you are sorting an array of 8 elements with quick sort. You just finished...

    Assume that you are sorting an array of 8 elements with quick sort. You just finished the first pass and the array looks like below. Which statement is true for the pivot value? 4 8 12 16 18 20 22 24 QUICKSORT ALGORITHM Quicksort selects a specific value called a pivot and rearranges the array into two parts (called partioning). If the array is randomly ordered, it does not matter which element is the pivot. For simplicity, the first element...

  • This program should test the running time of these algorithms: Selection Sort Insertion Sort Bubble Sort...

    This program should test the running time of these algorithms: Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort Heap Sort You have access to the implementation of all of these sorting algorithms, and you may use what is provided in your text directly. Be sure to cite the source of these implementations in the header of your program. Please maintain the property that these sorting algorithms sort arrays in ascending order. For this homework, you will write a...

  • Sorting algorithm: quick sort Exercise One (20 marks) Given the following program body for implementing Quick...

    Sorting algorithm: quick sort Exercise One (20 marks) Given the following program body for implementing Quick sort, complete the program by writing code where required import java.util.Random; public class QuickSort public void quickSectlinti] A) QuickSort/A, O, A.length-1); private void guickSortlin Aiat low.int high) //Complete the code for the quicksort method (5 marks] private void swaplint[] a, int indexl, int index2) //Complete the code for the swap method [3 marks] private int setPivotlint low, int high) Random rand = new Random();...

  • Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8...

    Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8 9 7 0 4 Pivot selection is defined to be the first element of each sub-list. Show the array before and after each quicksort round (when the array is partitioned after placing the pivot at its correct position). Also, clearly highlight the pivot in each partition b. Consider an unsorted array of integers of size n. Write a Java program to arrange the array...

  • Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays....

    Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays. - Write two methods that sort arrays of doubles. One method should use selection sort, and the other should use bubble sort. - In each of the sort methods, add code that counts the total number of comparisons and total number of swaps performed while sorting the entire array (be careful; don't count each pass through the array separately) - Each time an array...

  • JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers:...

    JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers: 47 71 15 35 66 61 44 26 68 56 18 19 36 84 69 55 1. Find the value of pivot 2. Show the result after partitionIt() is called first time 3. Show the value of partition 4. Show the content of the array ///////////////////////////// Lab6.java class ArrayIns { private long[] theArray; // ref to array theArray private int nElems; // number of...

  • Assume that an array A is given to you in a txt file. You should read...

    Assume that an array A is given to you in a txt file. You should read it. If you think this is a time-dependent data, print the number of local minimums in the array to a txt file. Local minimum means the element which is less than the previous element and the following element. Please do not use any additional library. For example, if A=[3, 2, 9, 8, 7, 8, 6], the code should print 2 and 7. (Local minimums...

  • Program with generic merge sort and binary search method help. The programming language I'm using is...

    Program with generic merge sort and binary search method help. The programming language I'm using is Java. This program should show understanding generic merge sort methods and generic binary search methods in java. The execution should include at least 5 found items including one from the first three items in the sorted array and one from the last three items in the sorted array as well as at least two items not found Create a generic merge sort method that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT