I'm trying to implement the InsertSort method but so far unsuccessful .. Could someone give me that strength?
void insertionSort(ListDN *l){
int ordered = 0,i,j;
Dnode *cursor = l->head->next ;
Dnode *aux;
Dnode *temp;
while(!ordered){
ordered= 1;
printf("%d cursor \n", cursor->info);
printf("%d cursor->prev \n", cursor->prev->info);
temp = cursor;
while(cursor->prev != NULL && cursor->info < cursor->prev->info){
troca(l, cursor->prev, cursor);
}
ordered= 0;
cursor=temp->next;
toList(l);
}
}
Exchange method ...
void troca(ListDN *list, Dnode *n1, Dnode *n2 ){
Dnode *n1a = n1->prev;
Dnode *n1p = n1->next;
Dnode *n2a = n2->prev;
Dnode *n2p = n2->next;
if(n1->next == n2){
n1p = n2;
n2a = n1;
if(n1a != NULL){
n1a->next = n2;
}else{
list->head = n2;
}
n2->prev = n1a;
n1->prev = n2;
n2->next = n1;
n1->next = n2p;
if(n2p != NULL){
n2p->prev = n1;
}else{
list->tail = n1;
}
}else{
if(n1a !=NULL){
n1a->next = n2;
}else{
list->head = n2;
}
n2->prev = n1a;
n1->prev = n2a;
if(n2p != NULL){
n2p->prev = n1;
}else{
list->tail = n1;
}
if(n1p != NULL){
n1p->prev = n2;
}
if(n2a != NULL){
n2a->next = n1;
}
n1->next = n2p;
n2->next = n1p;
}
}
If anyone can help please ...