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 8908729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:15:26+00:00 2026-06-15T03:15:26+00:00

I have implemented a set of sorting algorithms in javascript, for training purposes. My

  • 0

I have implemented a set of sorting algorithms in javascript, for training purposes. My implementations has been successfull in sorting integer and single-character string arrays, except for the heapsort.

My implementation sorts the array correctly, with the exception of putting the smallest number to the end, when returning the sorted array.

I’m not a CS student/graduate, nor I have got a lot of programming background. I’m learning via read/try/fail method, and I couldn’t get to make this work correctly.

I’ll put the code and it’s results below this paragraph. Lastly, I want to add that I’m taking the pseudocode at this Wikipedia article as the reference of my implementation.

function heapSort(intl)
{
    var l   = intl.length,
        end = l - 1,
        swap;
    intl = _heapify(intl,l);

    while(end>0)
    {
        swap      = intl[0];
        intl[0]   = intl[end];
        intl[end] = swap;
        --end;
        intl = _siftDown(intl, 0, end);
    }

    return intl;
}


function _heapify(intl, l)
{
    var start = (l - 2) / 2;
    while(start >= 0)
    {
        intl = _siftDown(intl, start, l-1);
        --start;
    }
    return intl;
}

function _siftDown(intl, start, end)
{
    var root = start,
        child, swap, swapr;
    while(root*2+1 <= end)
    {
        child = root * 2 + 1;
        swap = root;
        if(intl[swap] < intl[child])
            swap = child;
        if(child+1 <= end && intl[swap]<intl[child+1])
            swap = child + 1;
        if(swap!=root)
        {
            swap       = intl[root];
            intl[root] = intl[swap];
            intl[swap] = swap;
            root       = swap;
        }else
        {
            return intl;
        }
    }
    return intl;
}


x =
[24,5,896,624,437,5,6,4,37,45,654];
y =
["a","b","r","s","t","e","q","u","q"];

console.log(heapSort(x),"\n",heapSort(y));

Running the code via nodejs:

$ node sort.js
[ 5, 5, 6, 24, 37, 45, 437, 624, 654, 896, 4 ]
[ 'b', 'e', 'q', 'q', 'r', 's', 't', 'u', 'a' ]

I’ve tried to find where the error is, with altering the code, but I couldn’t find it. Can anyone tell where the problem is? Thanks in advance.

  • 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-15T03:15:27+00:00Added an answer on June 15, 2026 at 3:15 am

    I have seen 2 errors :

    1. In the _heapify function, the start variable is not an integer if l is not odd :

      var start = Math.floor( ( l - 2 ) / 2 );
      
    2. In the _siftDown function, you have used swap instead of swapr when you effectively swap the 2 elements of your array :

      swapr      = intl[root];  // <-- Here swapr instead of swap
      intl[root] = intl[swap];
      intl[swap] = swapr;       // <-- Here swapr instead of the 1st occurrence of swap
      root       = swapr;       // <-- Here swapr instead of swap
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have implemented the following set up (after being requested): slideshow of images changing,
I have implemented paging for a large set of data in an application by
I have written a set of classes and interfaces that are implemented in Moose
I have implemented a custom IPrincipal that I set in protected void Application_PostAuthenticateRequest(Object sender,
I have a class with 2 NSNotifications implemented //Set up notifications [[NSNotificationCenter defaultCenter] addObserver:self
I have successfully implemented the mandelbrot set as described in the wikipedia article, but
I have implemented software which have a DLL library which contains a set of
I have implemented a ShoppingCart where the CartItem s are put in a Set
I have to do a sorting on headers of the gridview. i have set
I have to implement an application in which I have set small edittext for

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.