VirtualBox

Ignore:
Timestamp:
Mar 9, 2016 8:06:41 PM (9 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
105937
Message:

Implemented RTSortShell.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/sort/shellsort.cpp

    r57358 r59972  
    3232#include <iprt/sort.h>
    3333
     34#include <iprt/alloca.h>
     35#include <iprt/assert.h>
     36#include <iprt/string.h>
     37
     38
     39
     40RTDECL(void) RTSortShell(void *pvArray, size_t cElements, size_t cbElement, PFNRTSORTCMP pfnCmp, void *pvUser)
     41{
     42    Assert(cbElement <= 32);
     43
     44    /* Anything worth sorting? */
     45    if (cElements < 2)
     46        return;
     47
     48    uint8_t *pbArray = (uint8_t *)pvArray;
     49    void    *pvTmp   = alloca(cbElement);
     50    size_t   cGap    = (cElements + 1) / 2;
     51    while (cGap > 0)
     52    {
     53        size_t i;
     54        for (i = cGap; i < cElements; i++)
     55        {
     56            memcpy(pvTmp, &pbArray[i * cbElement], cbElement);
     57            size_t  j     = i;
     58            while (   j >= cGap
     59                   && pfnCmp(&pbArray[(j - cGap) * cbElement], pvTmp, pvUser) > 0)
     60            {
     61                memmove(&pbArray[j * cbElement], &pbArray[(j - cGap) * cbElement], cbElement);
     62                j -= cGap;
     63            }
     64            memcpy(&pbArray[j * cbElement], pvTmp, cbElement);
     65        }
     66
     67        /* This does not generate the most optimal gap sequence, but it has the
     68           advantage of being simple and avoid floating point. */
     69        cGap /= 2;
     70    }
     71}
    3472
    3573
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette