View the step-by-step solution to:

/* HeapSort. To interactively provide test inputs, run the program with java HeapSort To conveniently test the algorithm with a large input, create a...

Have no ideal about heapsort...could you help me..thanks

/* CSC 225 - Fall 2012 Assignment 2 - Template for HeapSort This template includes some testing code to help verify the implementation. To interactively provide test inputs, run the program with java HeapSort To conveniently test the algorithm with a large input, create a text file containing space-separated integer values and run the program with java HeapSort file.txt where file.txt is replaced by the name of the text file. For marking purposes, your code must compile and run using the java compiler and runtime environment installed on the lab machines in ECS 242 or ECS 258. B. Bird - 09/24/2012 */ import java.util.Scanner; import java.util.Arrays; import; public class HeapSort{ public static final int max_values = 1000000; /* heapSort(inputArray) Sort the array inputArray using using an array-based heap sort algorithm. You may add additional functions if needed, but the entire program must be contained in this file. No dynamic memory allocation is permitted or necessary. Do not use a linked data structure of any sort. Do not change the function signature (name/parameters). */ public static void heapSort(int[] inputArray){ /* Create an array A which will contain the heap */ /* A has size n+1 to allow 1-based indexing */ int n = inputArray.length; int[] A = new int[n+1]; /* Copy the array inputArray into A, with inputArray[i] being stored in A[i+1] */ /* . .. Your code here . .. */ /* Transform A into a max-heap using a 'bottom-up' algorithm (which must run in O(n) time). */ /* One such algorithm is given in Part 2 of the lab notes. The recursive version is algorithm 8 and the iterative version is algorithm 9 */ /* . .. Your code here . ..*/ /* Perform a sequence of n 'remove-maximum' operations, storing the removed element at each step in successively smaller indices of A (e.g. the first removed element is stored in A[n], the second in A[n-1], etc.). Remember to use a 'bubble-down' operation to restore the heap property after each removal. Pseudocode
Background image of page 1
for bubble down is given as part of algorithms 8 and 9 in Part 2 of the lab notes. */ /* . .. Your code here . .. */ /* Copy the sorted values in A back into inputArray, with inputArray[i] getting the value from A[i+1] */ /* . .. Your code here . .. */ } public static void main(String[] args){ /* Code to test your implementation */ /* You may modify this, but the contents of this method will be discarded before marking (so do not put any of the heap sort implementation here) */ Scanner s; if (args.length > 0){ try{ s = new Scanner(new File(args[0])); } catch( e){ System.out.printf("Unable to open %s\n",args[0]); return; } System.out.printf("Reading input values from %s.\n",args[0]); }else{ s = new Scanner(; System.out.printf("Enter up to %d positive integers to be sorted. Enter a negative value to end the list.\n",max_values); } int[] array = new int[max_values]; int n = 0; int v; while(s.hasNextInt() && (v = s.nextInt()) >= 0) array[n++] = v; array = Arrays.copyOfRange(array,0,n); System.out.printf("Read %d values, sorting. ..\n",array.length); heapSort(array); //Don't print out the values if there are more than 100 of them if (array.length <= 100){ System.out.println("Sorted values:"); for (int i = 0; i < array.length; i++) System.out.printf("%d ",array[i]); System.out.println(); } //Check whether the sort was successful boolean isSorted = true; for (int i = 0; i < n-1; i++) if (array[i] > array[i+1]) isSorted = false; System.out.printf("Array %s sorted.\n",isSorted? "is":"is not"); } }
Background image of page 2
Sign up to view the entire interaction

Top Answer

Dear Student Please find... View the full answer

Computer - Preview not available

Sign up to view the full answer

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.


Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Ask a homework question - tutors are online