CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2010
    Posts
    28

    Generic heapsort question involving strings

    I'm writing a generic heapsort function that wil sort a group of words in ascending order. Here's what I'm confused about:


    Build longer strings from the input strings as you read them, e.g., convert xyz to xyz<blank>12345, where <blank> is a space. Note that the numbers must be filled out to five digits in order for the comparison to work. If you use this solution, you can use string functions to strip off the sequence numbers after the sort.

    As indicated in my assignment, I'm to sort the words using a stable (sequence-preserving) sort. Couldn't I just use the algorithm stable_sort to sort the words or am I missing something.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Generic heapsort question involving strings

    You could use stable_sort, or even the STL algorithms make_heap() and pop_heap() if you really need it to be heapsort.

    I have no idea what that bit about modifying the strings is, though. Must be something specific to your assignment's proposed solution.

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Generic heapsort question involving strings

    Quote Originally Posted by Lindley View Post
    You could use stable_sort, or even the STL algorithms make_heap() and pop_heap() if you really need it to be heapsort.
    Yes, stable sort works quite well. However, if you really need to heap sort, the algorithm also has a sort_heap algorithm. So call "make_heap" than "sort_heap". You could use pop_heap, but you'd just be doing the same thing, by hand and with more overhead.

    Quote Originally Posted by Lindley View Post
    I have no idea what that bit about modifying the strings is, though. Must be something specific to your assignment's proposed solution.
    Ditto. That's a real *** to me.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Generic heapsort question involving strings

    Right, I'd forgotten about that algorithm.

  5. #5
    Join Date
    Mar 2002
    Location
    Kent, United Kingdom
    Posts
    399

    Re: Generic heapsort question involving strings

    I would hazard a guess to say that the assignment requires all strings to be the same length, so they are to be padded with "<space>1234..." up to however long they need to be.
    your humble savant

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured