# Summary of bubble sorting of linked list (two methods, the second is simpler)

In the last semester of freshman year, we learned bubble sorting, selection sorting, insertion sorting and so on. Today we mainly explain bubble sorting of linked list. (reading code and explaining)
First, let's review what bubble sorting is. Bubble sorting is a kind of sorting with time complexity under the square of n; each cycle compares the two items before and after, either sorting from small to large, or sorting from large to small.

The first is:

``` struct node *bubblesort(struct node *headptr)//Accept the head pointer, the beginning of the list, and the list whose head node is not empty
struct node *pre=NULL,*flag=NULL,*head=*headptr,*temp;//pre represents the previous pointer, and flag is used as the loop variable, which is very important
int num;//Loop variable
{
num=0;
*temp->nextptr=*head;//Place the first finger behind the second
if(num>0)//It is convenient to separate the null pointer in pre
*pre->nextptr=*temp;
*pre=*temp;//Move pre, one bit backward
}
else{//If the size relationship is not satisfied. Move both to one place
}
num++；//Convenience of pre
}
*flag=*head;take flag Move forward one bit at a time to minimize the number of times
}

```

Second kinds

```struct node *bubblesort(struct node *head)//Chain list with empty first node
{
*tail=NULL;//Loop variable
{
while(*(p->nextptr)!=NULL)
{
if(p->num<p->nextptr->num){//The former is smaller than the latter
pre->nextptr=p->nextptr;
p->nextptr=p->nextptr->nextptr;
pre->nextptr->nextptr=p;
}
else{
*p=*(p->nextptr);
*pre=*(pre->nextptr);
}
}
*tail=*p;//Move the end pointer one bit forward
}