Bubble algorithm (implemented in C)

Source: Internet
Author: User

Under the book upload directoryCode.

 

# Include <studio. h>/* header file */<br/>/* Custom Data Type node, indicates the data to be sorted */<br/> typedef struct node <br/> {<br/> int data; <br/> struct node * next; // pointer of the next node <br/> struct node * Prev; // pointer of the previous node <br/>} node; <br/>/* Insert a node to the head of the two-way linked list */<br/> insertlt (node * head, node * node) <br/> {<br/>/* The linked list is empty, with the node to be inserted as the header node */<br/> If (* head = NULL) /* First node */<br/> {<br/> (* head) = node; /* use the new node as the linked list header */<br/> (* head)-> next = NULL;/* Leave the node blank * /<Br/> (* head)-> pre = NULL;/* The forward node is empty */<br/>} else {/* The linked list is not empty, insert the newly added node as the header node into the head of the linked list */<br/> node-> next = * head; /* Insert the head of the new node into the linked list */<br/> (* head)-> pre = node; <br/> (* head) = node; <br/> (* head)-> pre = NULL; <br/>}< br/>/* A Bubble Sorting function, compare from the first node of the linked list to the end of the node */<br/> perbubble (node * head, node * node) <br/> {<br/> node * TMP, * P; // temporary variable definition <br/> int N; <br/> for (TMP = head; TMP! = Node; TMP = TMP-> next) // compare the values from the first node, to the end of a node <br/>{< br/> If (TMP-> next = NULL) break; <br/>/* if the current value is greater than the value of a subsequent node, exchange two nodes */<br/> If (TMP-> DATA> TMP-> next-> data) <br/>{< br/> N = TMP-> data; <br/> TMP-> DATA = TMP-> next-> data; <br/> TMP-> next-> DATA = N; <br/>}< br/>/* bubble sort <SPAN class = 'wp _ keywordlink '> algorithm </span> function head list list to be sorted */<br/> bubblesort (node * head) <br/> {<br/> node * TMP = NULL <br/> for (TMP = head; TMP-> next! = NULL;) // find the last node of the two-way linked list <br/> TMP = TMP-> next; <br/>/* starting from the last node, until the first element, perform a sort process */<br/> for (; TMP! = NULL; TMP = TMP-> pre) <br/>{< br/> perbubble (Head, TMP ); // call the sorting function <br/>}< br/> main () <br/>{< br/> int N; /* loop variable definition */<br/> node * head = NULL;/* The head pointer of the linked list. The Initialization is empty */<br/> node * TMP, * P; /* pointer variable, temporarily saving the pointer of node variable */<br/> for (;)/* cyclically calling the data input from the keyboard, based on the input data resume two-way linked list, enter a negative number to exit the loop */<br/> {<br/> scanf ("% d", & N ); // input an integer from the standard input device <br/> If (n <0) break; <br/> TMP = (node *) malloc (sizeof (node )); // dynamically applied memory <br/> memset (TMP, 0x00, sizeo F (TMP); // clear the applied memory <br/> TMP-> DATA = N; // Save the input value <br/> TMP-> next = NULL; <br/> TMP-> pre = NULL; <br/> insertlt (& head; TMP); <br/>}< br/> bubblesort (head ); <br/> for (TMP = head; TMP! = NULL; // cyclically outputs the sorted result and releases the dynamically applied memory variable <br/>{< br/> printf ("% d/N ", TMP-> data); <br/> P = TMP; <br/> TMP = TMP-> next; // jump to the next node <br/> P-> next = NULL; <br/> free (P ); // release memory <br/>}< br/>} 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.