c++ 链表排序

node *bubbleSortList(node *head)
{
    if (head == NULL || head->next == NULL) return head;
    node *p = NULL;
    bool isChange = true;
    while (p != head->next->next && isChange)
    {
        node *q = head->next;
        isChange = false; //标志当前这一轮中又没有发生元素交换,如果没有则表示数组已经有序
        for (; q->next && q->next != p; q = q->next)
            if (q->data > q->next->data)
                swap(q->data, q->next->data), isChange = true;
        p = q;
    }
    return head;
}
发布了30 篇原创文章 · 获赞 43 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/qq_43041976/article/details/90113640
今日推荐