HELP... with a C programme

I’m trying to use insertion sort on a pointer structure and I’m working with a very simple structure with only one element, but I’m kinda following the logic of an insertion sort using variables like cha or int… so it isn’t really working well… Could someone help me pls

structure:

typedef struct REGISTO{      //registo=register
    char nome[20];    //nome=name
}AGENDA;

code:

void INSERTION_SORT(AGENDA **PESSOAS, int length){   //pessoas=people
AGENDA *prekey;
    AGENDA *key;      //agenda=phonebook
    int i, j;

    prekey = NULL;
    for (i = 1; i < length; i++){
        key = *(PESSOAS + i);
        j = i;
    }
    prekey = *(PESSOAS + j - 1);
    while (j > 0 && prekey->nome > key->nome){
        **(PESSOAS + j) = **(PESSOAS + j - 1);
        j--;
    }
    **(PESSOAS + j) = key;
}

main

 void main(){
int length = 50;
INSERTION_SORT(PESSOAS, length);
free(PESSOAS);
}

I have more stuff to the programme but this is the only part I’m developing more rn so as not to make it confusing I’m only showing this part.

Please don’t index into arrays like this. It is archaic and makes it harder to read the code.

Using a variable name in all caps is also a confusing violation of conventional behaviors.

Function names also should not be all caps.

Same thing with typedefs. All caps is for global constants and macros only.

Were these loops supposed to be nested?

It is hard to be sure with the archaic indexing, but I don’t thing this actually swaps the pointers?

This isn’t how you compare strings.

Thank you for the tips. Actually i wrote some things in caps so it would help me when looking at the code but forgot that it could be confusing to post it here without changing first.

Here is the code in another way… I’m trying to make selection sort instead now. but what can i do to make it work?

typedef struct Registo{
    char nome[20];
}Agenda;

void Selection_Sort(Agenda *Pessoas, int length){           //!!!!!!!!!!!!!!!

    Agenda key;
    int i, j, res;

    for(i = 0; i < length; i++){
    for(j = 0; j < length; j++){
    res = strcmp(Pessoas[i].nome, Pessoas[j].nome);
    if(res < 0){
    key = Pessoas[i];
    Pessoas[i] = Pessoas[j];
    Pessoas[j] = key;
}
}
}
}

void main(){
    int length = 50;
    int i=0;
    Selection_Sort(Pessoas, length);
    free(Pessoas);

}

Indentation helps others read the code.

Are you actually creating the input array somewhere?

Your start value for the j index doesn’t match the selection sort.

Are you working from some description of these sorting algorithms?

I’m trying to make this work with a pointer structure… I’m not very familiar with the logic yet…

I’m not sure what you mean by ‘pointer structure’. A pointer is just the address of some memory location on your computer where some data lives.

What I mean is just that instead of for example printing Pessoas.nome, I do it like Pessoas->nome and I think that affects how I create the rest of the function… since i have *Pessoas… I’m not sure how it changes the way i should write the fuction tho

The compiler should be giving you lots of hints as to if you want dot or arrow.

It kind of looks like you are doing a bubble sort, not a selection sort? (Bubble sort is when you compare two values and switch them around if they are not in the relevant sort order).
Selection sort is when you search for the smallest item and “select it” to put it in the first location, then ignore that item to redo the steps for selection over the remaining items.

In both sorts you have to repeatedly read through the list over and over to gradually sort it. (For selection sort you would do subsequent iterations over the unworried portion only).