# 单链表的插入排序、冒泡排序和选择排序（交换指针）

/*  insertion_sort_sll函数：用插入排序对单链表进行排序  */
void insertion_sort_sll(struct nlist **nextp)
{
struct nlist **pre;
struct nlist *new;
struct nlist *key;

if (*nextp == NULL)
return;

key = (*nextp)->next;
(*nextp)->next = NULL;
while (key != NULL) {
pre = nextp;
while (*pre != NULL && (*pre)->val < key->val)
pre = &(*pre)->next;
new = key->next;
key->next = *pre;
*pre = key;
key = new;
}
}

/*  bubble_sort_sll函数：用冒泡排序对单链表进行排序  */
void bubble_sort_sll(struct nlist **nextp)
{
struct nlist **pre;
struct nlist **cur;
struct nlist *p;      /*  两个临时指针  */
struct nlist *q;
enum {NO, YES};
int exchange = YES;

while (exchange == YES) {
cur = nextp;
exchange = NO;
while (*cur != NULL && (*cur)->next != NULL)
if ((*cur)->val > (*cur)->next->val) {
exchange = YES;
pre = &(*cur)->next->next;
p = *cur;
q = *pre;
*pre = *cur;
*cur = (*cur)->next;
p->next = q;
cur = pre;
} else
cur = &(*cur)->next;
}
}

/*  selection_sort_sll函数：用选择排序对单链表进行排序  */
void selection_sort_sll(struct nlist **nextp)
{
struct nlist **cur;
struct nlist **p_min;

for ( ; *cur != NULL; cur = &(*cur)->next)
if ((*cur)->val < (*p_min)->val)
p_min = cur;
*nextp = *p_min;
*p_min = (*p_min)->next;
(*nextp)->next = NULL;
}
}


• 本文已收录于以下专栏：

举报原因： 您举报文章：单链表的插入排序、冒泡排序和选择排序（交换指针） 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)