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
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);
}
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
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).