Question

In: Computer Science

•Make the steps of the followed the numbers 34, 15, 23, 9, 41, 26, 39, 11,...

•Make the steps of the followed the numbers 34, 15, 23, 9, 41, 26, 39, 11, 7, 28 by using Heap Sort!

Solutions

Expert Solution

The following is the code for above question. In this code, the array will be displayed for each step of heap sort.

public class Main {
    public static int count=0;
    public static void main(String args[]) {
      int elements[] = {9,5,2,12,7,4};
      int n=elements.length,i,temp;
      for(i=n/2-1;i>=0;i--) 
      { //building the max heap
        heapify(elements,n,i);
      }

     for(i=n-1;i>=0;i--) 
     {                     //Heap sort starts
      temp=elements[0];
      elements[0]=elements[i];
      elements[i]=temp;

                               // Heapify root element
      heapify(elements,i,0);
     }
      
        System.out.print("Now the array is sorted");
      
    }
    
      public static void heapify(int elements[], int n, int i) {
      // we have to find the largest among root, left and right child
      int largest=i,l=2*i+1,r=2*i+2,temp;
  
      if(l<n&&elements[l]>elements[largest])
      {
        largest=l;
      }
  
      if(r<n&&elements[r]>elements[largest])
      {
        largest=r;
      }
      if(largest!=i) {  // If root is not largest then swap and continue;
        temp=elements[i];
        elements[i]=elements[largest];
        elements[largest]=temp;
  
        heapify(elements,n,largest);
      }
      count++;
      System.out.printf("Step %d-> ",count);
      for(i=0;i<elements.length;++i)
      {
        System.out.print(elements[i]+" ");
      }
      System.out.println("\n");
    }
  }
  

I am also attaching the output for your reference.

Output:

Step 1-> 9 5 4 12 7 2

Step 2-> 9 5 4 12 7 2

Step 3-> 9 12 4 5 7 2

Step 4-> 9 12 4 5 7 2

Step 5-> 12 9 4 5 7 2

Step 6-> 12 9 4 5 7 2

Step 7-> 9 7 4 5 2 12

Step 8-> 9 7 4 5 2 12

Step 9-> 9 7 4 5 2 12

Step 10-> 7 5 4 2 9 12

Step 11-> 7 5 4 2 9 12

Step 12-> 7 5 4 2 9 12

Step 13-> 5 2 4 7 9 12

Step 14-> 5 2 4 7 9 12

Step 15-> 4 2 5 7 9 12

Step 16-> 2 4 5 7 9 12

Step 17-> 2 4 5 7 9 12

Now the array is sorted

#Please dont forget to upvote if you find the solution helpful. Feel free to ask doubts if any, in the comments section. Thank you.


Related Solutions

Buffalo Boston 26 23 27 14 39 11 23 19 17 19 16 4 21 9...
Buffalo Boston 26 23 27 14 39 11 23 19 17 19 16 4 21 9 31 12 1 12 23 7 32 32 32 26 24 21 42 16 38 16 29 18 16 16 12 20 29 20 16 11 18 10 27 18 2 11 21 17 35 20 21 20 29 25 24 16 17 17 21 8 38 21 9 24 31 26 16 27 24 18 24 17 13 15 21 21 21 32...
43 77 36 40 47 47 39 33 51 43 34 41 31 32 31 23...
43 77 36 40 47 47 39 33 51 43 34 41 31 32 31 23 50 41 43 45 50 44 41 45 40 33 42 25 41 36 38 33 26 54 44 49 21 26 37 43 32 45 38 40 25 62 41 62 45 37 44 43 41 33 37 25 37 40 32 42 56 34 47 52 39 47 41 44 49 34 43 48 49 41 31 48 First, sort the data....
Ages Number of students 15-18 5 19-22 6 23-26 3 27-30 9 31-34 9 35-38 6...
Ages Number of students 15-18 5 19-22 6 23-26 3 27-30 9 31-34 9 35-38 6 Find the relative frequency for the class with lower class limit 19 Relative Frequency = % Give your answer as a percent, rounded to two decimal places A Frequency Distribution Table using data This list of 16 random numbers has been sorted: 22 29 34 34 35 40 43 50 50 50 51 53 54 55 56 56 Fill in this table with the...
) Let X ={35, 45, 39, 41, 41, 44, 46, 48, 49, 34, 12, 50, 20,...
) Let X ={35, 45, 39, 41, 41, 44, 46, 48, 49, 34, 12, 50, 20, 38, 40}(a.) Insert X into R. (a.) Find the mean of X. (b.) Depict X in a boxplot. (NOTE: This question should be answered entirely using code for R.)
Ages Number of students 15-18 6 19-22 7 23-26 9 27-30 6 31-34 8 35-38 8...
Ages Number of students 15-18 6 19-22 7 23-26 9 27-30 6 31-34 8 35-38 8 Find the relative frequency for the class with lower class limit 15 Relative Frequency =  % Give your answer as a percent, rounded to two decimal places
13.Data Set : 41, 18, 21, 34, 90, 39, 41, 14, 45, 53, 50, 82, 38,...
13.Data Set : 41, 18, 21, 34, 90, 39, 41, 14, 45, 53, 50, 82, 38, and 92 a) Compute the percentile of 38 b) Find the 90th percentile c) Find the five number Summary d) Construct a modified Box-Plot e) Determine if data has any potential outliers and explain why?
MT scores: 11, 11, 16, 17, 19, 20, 21, 21 23 24 24 26 26 27...
MT scores: 11, 11, 16, 17, 19, 20, 21, 21 23 24 24 26 26 27 27 28 28 28 29 30 31 31 32 33 35 37 38 38 39 42 44 Questions for Class MT Score Distribution Analysis 1. Create a boxplot of MT scores. 2. Compute the probability that a randomly selected student from the class scored higher than 20. 3. Are the MT scores normally distributed? Why or why not? 4. Assuming a normal fit, compute...
MT scores: 11, 11, 16, 17, 19, 20, 21, 21 23 24 24 26 26 27...
MT scores: 11, 11, 16, 17, 19, 20, 21, 21 23 24 24 26 26 27 27 28 28 28 29 30 31 31 32 33 35 37 38 38 39 42 44 Questions for Class MT Score Distribution Analysis 1. Create a histogram of MT scores. 2. Describe the shape of the MT scores distribution. 3. Compute the mean and standard deviation. 4. Compute the 5-number summary. 5. Create a boxplot of MT scores. 6. Compute the probability that...
In a box are six balls with the numbers 2, 6, 7, 13, 23, 34. (a)...
In a box are six balls with the numbers 2, 6, 7, 13, 23, 34. (a) (2 points) You draw four balls at the same time. What is the probability that two of them are prime numbers? (b) (2 points) Draw a ball, put it back in the box, and draw another ball. Let A be the event that the first ball is even, and let B be the event that the product of the two numbers on the two...
In a box are six balls with the numbers 2, 6, 7, 13, 23, 34. (a)...
In a box are six balls with the numbers 2, 6, 7, 13, 23, 34. (a) (2 points) You draw four balls at the same time. What is the probability that two of them are prime numbers? (b) (2 points) Draw a ball, put it back in the box, and draw another ball. Let A be the event that the first ball is even, and let B be the event that the product of the two numbers on the two...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT