Question

Can someone please do it using the java language and please do it ASAP ? I...

Can someone please do it using the java language and please do it ASAP ? I would appreciate it.

Use the Heap class provided to implement a sort routine in a Main class where the user enters a series of values, each value is then pushed onto a heap, then the values are printed out in ascending order.

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

For solving the above question I firstly made a MAX heap form inserted values and then swap the root with end value and reduced the size of heap by 1. In this way, elements are sorted in ascending order.

Code is :-

public class Heap
{
    public static void main(String args[])
    {
        int values[] = {2,53,23,12,54,76,11,1};       // insertin random values in heap
        int n = values.length;          //calculating size of array
        sort(values);           // funtion to sort using heap sort
        System.out.println("Sorted array is");      //printing the elements in ascending order
        for (int i=0; i<n; ++i)
            System.out.print(values[i]+" ");
    }

    public static void sort(int values[])
    {
        int n = values.length;
        for (int i = (n / 2 - 1); i >= 0; i--)      // Building  heap by rearranging array
            heapify(values, n, i);
        for (int i=n-1; i>0; i--)       // One by one extract an element from heap
        {                               // Move current root to end
            int temp = values[0];
            values[0] = values[i];
            values[i] = temp;
            heapify(values, i, 0);                 // call max heapify on the reduced heap
        }
    }

    static void heapify(int arr[], int n, int i)
    {
        int large = i;
        int left = 2*i + 1; // left = 2*i + 1
        int right = 2*i + 2; // right = 2*i + 2
        if (left < n && arr[left] > arr[large])     //if left child is larger
            large = left;
       if (right < n && arr[right] > arr[large])    // If right child is largest
           large = right;
        if (large != i)     // If root is not largest than swap
        {
            int temp = arr[i];
            arr[i] = arr[large];
            arr[large] = temp;
            heapify(arr, n, large);     // recusive call to heapfiy function for sorting the subtree
        }
    }
}

Screenshot of code and it's output is :-

1 2 3 public class Heap { public static void main(String args[]) 4 { 5 6 7 int values[] = {2,53, 23,12,54,76,11,1}; // insert27 28 29 30 31 static void heapify(int arr[], int n, int i) { int large = i; int left = 2*i + 1; // left = 2*i + 1 int right

output -

C:\Program Files\Java\jdk1.8.0_181\bin\java.exe Sorted array is 1 2 11 12 23 53 54 76 Process finished with exit code o

HOPE YOU GOT IT !!!

Add a comment
Know the answer?
Add Answer to:
Can someone please do it using the java language and please do it ASAP ? I...
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
  • 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...

  • do in C++ language thank you Task 4: Functions or classes are ok. Please read data...

    do in C++ language thank you Task 4: Functions or classes are ok. Please read data into program from descending_mostly_sorted.txt and store into an array. Implement Selection Sort to sort the values in ascending order. Least → Greatest Measure the time it takes to execute the sort in milliseconds or even nanoseconds. Please run the sort 3 times. Attach Photos of Source Code and Output Task 5: Functions or classes are ok. Please read data into program from descending_mostly_sorted.txt and...

  • simple PYTHON 3 language. please comment each line I want to understand. I know the picture...

    simple PYTHON 3 language. please comment each line I want to understand. I know the picture says java but please complete code in PYTHON. please use menu driven [E] The "back" button used in a web browser is a great example of how stacks may be used. When a user visits a new web page, the current page gets pushed onto the stack. When the user clicks the back button, the last page pushed onto the stack is popped off...

  • PLEASE HELP!!!I need help with the following JAVA PROGRAMS. Please ready carefully. So this comes from...

    PLEASE HELP!!!I need help with the following JAVA PROGRAMS. Please ready carefully. So this comes from tony gaddis starting out with java book. I have included the screenshot of the problems for reference. I need HELP implementing these two problems TOGETHER. There should be TWO class files. One which has TWO methods (one to implement problem 8, and one to implement problem 9). The second class should consist of the MAIN program with the MAIN method which calls those methods....

  • please write code in java language and do not add any break, continue or goto statements...

    please write code in java language and do not add any break, continue or goto statements Write a program with a graphical interface that allows the user to convert an amount of money between U.S. dollars (USD), euros (EUR), and British pounds (GBP). The user interface should have the following elements: a text box to enter the amount to be converted, two combo boxes to allow the user to select the currencies, a button to make the conversion, and a...

  • Programming Assignment 6 Write a Java program that will implement a simple appointment book. The ...

    Programming Assignment 6 Write a Java program that will implement a simple appointment book. The program should have three classes: a Date class, an AppointmentBook class, and a Driver class. • You will use the Date class that is provided on Blackboard (provided in New Date Class example). • The AppointmentBook class should have the following: o A field for descriptions for the appointments (i.e. Doctor, Hair, etc.). This field should be an array of String objects. o A field...

  • HI CAN YOU PLEASE PROGRAM THIS ASSIGNMENT USING C++ Ask the user to input a string. Then sort the string in reverse lex...

    HI CAN YOU PLEASE PROGRAM THIS ASSIGNMENT USING C++ Ask the user to input a string. Then sort the string in reverse lexicographic order and print it out. Reverse lexicographic means reverse alphabetic, e.g. zyxwvuts...edcba. This would be done using the integer values of the characters (ASCIl table). Use any one of the sorting methods we saw in class. For example: if the user enters "zeta" sorting it in reverse lexicographic order would give "ztea. if the user enters "ZETA"...

  • Can someone please explain how to do this asap?? I'm not able to get the right...

    Can someone please explain how to do this asap?? I'm not able to get the right answer, I would greatly appreciate it a. 27) The production function is y(L,K) = 2L1/2K1/2, where L is the number of units of labour and K is the number of units of capital used. If the cost of labour is $36 per unit and the cost of capital is $4 per unit, then the lowest total cost of producing 48 units of output will...

  • Language- Java It’s urgent, please help ASAP! lE will result in a zero for the entire...

    Language- Java It’s urgent, please help ASAP! lE will result in a zero for the entire uhTe R will be submitted for you when time is up. D | Question 43 35 pts Write a recursive method to caculate the sum of the following convergent series of the alternating signs of the reciprocals of powers of 2 out to a number of terma given as a parameter: 3 1 2 4 8 16 32 The header will be as follows...

  • would someone please kind enough to help me out on my intro to java study guide...

    would someone please kind enough to help me out on my intro to java study guide by helping me with some useful tips/ examples or anything on these questions. My grade is on the line and would really appreciate your help! Final exam study guide 13. truncating vs rounding - Where do you encounter truncation? Where do you encounter rounding? 14. Can you identify the following items in a Java source code example (using just syntax, usage, and naming convention)?...

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