-
April 25th, 2010, 09:07 PM
#1
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.
-
April 25th, 2010, 09:15 PM
#2
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.
-
April 26th, 2010, 02:53 AM
#3
Re: Generic heapsort question involving strings
Originally Posted by Lindley
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.
Originally Posted by Lindley
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.
-
April 26th, 2010, 09:19 AM
#4
Re: Generic heapsort question involving strings
Right, I'd forgotten about that algorithm.
-
April 26th, 2010, 10:50 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|