链表简单排序
链表排序
1、直接选择排序
1.1 排序流程:
1.2 代码实现
void list_sort(LinkList* list)
{
LinkNode * p = NULL;
LinkNode* q = NULL;
LinkNode* t = NULL;
for (p = list->head; p != NULL; p = p->next) //遍历链表
{
t = p; //记录头指针
for (q = p->next; q != NULL; q = q->next)
{
if (q->data < t->data)
{
t = q;
}
}
if (t != p)
{
swap(p->data, t->data);
}
}
}
2、冒泡排序
2.1 排序流程
冒泡排序思想
2.2 代码实现
//链表排序:冒泡排序
void list_Bubble_sort(LinkList* list)
{
LinkNode* p = NULL;
LinkNode* q = NULL;
for (p = list->head; p != NULL; p = p->next)
{
for (q = list->head; q->next != NULL; q = q->next)
{
if (q->data > q->next->data)
{
swap(q->data, q->next->data);
}
}
}
}