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
		if(*head->num<*(head->nextptr)->num{//Circularly compares the sizes of two adjacent ones, sorted from large to small
			*temp=*(head->nextptr);//Store the second pointed needle
			*(head->nextptr)=*(head->nextptr->nextptr);//Connect the third finger to the back of the first finger
			*temp->nextptr=*head;//Place the first finger behind the second
			if(num>0)//It is convenient to separate the null pointer in pre
			*pre=*temp;//Move pre, one bit backward
			if(num==0)//If it is a head pointer, it will change the head PTR, the head pointer of the linked list
		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
		*head=*headptr;//Change head to headptr chain header again
return *headptr;//Return to the ordered list

Second kinds

struct node *bubblesort(struct node *head)//Chain list with empty first node
	struct node *pre,p,*tail,*headptr=*head;;
	*tail=NULL;//Loop variable
	while(*headptr!=NULL)//Control cycle variable
		*pre=*headptr;//Assignment of front pointer
			if(p->num<p->nextptr->num){//The former is smaller than the latter
		*tail=*p;//Move the end pointer one bit forward
return *headptr;//Return to the ordered list

Posted by psurrena on Sun, 01 Dec 2019 08:09:59 -0800