Numerically Sorting Values (Shell Sort)

Hi, I'm having troubles with my sorting algorithm that I got from mathbits.com. It does the job very well, however almost too well. This is a sample of what I inputted
{T79_W1, T7_W1, T79_W11, T78_W3, T7_W2, T79_W3, T79_W10, T78_W4, T79_W2}
which comes out
{T78_W3, T78_W4, T79_W1, T79_W10, T79_W11, T79_W2, T79_W3, T7_W1, T7_W2}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void sort (std::vector<std::string> &ToSort)
{
int flag = 1, ToSortLength = ToSort.size();
int d = ToSortLength;
std::string temp;

while (flag || (d > 1))
{
flag = 0;
d = (d + 1)/2;
for (int k = 0;k < (ToSortLength - d);k+++
{
if (ToSort[k + d] < ToSort[k])
{
temp = ToSort[k + d];
ToSort[k + d] = ToSort[k];
ToSort[k] = temp;
flag = 1
}
}
}
}


Sorry if that didn't come out looking so pretty, new to this, plus had to type it out since my code is on a virtual machine.

For a description of what this actually does, see http://mathbits.com/MathBits/CompSci/Arrays/Shell.htm. I guess this is more a logic problem than anything else but I was wondering if anyone could think of a clever way to sort this out, been staring at it for a while now!

Thank you!
Last edited on
closed account (o3hC5Di1)
Hi there,

I'm entirely sure about the problem you are describing, is it the following:

T79_W1, T79_W10, T79_W11, T79_W2, T79_W3


That 10 and 11 are sorted before 2?

If so, what you need is "natural sorting", unfortunately I can't give any advice on the topic myself, but a quick google for that term (appended by c++) should get you there.

Hope that helps.
All the best,
NwN

Sorry, to clarify I want to sort them so that the numbers go up in a numerical progression as opposed to just being sorted in ascending order e.g. 1, 10, 11, 2 I would prefer to 1, 2, 10, 11. Thanks for the tip!
closed account (o3hC5Di1)
Thought so - in that case you'll need natural sorting - perhaps one of the experts around here can give you more specific advice.

Sorry I couldn't be of more help.

All the best,
NwN
Topic archived. No new replies allowed.