Question

How would the Java Program look like if I wanted to place the following values in...

How would the Java Program look like if I wanted to place the following values in the oder that they would be in after being bulk insterted into a heap for Heap sort and how would the heap look like after three deleteMin() commands? These would be some example values: 9 5 8 2 4 3 1 7 6 0. ( place values that were removed using a delete min at the start in order that they were removed/

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

Answer:

import java.io.*;

class GFG {

     

    // To heapify a subtree rooted with node i which is

    // an index in arr[]. n is size of heap

    static void heapify(int arr[], int n, int i)

    {

        int smallest = i; // Initialize smalles as root

        int l = 2 * i + 1; // left = 2*i + 1

        int r = 2 * i + 2; // right = 2*i + 2

        // If left child is smaller than root

        if (l < n && arr[l] < arr[smallest])

            smallest = l;

        // If right child is smaller than smallest so far

        if (r < n && arr[r] < arr[smallest])

            smallest = r;

        // If smallest is not root

        if (smallest != i) {

            int temp = arr[i];

            arr[i] = arr[smallest];

            arr[smallest] = temp;

            // Recursively heapify the affected sub-tree

            heapify(arr, n, smallest);

        }

    }

    // main function to do heap sort

    static void heapSort(int arr[], int n)

    {

        // Build heap (rearrange array)

        for (int i = n / 2 - 1; i >= 0; i--)

            heapify(arr, n, i);

        // One by one extract an element from heap

        for (int i = n - 1; i >= 0; i--) {

             

            // Move current root to end

            int temp = arr[0];

            arr[0] = arr[i];

            arr[i] = temp;

            // call max heapify on the reduced heap

            heapify(arr, i, 0);

        }

    }

    /* A utility function to print array of size n */

    static void printArray(int arr[], int n)

    {

        for (int i = 0; i < n; ++i)

            System.out.print(arr[i] + " ");

        System.out.println();

    }

    // Driver program

    public static void main(String[] args)

    {

        int arr[] = { 4, 6, 3, 2, 9 };

        int n = arr.length;

        heapSort(arr, n);

        System.out.println("Sorted array is ");

        printArray(arr, n);

    }

}

Hope I answered the questions.

If you have any doubts/queries, feel free to ask by commenting down below. I will respond within 24 hours

And if you like my answer, then please do upvote for it, your feedback really matters alot to me.

STAY HOME STAY SAFE

Add a comment
Know the answer?
Add Answer to:
How would the Java Program look like if I wanted to place the following values in...
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
  • 1. Show what a heap would look like if the following values are inserted one at...

    1. Show what a heap would look like if the following values are inserted one at a time versus using a bulk insert process. Values: 10, 12, 1, 14, 6, 5, 8, 15, 3, 9, 7, 4, 11, 13, 2 2. Perform deleteMin 4 times on the heap from #1 that was inserted one at a time. Show what the heap looks like after each delete.

  • JAVA Each of the following 6 questions refer to the following list of values in the...

    JAVA Each of the following 6 questions refer to the following list of values in the order as shown. 555 288 633 666 444 422 399 477 21. Print the values in the order they would have after the first round of bubble sort’s bubbling operation low to high. 22. Print the values in the order they would have after the first round of selection sort for sorting low to high, assuming it selects the max. 23. Print the values...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • Write a small JAVA program that continually reads in user values and calculates the average of all values. The program s...

    Write a small JAVA program that continually reads in user values and calculates the average of all values. The program should first ask the user for the number of values to be entered. Upon storing this value, the program should proceed to read in that amount of values. What will be the best strategy here? a suggestion would be only calculating the average after you know you have read in all values. You can expect to use some type of...

  • java, any help will do thanks Exercise 8.1.6: Write a program to input the following values...

    java, any help will do thanks Exercise 8.1.6: Write a program to input the following values into an array named prices: 10.95, 16.32, 12.15, 8.22, 15.98, 26.22, 13.54, 6.45, 17.59. After the data was entered, have your program output the values in the order they were entered and in reverse order. Use an array and a loop.

  • can someone please help me with this. I need to use java. Recursion Write and run...

    can someone please help me with this. I need to use java. Recursion Write and run a program that reads in a set of numbers and stores them in a sequential array. It then calls a recursive function to sort the elements of the array in ascending order. When the sorting is done, the main function prints out the elements of the newly sorted array Hint: How do you sort an array recursively? Place the smallest element of the array...

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • Write a Java program with a single-dimension array that holds 11 integer numbers and sort the...

    Write a Java program with a single-dimension array that holds 11 integer numbers and sort the array using a bubble sort. Next, identify the median value of the 11 integers. Here are the steps your program must accomplish. algorithm (either flowchart or pseudocode) that you will use to write the program Step 1. Create an Place the algorithm in a Word document. 6. Ste the Step 2. Code the program in Eclipse and ensure the following steps are accomplished. 1....

  • Please finish all the questions,Thanks Following is Appendix assume the definition of Java class Heap given in the App...

    Please finish all the questions,Thanks Following is Appendix assume the definition of Java class Heap given in the Appendix For this question, a. (2 marks Consider the following heap 30 12 20 19 6 10 18 Given the array representation of a heap discussed in class, what is the array that corre sponds to this heap? b. (5 marks) Successively insert into the heap of part (a.) 22, 35 and 11, in that order. Use the standard heap insertion algorithm....

  • Write a complete JAVA program to do the following program. The main program calls a method...

    Write a complete JAVA program to do the following program. The main program calls a method to read in (from an input file) a set of people's three-digit ID numbers and their donations to a charity (hint: use parallel arrays)Then the main program calls a method to sort the ID numbers into numerical order, being sure to carry along the corresponding donations. The main program then calls a method to print the sorted 1ists in tabular form, giving both ID...

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