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.
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]+" ");
}
}
JAVA - Natural Quick Sort .txt and Return ----------------------------------------------------------------------------- The program should input a .txt file...
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 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...
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();...
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 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...
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...
In JAVA Create a program with an array with the following data: 50 12 31 76 5 23 15 Use a text file (.txt created in notepad) to read the data into the array. Write a sort method (your choice of sorting algorithm) to sort the array values values - highest to lowest. Write a method to determine the minimum value. Your program should call the sort and minimum methods. Output the sorted array and minimum value.
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...
Create a JAVA program with an array with the following data: 50 12 31 76 5 23 15 Use a text file (.txt created in notepad) to read the data into the array. Write a sort method (your choice of sorting algorithm) to sort the array values values - FROM HIGHEST TO LOWEST. Write a method to determine the minimum value. Your program should call the sort and minimum methods. Output the sorted array and minimum value.
Java Program Create a class to store an array of with enough space to store 10 integer values. Using the principle of recursion, implement the following: *getSize : returns the size of the array. *get (i): returns the i-th element of the array. If the element does not exist, it throws a "NoSuchElementExceptionâ which is a subclass of Java class RunTimeException. *add (val): inserts value as the last element of the array. If necessary, double the size of the current...