Alex Sokolek
Member
- Joined
- Apr 5, 2024
Hi. Does anyone recognize this sort algorithm?
BOOL swap;
tagFileNode* TempNode;
int i, j, diff = 0;
for (j = _NodeCount / 2; j > 0; j /= 2)
{
swap = true;
while (swap)
{
swap = false;
for (i = 0; i + j < _NodeCount; ++i)
{
if (compare(_NodeList[i + 0]->Item, _NodeList[i + j]->Item) > 0)
{
swap = true;
TempNode = _NodeList[i+0];
_NodeList[i+0] = _NodeList[i + j];
_NodeList[i + j] = TempNode;
}
}
}
}
It is similar to a bubble-sort, except that it has an outer loop that runs the interval (j) from half the list, then a quarter of the list, then an eigth of the list, and so on until it is at an interval of 1. I get a factor of ten improvement in performance over the bubble-sort.
I think this is a merge-exchange-sort, but i'm not certain, so I'm asking for insight. Thanks.
p.s. Does anyone know how to include code and keep the indents. I tried tab and spaces - it did not work.
BOOL swap;
tagFileNode* TempNode;
int i, j, diff = 0;
for (j = _NodeCount / 2; j > 0; j /= 2)
{
swap = true;
while (swap)
{
swap = false;
for (i = 0; i + j < _NodeCount; ++i)
{
if (compare(_NodeList[i + 0]->Item, _NodeList[i + j]->Item) > 0)
{
swap = true;
TempNode = _NodeList[i+0];
_NodeList[i+0] = _NodeList[i + j];
_NodeList[i + j] = TempNode;
}
}
}
}
It is similar to a bubble-sort, except that it has an outer loop that runs the interval (j) from half the list, then a quarter of the list, then an eigth of the list, and so on until it is at an interval of 1. I get a factor of ten improvement in performance over the bubble-sort.
I think this is a merge-exchange-sort, but i'm not certain, so I'm asking for insight. Thanks.
p.s. Does anyone know how to include code and keep the indents. I tried tab and spaces - it did not work.
Last edited: