关于链表的插入排序
#include <iostream.h>
#include <stdlib.h>
/* InsertonSort: sort liked list by the insertion sort method.
PRE: The linked list list had been created.Each entry of list contains a key
POST: The entries of the list have been rearranged so that the keys in all the entries are sorted into nondecreasing order.*/
void InsertionSort(List* list)
{
ListNode *fu; /*the first unsorted node to be inserted */
ListNode *Is; /*the last sorted node (tail of sorted sublists) */
ListNode *current,*trailing;
if(list->head){
Is = list->head; /*An empty list is already sorted */
while(Is->next) {
fu=Is->next; /*Remember first unsorted node */
if(LT(fu->entry.key,list->head->entry.key)){
Is->next=fu->next;
fu->next=list->head;
list->head=fu; /*Insert first unsorted at the head of sorted list*/
}else{
trailing=list->head;
for(current=trailing->next; GT(fu->entry.key,current->entry.key);current=current->next)
trailing=current;
/*First unsorted node now belongs between trailing and current */
if(fu== current)
Is=fu;
else{
Is->next=fu->next;
fu->next=current;
trailing->next=fu;
}
}
}
}
}
这是用链表实现插入排序的过程,但是看不清这个循环最后是什么样的变化,有人能分析下这个链表在排序过程中如何变化吗?GT和LT分别是宏代表GREATER THAN 和LESS THAN