我想选择C中的排序

我想用C中的struct选择排序。

这是作业。我想了6个小时,但无法完成。

I think, for example, next address of the address of s_data[0].int_num is not s_data[1].int_num? But I couldn't find a solution.

条件是:即使更改数据,我也只能修复比较功能。这是我尝试显示的未完成的代码。 如果出现问题,请告诉我。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <memory.h>

typedef struct numData
{
    int int_num;
    double double_num;
} numData;

typedef struct numData2
{
    int no;
    double no2;
} numData2;

void myprint(numData *data, int index);
int mysort2(void *data, int num, int size, int (*compare)(const void *, const void *));
int compare(const void *n1, const void *n2);
int compare2(const void *n1, const void *n2);

int main()
{

    int index = 100;
    numData s_data[index];

    //generate random
    srand(time(NULL));
    for (int i = 0; i < 100; i++)
    {
        s_data[i].int_num = rand() % 100;                         //int
        s_data[i].double_num = (double)rand() / (RAND_MAX / 100); //double
    }

    //before sort print
    printf("before sort >>\n");
    myprint(&s_data, index);

    //sort(function)
    mysort2(&s_data[0].int_num, index, sizeof(s_data[0].int_num), compare);
    mysort2(&s_data[0].double_num, index, sizeof(s_data[0].double_num), compare2);

    //after sort print
    printf("\nafter sort >>\n");
    myprint(&s_data, index);

    return 0;
}

//for int
int compare(const void *n1, const void *n2)
{
    if (((numData2 *)n1)->no > ((numData2 *)n2)->no)
        return 1;
    else
        return 0;
}

//for double
int compare2(const void *n1, const void *n2)
{
    if (((numData2 *)n1)->no2 > ((numData2 *)n2)->no2)
        return 1;
    else
        return 0;
}

int mysort2(void *data, int num, int size, int (*compare)(const void *, const void *))
{
    int i, j;
    int min;
    unsigned char *tmp;

    //for swap
    tmp = (unsigned char *)malloc(sizeof(unsigned char) * size);
    if (tmp == NULL)
        return -1;

    //sort
    for (i = 0; i < num; i++)
    {
        min = i;
        for (j = i + 1; j < num; j++)
        {
            if (compare((unsigned char *)data + size * min, (unsigned char *)data + size * j) == 1)
            {
                min = j;
            }
        }
        //swap
        memcpy(tmp, (unsigned char *)data + size * min, size);
        memcpy((unsigned char *)data + size * min, (unsigned char *)data + size * i, size);
        memcpy((unsigned char *)data + size * i, tmp, size);
    }
    free(tmp);
    return 0;
}

void myprint(numData *data, int index)
{
    // int
    printf("(int)\n");
    for (int i = 1; i <= 100; i++)
    {
        printf("%d ", data[i - 1].int_num);
        if (i % 20 == 0)
            printf("\n");
    }

    // double
    printf("\n(double)\n");
    for (int i = 1; i <= 100; i++)
    {
        printf("%f ", data[i - 1].double_num);
        if (i % 5 == 0)
            printf("\n");
    }
}