예제 #1
0
//==============================================================================
void selection_sort(std::vector<int>& data)
{
    // Do nothing if empty vector (note unsigned 0 - 1 is a big number)
    if (data.size() == 0)
        { return; }

    // Index of last element in vector, also last in unsorted part
    Vec_Idx last = data.size() - 1;

    // Do the sort
    while (last > 0)
    {
        // Find greatest in unsorted part
        Vec_Idx idx_of_greatest = 0;
        for (Vec_Idx idx = 0; idx <= last; ++idx)
        {
            if ( LESS_THAN(data[idx_of_greatest], data[idx]) )
            {
                // Remember as new greatest so far
                idx_of_greatest = idx;
            }
        }

        // Swap last in unsorted with greatest in unsorted part
        SWAP(data[last], data[idx_of_greatest]);

        // Increase sorted part
        --last;
    }
}
예제 #2
0
//==============================================================================
// The unsigned ints cause problems here, jdx may go to -1.
// Subscripts are cast so there are no warnings.
void quick_sort(std::vector<int>& data, int left, int right)
{
      // Calculate the pivot
      int pivot = data[Vec_Idx((left + right) / 2)];

      // Partition
      int idx = left, jdx = right;
      while (idx <= jdx) {
            while (LESS_THAN(data[Vec_Idx(idx)], pivot))
                  idx++;

            while (GREATER_THAN(data[Vec_Idx(jdx)], pivot))
                  jdx--;

            if (idx <= jdx)
            {
                  SWAP(data[Vec_Idx(idx)], data[Vec_Idx(jdx)]);
                  idx++;
                  jdx--;
            }
      }

      // Recurse
      if (left < jdx)
            { quick_sort(data, left, jdx); }

      if (idx < right)
            { quick_sort(data, idx, right); }
}
예제 #3
0
파일: 195.c 프로젝트: pandeyiyer/acm
static void permute(char *p, int n)
{
    register int i, j, k;
    register char c;

    if (n < 2) {
        for (i = 0; i < n; i++) putchar(p[i]);
        putchar('\n');
        return;
    }

    for (i = 0; i < n; i++)
        for (j = i + 1; j < n; j++)
            if (LESS_THAN(p[j], p[i]))
            {
                c=p[i];
                p[i]=p[j];
                p[j]=c;
            }

    for (;;) {
        p[n] = '\n';
        fwrite(p, 1, n + 1, stdout);

        for (i = n - 1; i >= 0; i--)
            if (LESS_THAN(p[i], p[i + 1])) break;
        if (i < 0) break;

        for (k=(i+1), j=(i+2); j < n; j++)
            if (LESS_THAN(p[i], p[j]) && LESS_THAN(p[j], p[k]))
                k = j;

        c = p[i];
        p[i] = p[k];
        p[k] = c;
        for (i++; i < n; i++)
            for (j = i + 1; j < n; j++)
                if (LESS_THAN(p[j], p[i]))
                {
                    c=p[i];
                    p[i]=p[j];
                    p[j]=c;
                }
    }
}
예제 #4
0
//==============================================================================
void bubble_sort(std::vector<int>& data)
{
    // Go through vector repeatedly
    for(Vec_Idx limit = data.size(); limit > 0; limit--)
    {
        // Go through vector once, swap element and next element if out of order
        for(Vec_Idx idx = 0; idx < limit - 1; idx++)
        {
            if( LESS_THAN(data[idx + 1], data[idx]) )
            {
                SWAP(data[idx],data[idx + 1]);
            }
        }
    }
}
예제 #5
0
size_t SegmentPool::nextPointer(size_t pointer, uint32_t pivot, uint32_t reverse) const
{
    if (pointer == UNDEFINED_POINTER)
        return UNDEFINED_POINTER;

    uint32_t pSegment = DECODE_SEGMENT(pointer);
    uint32_t pOffset = DECODE_OFFSET(pointer);

    while (LESS_THAN(pool_[pSegment][pOffset + 3], pivot, reverse))
    {
        uint32_t oldSegment = pSegment;
        uint32_t oldOffset = pOffset;
        if ((pSegment = pool_[oldSegment][oldOffset + 1]) == UNDEFINED_SEGMENT)
            return UNDEFINED_POINTER;

        pOffset = pool_[oldSegment][oldOffset + 2];
    }

    return ENCODE_POINTER(pSegment, pOffset);
}
예제 #6
0
uint32_t gallopSearch(
        const uint32_t* block,
        bool reverse,
        uint32_t count,
        uint32_t index,
        uint32_t pivot)
{
    if (index >= count || LESS_THAN(block[count - 1], pivot, reverse))
        return INVALID_ID;

    if (GREATER_THAN_EQUAL(block[index], pivot, reverse))
        return index;

    if (block[count - 1] == pivot)
        return count - 1;

    int beginIndex = index;
    int hop = 1;
    int tempIndex = beginIndex + 1;
    while ((uint32_t)tempIndex < count && LESS_THAN_EQUAL(block[tempIndex], pivot, reverse))
    {
        beginIndex = tempIndex;
        tempIndex += hop;
        hop *= 2;
    }

    if (block[beginIndex] == pivot)
        return beginIndex;

    int endIndex = count - 1;
    hop = 1;
    tempIndex = endIndex - 1;
    while (tempIndex >= 0 && GREATER_THAN(block[tempIndex], pivot, reverse))
    {
        endIndex = tempIndex;
        tempIndex -= hop;
        hop *= 2;
    }

    if (block[endIndex] == pivot)
        return endIndex;

    // Binary search between begin and end indexes
    while (beginIndex < endIndex)
    {
        uint32_t mid = beginIndex + (endIndex - beginIndex) / 2;

        if (GREATER_THAN(block[mid], pivot, reverse))
        {
            endIndex = mid;
        }
        else if (LESS_THAN(block[mid], pivot, reverse))
        {
            beginIndex = mid + 1;
        }
        else
        {
            return mid;
        }
    }

    return endIndex;
}