typedef struct Node{ int val; struct Node *next; }Node, *NodePtr; void bubbleSort(NodePtr head) { int i,j,tmp; bool flag; for(i = 0; i < len-1 ;i++) { NodePtr curr = head; NodePtr next = head->next; flag = false; for(j=0; j < len-i-1 ;j++) { if(curr->val > next->val ) { tmp = curr->val; curr->val = next->val; next->val = tmp; flag = true; } } if(flag == false) break; // 沒有結點需要swap } }
[Interview] Bubble sort using singly-linked list
原文:http://www.cnblogs.com/bittorrent/p/4736662.html