Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8677581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:29:43+00:00 2026-06-12T20:29:43+00:00

I have written the following heap sort code and I get the wrong output

  • 0

I have written the following heap sort code and I get the wrong output (not sorted) at times and I can’t seem to find why…any help will be much appreciated!

def heap_sort(self, a):

    heapsize = self.build_max_heap(a)

    n = len(a)-1
    i = len(a)-1

    for i in range(i, 0, -1):
        temp = a[0]
        a[0] = a[i]
        a[i] = temp
        heapsize = heapsize - 1
        self.max_heapify(heapsize, a, 0)       #rebuild max heap at with new root

    return a

def max_heapify(self, heapsize, a, i):

    left = (2*(i+1))-1      #left child of i
    right = 2*(i+1)             #right child of i
    largest = i

    if left < heapsize and a[left] > a[i]:
        largest = left

    if right < heapsize and a[right] > a[largest]:
        largest = right

    if largest != i:
        temp = a[largest]
        a[largest] = a[i]
        a[i] = temp
        self.max_heapify(heapsize, a, largest)

def build_max_heap(self, a):

    heapsize = len(a)
    i = int(heapsize/2)-1

    for i in range(i, 0):
        self.max_heapify(heapsize, a, i)

    return heapsize

My tests:

#--Test for 0 in array--#
def zero_array(self):
    a = [12,0,232]
    print self.sort.heap_sort(a)
    return

#--Test for duplicate in array--#
def duplicate_array(self):
    a = [12, 12, 7]
    print self.sort.heap_sort(a)
    return

#--Test for all same values in array--#
def allsame_array(self):
    a = [1,1,1]
    print self.sort.heap_sort(a)
    return

#--Test for negative values in array--#
def negative_array(self):
    a = [-23, -2, 123]
    print self.sort.heap_sort(a)
    return

Output (which is supposed to all be sorted):

    [0, 232, 12]
    [7, 12, 12]
    [1, 1, 1]
    [-2, 123, -23]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T20:29:46+00:00Added an answer on June 12, 2026 at 8:29 pm

    It could be because your second range() is incrementing forwards instead of backwards.

    for i in range(i, 0):
    

    Should be:

    for i in range(i, 0, -1):
    

    You also need to keep in mind that range() will stop just before the second argument, so range(5, 0, -1) will return [5, 4, 3, 2, 1].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written the following heap sort code and I get the wrong output
I have written a following code to get just the file name without extension
I have written following code to get location name package demo.gps.locname; import java.io.IOException; import
I have written following mergesort code, but its not working. Could you please help
I have written following code to find out the coordinates of list of countries.
I have written following regex But its not working. Can you please help me?
I have written following code to get JSON result from webservice. function SaveUploadedDataInDB(fileName) {
I have written following code to get an integer from keyboard. It will prompt
I have written following code to pass File1 variable to Javascript, but its not
I have written following code to attach gesture recogniser to multiple imageviews. [imageview1 setUserInteractionEnabled:YES];

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.