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

  • Home
  • SEARCH
  • 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 8259669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:55:08+00:00 2026-06-08T02:55:08+00:00

I’m working on a quick sort algorithm for a Huffman coding project I’m working

  • 0

I’m working on a quick sort algorithm for a Huffman coding project I’m working on (explains why all the function names start with huff). When walking through it with the debugger, the function seems to freeze when finding the highest term (when trying to find the term from the right side of the vector which “should not” be on that side). There may (probable are) other problems with this code but i’m focussing on this one for now. By the way, most of the time (all of the time) I call cout, it’s for debugging purposes.

Edit: A lot of corrections have been made to my code from the comments, but none of them fix my problem. For that reason, I’m updating the code.

void huff_sort_partition(vector<Node*>* v, int b, int e){
    int tempt = b+((rand()%(e-b))+1);
    int p_idx = (*v)[tempt]->weight;
    cout << tempt << endl;
    int l = b+0;
    int r = e;
    cout << "P:" << p_idx << "  L-R:" << l << "-" << r << endl;
    while(l < r){
        while((*v)[l]->weight < p_idx){
            l++;
        }
        while((*v)[r]->weight > p_idx){
            r--;
        }
        Node* s = (*v)[b+l];
        (*v)[b+l] = (*v)[b+r];
        (*v)[b+r] = s;
    }

    huff_sort_partition(v, b, l-1);
    huff_sort_partition(v, l+1, e);
}
void Huff::huff_sort(vector<Node*>* v){
    srand ( time(NULL) );
    cout << "------sort------" << endl;
    huff_sort_partition(v, 0, v->size());
}

Edit: I thought I’d add this since no one has answered this yet. If the code “should” work, then comment that (that way I can look for a reason outside this code for why it wont work).

  • 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-08T02:55:09+00:00Added an answer on June 8, 2026 at 2:55 am

    Consider what happens in your code when there are several nodes with the pivot weight – for simplicity, consider the weights [1, 9, 5, 2, 7, 5, 6, 8, 3, 7] and perchance the pivot index is 5, so

    void huff_sort_partition(vector<Node*>* v, int b, int e){
        int p = (*v)[b+(rand() % (e - b + 1))]->weight;
    

    we have p = 5

        int l = 0;
        int r = e-b;
    

    l = 0 and r = 9

        cout << "P:" << p << "  L-R:" << l << "-" << r << endl;
        while(l < r){
            while((*v)[b+l]->weight < p){
                l++;
            }
    

    1 < 5, then increment l, l = 1, v[1] = 9 > 5.

            while((*v)[b+r]->weight > p){ // where it freezes up and wont move on
                r--;
            }
    

    7 > 5, decrement r, r = 8, v[8] = 3 < 5. Swap v[1] and v[8], giving [1, 3, 5, 2, 7, 5, 6, 8, 9, 7].

    Next round, l = 1 < 8 = r. v[1] = 3 < 5, l becomes 2, v[2] = 5 isn’t smaller than 5, end of loop. Now the second inner loop is entered, v[8] = 9 > 5, v[7] = 8 > 5, v[6] = 6 > 5; v[5] = 5 isn’t larger than 5, swap v[2] and v[5], giving [1, 3, 5, 2, 7, 5, 6, 8, 9, 7].

    Next round, l = 2 < 5 = r, v[2] = 5 isn’t smaller than 5, v[5] = 5 isn’t larger than 5, swap v[2] and v[5]. Oops, we’re stuck.

    The usual way to prevent this is to swap the pivot out of the way and have one of the two conditions a weak inequality, also one must check the condition l < r also in the inner loops, or in the case all entries are equal one would run off the end of the array/vector. Then after partitioning, one swaps the pivot into the right place.

    The following code uses the standard way (untested, typos possible):

    void huff_sort_partition(vector<Node*>* v, int b, int e){
        // Nothing to sort if there are fewer than two elements
        if (e <= b) return;
        int tempt = b+((rand()%(e-b))+1);
        int p_idx = (*v)[tempt]->weight;
        // swap pivot out of the way
        Node *tmp_Node = (*v)[tempt];
        (*v)[tempt] = (*v)[e];
        (*v)[e] = tmp_Node;
        cout << tempt << endl;
        int l = b;
        int r = e - 1;
        cout << "P:" << p_idx << "  L-R:" << l << "-" << r << endl;
        while(l < r){
            while((l < r) && (*v)[l]->weight < p_idx){
                l++;
            }
            while((l < r) && (*v)[r]->weight >= p_idx){
                r--;
            }
            if (l < r){
                Node* s = (*v)[l];
                (*v)[l] = (*v)[r];
                (*v)[r] = s;
                // stuff at l and r is okay now, we don't need to test again
                ++l;
                --r;
            }
        }
        // Now l is the first index with weight >= pivot weight,
        // swap pivot into place
        tmp_Node = (*v)[l];
        (*v)[l] = (*v)[e];
        (*v)[e] = tmp_Node;
    
        huff_sort_partition(v, b, l-1);
        huff_sort_partition(v, l+1, e);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Specifically, suppose I start with the string string =hello \'i am \' me And
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.