Sorting algorithm to sort the list ---

Foreword

  Similar sort the list of ideas and sort the array, the difference is easy to traverse an array, data exchange is easy; the list (single list) can only be traversed in one direction, not reverse traversal (Alternatively, you can reverse the traversal), random access and can not, therefore ordering more trouble, while the list of data exchange is very troublesome, if the two switching nodes, the need involving three nodes, potentially increasing complexity, can directly exchange data node, this embodiment is relatively simple.

  The following lists several relatively good understanding of simple Ye Hao list sorting algorithm, code is as follows:

  1 #include <iostream>
  2 
  3 using namespace std;
  4 
  5 struct Node
  6 {
  7     struct Node *next;
  8     int data;
  9 };
 10 
 11 Node *createNode(int x)
 12 {
 13     Node *p = NULL;
 14     
 15     p = new Node;
 16     p->next = NULL;
 17     p->data = x;
 18     
 19     return p;
 20 }
 21 
 22 void addNode(Node **head, Node *p)
 23 {
 24     Node *temp = NULL;
 25     if(*head == NULL)
 26     {
 27         *head = p;
 28     }
 29     else
 30     {
 31         temp = *head;
 32         while(temp->next != NULL)
 33         {
 34             temp = temp->next;
 35         }
 36         temp->next = p;
 37     }
 38 }
 39 
 40 void showList(Node *head)
 41 {
 42     while(head != NULL)
 43     {
 44         cout << head->data << "  ";
 45         head = head->next;
 46     }
 47     cout << endl;
 48 }
 49 
 50 void swap(int& a, int& b)
 51 {
 52     int tmp = B;
 53 is      B = A;
 54 is      A = tmp;
 55  }
 56 is  
57 is  // 1. directly exchange data directly select the sort ------ 
58  void ListSort_1 (the Node ** head)
 59  {
 60      the Node * P = NULL;
 61 is      the Node Q * = NULL;
 62 is      the Node T * = NULL;
 63 is      
64      IF (* == NULL || head (head *) -> Next == NULL)
 65      {
 66          return ;
 67      }
 68  
69     for(p = *head; p != NULL; p = p->next)
 70     {
 71         t = p;
 72         
 73         for(q = p->next; q != NULL; q = q->next)
 74         {
 75             if(q->data < t->data)
 76             {
 77                 t = q;
 78             }
 79         }
 80         
 81         if(t != p)
 82         {
 83             swap(p->data, t->data);
 84         }
 85     }
 86      
87      return ;
 88  }
 89  
90  // 2. Bubble Sort exchange data directly ------ 
91 is  void ListSort_2 (the Node ** head)
 92  {
 93      the Node * P = NULL;
 94      the Node Q * = NULL;
 95      the Node T * = NULL;
 96      
97      IF (* == NULL || head (head *) -> Next == NULL)
 98      {
 99          return ;
 100      }
 101  
102      for ! (* P = head; P = NULL; = p-P> Next)
103      {
 104          for (Q = * head; Q-> Next = NULL;! Q = Q-> Next)
 105          {
 106              IF (Q-> Data> Q-> next-> Data)
 107              {
 108                  the swap (Q- > Data, Q-> next-> Data);
 109              }
 110          }
 111          
112      }
 113  }
 114  
115  // 3. insertion sort 
1 16  void ListSort_3 (the Node ** head)
 117  {
 1 18      // inserted directly related to sort the list reverse traversal, much trouble is not recommended 
119  }
 120 
121 Node *merge(Node *left, Node *right)
122 {
123     Node *head = createNode(-1);  // 小技巧
124     Node *temp = head;
125     
126     while(left != NULL && right != NULL)
127     {
128         if(left->data < right->data)
129         {
130             temp->next = left;
131             temp = temp->next;
132             left = left->next;
133         }
134         else
135         {
136             temp->next = right;
137             temp = temp->next;
138             right = right->next;
139         }
140     }
141     
142     if(left != NULL)
143     {
144         temp->next = left;
145     }
146     
147     if(right != NULL)
148     {
149         temp->next = right;
150     }
151     
152     return head->next;
153 }
154 
155 Node *_ListSort_4(Node *head)
156 {
157     Node *pFast = NULL;
158     Node *pSlow = NULL;
159     Node *mid = NULL;
160     
161     if(head == NULL || head->next == NULL)
162     {
163         return head;
164     }
165     
166     pFast = head;
167     pSlow = head;
168     
169     while(pFast->next != NULL && pFast->next->next != NULL)
170     {
171         pFast = pFast->next->next;
172         pSlow = pSlow->next;
173     }
174     
175     mid = pSlow->next;
176     pSlow->next = NULL;
177     
178     return merge(_ListSort_4(head), _ListSort_4(mid));
179 }
180 
181 // 4. 归并排序      ------交换节点
182 void ListSort_4(Node **head)
183 {
184     Node *temp = *head;
185     *head = _ListSort_4(temp);
186 }
187 
188 // 链表反转
189 Node *reverse(Node *head)
190 {
191     Node *pCur = NULL;
192     Node *pNext = NULL;
193     
194     if(head == NULL || head->next == NULL)
195     {
196         return head;
197     }
198     
199     pCur = head->next;
200     head->next = NULL;
201     
202     while(pCur != NULL)
203     {
204         pNext = pCur->next;
205         pCur->next = head;
206         head = pCur;
207         pCur = pNext;
208     }
209     
210     return head;
211 }
212 
213 // 链表是否成环
214 bool isCirCle(Node *head)
215 {
216     bool bIsCirCle = false;
217     Node *pFast = NULL;
218     Node *pSlow = NULL;
219     
220     if(head == NULL || head->next == NULL)
221     {
222         return bIsCirCle;
223     }
224     
225     pFast = head;
226     pSlow = head;
227     while(pFast->next != NULL && pFast->next->next != NULL)
228     {
229         pFast = pFast->next->next;
230         pSlow = pSlow->next;
231         
232         if(pFast == pSlow)
233         {
234             bIsCirCle = true;
235             break;
236         }
237     }
238     
239     return bIsCirCle;
240 }
241 
242 int main()
243 {
244     Node *head = NULL;
245     
246     addNode(&head, createNode(2));
247     addNode(&head, createNode(5));
248     addNode(&head, createNode(7));
249     addNode(&head, createNode(4));
250     addNode(&head, createNode(6));
251     addNode(&head, createNode(3));
252     addNode(&head, createNode(1));
253     addNode(&head, createNode(9));
254     addNode(&head, createNode(8));
255 
256     cout << "Sort Before:" << endl;
257     showList(head);
258     
259     //ListSort_1(&head);
260     //ListSort_2(&head);
261     ListSort_4(&head);
262     
263     cout << "Sort After:" << endl;
264     showList(head);
265     
266     cout << "revert:" << endl;
267     showList(reverse(head));
268     
269     cout << "list is circle: " << isCirCle(head) << endl;
270 
271     while(1);
272     return 0;
273 }

 

Guess you like

Origin www.cnblogs.com/chusiyong/p/11323354.html