M
Mariano
I have a structure with a numeric field and a pointer to char:
typedef struct s
{
int numero;
char *carattere;
} strutt;
I have created, then, an array of structure:
strutt *arr;
arr = (strutt *)malloc(32 * sizeof(strutt));
I can insert a new element as:
arr[0].numero = 5;
arr[0].carattere = "qwerty"
When this dynamic array of structure will be full, it can be necessary
to sort it. I need to sort it with insertion sort algorithm, this is
how I have implemented:
void insertion_sort(strutt *x, int length)
{
strutt key;
int i,j=1,scambi=0;
for(j=1;j<length;j++)
{
key = x[j];
i=j-1;
while(x.numero>key.numero && i>=0)
{
x[i+1] = x;
i--;
}
x[i+1] = key;
}
}
This function works perfectly if I need to sort integer number (order
by field "numero"). But what if I need to modify this algorithm to
make it working with "Carattere" field???
typedef struct s
{
int numero;
char *carattere;
} strutt;
I have created, then, an array of structure:
strutt *arr;
arr = (strutt *)malloc(32 * sizeof(strutt));
I can insert a new element as:
arr[0].numero = 5;
arr[0].carattere = "qwerty"
When this dynamic array of structure will be full, it can be necessary
to sort it. I need to sort it with insertion sort algorithm, this is
how I have implemented:
void insertion_sort(strutt *x, int length)
{
strutt key;
int i,j=1,scambi=0;
for(j=1;j<length;j++)
{
key = x[j];
i=j-1;
while(x.numero>key.numero && i>=0)
{
x[i+1] = x;
i--;
}
x[i+1] = key;
}
}
This function works perfectly if I need to sort integer number (order
by field "numero"). But what if I need to modify this algorithm to
make it working with "Carattere" field???