ningenkawai

rearrange

#include <stdio.h>

#include <cinttypes>

#include <string.h>

struct node {
    int32_t val;
    int32_t offset;
};
node base_arr[12];
int32_t count = 0;

void rearrange(node * arr, int32_t size)
{

    for (int32_t idx = 0; idx < size; idx++)
    {
        int32_t limit = arr[idx].val + arr[idx].offset;
        printf("\n-----------------\nrearrange, idx = %02d, count = %02d", idx, count);
        memcpy( & base_arr[count], & arr[idx], sizeof(node));
        count++;
        for (int32_t si = idx + 1; si < size; si++)
        {
            if (arr[si].val <= limit)
            {
                idx++;
            }
            else
            {
                break;
            }
        }
    }
}

int main() {
    node arr[12];
    for (int s = 0; s < 12; s++)
    {
        arr[s].val = s;
        arr[s].offset = 0;
    }
    arr[5].offset = 2;
    printf("IDX = ");
    for (int i = 0; i < 12; i++)
    {
        printf("%02d ", i);
    }
    printf("\nVAL = ");
    for (int i = 0; i < 12; i++)
    {
        printf("%02d ", arr[i].val);
    }
    printf("\nOff = ");
    for (int i = 0; i < 12; i++)
    {
        printf("%02d ", arr[i].offset);
    }
    rearrange(arr, 12);
    printf("\n-----------------\n");
    printf("IDX = ");
    for (int i = 0; i < 12; i++)
    {
        printf("%02d ", i);
    }
    printf("\nVAL = ");
    for (int i = 0; i < 12; i++)
    {
        printf("%02d ", base_arr[i].val);
    }
    printf("\nOff = ");
    for (int i = 0; i < 12; i++)
    {
        printf("%02d ", base_arr[i].offset);
    }
}