Question

In: Computer Science

Hi, so for my homework assignment, we are meant to change this function so that instead...

Hi, so for my homework assignment, we are meant to change this function so that instead of making a list from the array going up in an ascending order, making it be listed in descending order, meaning going from largest number to smallest number, the code I have now works for the ascending order, but how would I change this for it to go descending? Any help would be amazing, and please with a tutorial, thanks so much.

def heapify(arr, n, i):
largest = i # Initialize largest as root
l = 2 * i + 1 # left = 2*i + 1
r = 2 * i + 2 # right = 2*i + 2
# See if left child of root exists and is
# greater than root
if l < n and arr[i] < arr[l]:
largest = l
# See if right child of root exists and is
# greater than root
if r < n and arr[largest] < arr[r]:
largest = r
# Change root, if needed
if largest != i:
arr[i], arr[largest] = arr[largest], arr[i] # swap
# Heapify the root.
heapify(arr, n, largest)

Solutions

Expert Solution

def heapify(arr, n, i):
    smallest = i  # Initialize smallest as root 
    l = 2 * i + 1  # left = 2*i + 1 
    r = 2 * i + 2  # right = 2*i + 2 

    # See if left child of root exists and is 
    # less than root 
    if l < n and arr[i] >= arr[l]:
        smallest = l

    # See if right child of root exists and is 
    # less than root 
    if r < n and arr[smallest] >= arr[r]:
        smallest = r

    if smallest != i:
        arr[i], arr[smallest] = arr[smallest], arr[i]  # swap 

        # Heapify the root. 
        heapify(arr, n, smallest)

    # The main function to sort an array of given size 


def heapSort(arr):
    n = len(arr)

    # Build a maxheap. 
    for i in range(n, -1, -1):
        heapify(arr, n, i)

    # One by one extract elements 
    for i in range(n - 1, 0, -1):
        arr[i], arr[0] = arr[0], arr[i]  # swap 
        heapify(arr, i, 0)

    # Driver code to test above 


arr = [12, 11, 13, 5, 6, 7]
heapSort(arr)
n = len(arr)
print("Sorted array is")
for i in range(n):
    print("%d" % arr[i]),

Sorted array is 13 12 12 11 7 6


Related Solutions

Hi, my homework assignment is to explain what wokld happen if we only used WACC (Weighted...
Hi, my homework assignment is to explain what wokld happen if we only used WACC (Weighted Average Cost of Capital) to decide on projects, no matter how risky they are? Any help would be appreciated, thanks!
Validating Input file, So for my C++ assignment, my professor says we need to validate the...
Validating Input file, So for my C++ assignment, my professor says we need to validate the Input File, Which means, if the line is missing a comma, one of the 3 information, or the string is a white line, it will ignore it. If the line has a white line, the program will correct it and will read in the line. I will store these into an Array, and display them in First name, Last name, and Number of Votes...
This question is in my plant physiology homework and the assignment is on the electron transport...
This question is in my plant physiology homework and the assignment is on the electron transport chain. How is the H+ gradient (potential energy) used? What are the products that the proton gradient made? Where in the chloroplast are they made? (3 points)
Checking Input File for correct line format. So for my C++ assignment, my professor says we...
Checking Input File for correct line format. So for my C++ assignment, my professor says we need to validate the Input File, if there are white lines and spaces, in which it will ignore them, separated by a comma, and that all data items are there. This will be used in a program where i will store these into an Array, and display them in First name, Last name, and Number of Votes. This is a sample of the txt...
Checking Input File for correct line format. So for my C++ assignment, my professor says we...
Checking Input File for correct line format. So for my C++ assignment, my professor says we need to validate the Input File, if there are white lines and spaces, in which it will ignore them, separated by a comma, and that all data items are there. This will be used in a program where i will store these into an Array, and display them in First name, Last name, and Number of Votes vertical columned categories. This is a sample...
This isn't a homework assignment more of a search for advice. The lab experiment that my...
This isn't a homework assignment more of a search for advice. The lab experiment that my group is conducting is to separate two mixtures, a solid-solid and a liquid-liquid mixture. Each mixture has 2 separate components. The solid-solid mixture was easy enough, a quick solvent extraction completely separated the two and soon we will do IR, NMR and melting point determination to try to identify the solids. The liquids however are another problem all in themselves. The mixture is tinted...
Hi I am not sure how to go about answering this question on my homework. ABC...
Hi I am not sure how to go about answering this question on my homework. ABC corporation adopts a compensatory stock option plan. the plan covers 25 executives and provides each executive 300 otions to purchase stock for $100/share. the options vest in 3 years. the plan starts on 1/1/18 and matures 12/31/20. we assume a 95% retention rate. at 12/31/20 22 executives earn the options. each option is valued at $17/share. record the journal entries for 12/31/18, 12/31/19, and...
Hi guys, I'm working on an assignment for my Java II class and the narrative for...
Hi guys, I'm working on an assignment for my Java II class and the narrative for this week's program has me a bit lost. I've put the narrative of the assignment in the quotation marks below. " Included with this assignment is an encrypted text file. A simple Caesar cipher was used to encrypt the data. You are to create a program which will look at the frequency analysis of the letters (upper and lowercase) to help “crack” the code...
Hi, I am doing up my homework on mathematics and here are some questions. I want...
Hi, I am doing up my homework on mathematics and here are some questions. I want to cross-reference my answers thank you. Decide if the statements below are True or False. Please include some rough workings for me to understand: (1) Mr. Tan borrowed $500,000 from Bank XYZ at 5% annual interest to be repaid monthly over 20 years. The amount that he pays back to XYZ each month is between $3000 to $3500. (2) Continuing from (1): after 15...
Hi I am working on my chemistry homework about acids and bases and I am confused...
Hi I am working on my chemistry homework about acids and bases and I am confused on this question from a lab. We used grape juice and cranberry juice and we had to put HCl and NaOH in the test tube full of juice. 1. Referring back to procedure 1 of the experiment, which of the two juices is more useful as a general pH indicator and why? I think it is grape juice but I just don't know what...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT