Sorting Algorithm ---- insert sorting with no table header linked list, algorithm ----
- /*
- ======================================
- Function: sort by insert directly (from small to large)
- Return: pointer to the table header of the linked list.
- ======================================
- */
- /*
- The basic idea of directly inserting sorting is to assume that n-1-1 nodes in the linked list are already key values.
- (That is, the field sorted with it, we take the student number num as the key value) sort the order, for node n in
- Find the insert position in the sequence so that the new sequence is still ordered after n is inserted. According to this idea
- You can execute the chain table from start to end to change the unordered chain table to an ordered chain table.
- The following figure shows the direct insertion and sorting of a one-way linked list:
- ----> [1] ----> [3] ----> [2]... ----> [n] ----> [NULL] (original linked list)
- Head 1-> next 3-> next 2-> next n-> next
- ----> [1] ----> [NULL] (retrieve 1st nodes from the original linked list as an ordered linked list with only one node)
- Head
- Figure 11
- ----> [3] ----> [2]... ----> [n] ----> [NULL] (the original linked list contains the nodes used for direct insertion and sorting)
- First 3-> next 2-> next n-> next
- Figure 12
- ----> [1] ----> [2] ----> [3]... ----> [n] ----> [NULL] (sorted linked list)
- Head 1-> next 2-> next 3-> next n-> next
- Figure 13: Direct insertion and sorting of linked lists with N nodes
- 1. First, use the first node as an ordered linked list in the original linked list, and the remaining nodes as undetermined nodes.
- 2. Fetch nodes from the linked list in Figure 12 and locate the insertion in the linked list in Figure 11.
- 3. Although two linked lists are drawn in the figure above, there is actually only one linked list. In sorting, only one header pointer first is added to point to the remaining nodes.
- Please be sure to clarify this point, or you may think it is the same as the preceding sorting method.
- */
- Struct student * InsertSort (struct student * head)
- {
- Struct student * first;/* left the node header pointer for direct insertion of sorting in the original linked list */
- Struct student * t;/* Temporary pointer variable: insert node */
- Struct student * p;/* Temporary pointer variable */
- Struct student * q;/* Temporary pointer variable */
- First = head-> next;/* The original linked list is left with the node linked list for direct insertion and sorting: it can be understood according to Figure 12. */
- Head-> next = NULL;/* ordered linked list containing only one node: see Figure 11. */
- While (first! = NULL)/* traverse the unordered linked list */
- {
- /* Note: here the for statement is the place where the sort idea is inserted directly */
- For (t = first, q = head; (q! = NULL) & (q-> num <t-> num); p = q, q = q-> next ); /* unordered nodes are inserted in the ordered linked list */
- /* Exit the for loop, that is, locate the insert position */
- /* Note: In principle, this sentence can be placed in the position annotated below, but it cannot. Cause: If you understand the above 3rd items, you will know. */
- First = first-> next;/* the node in the unordered linked list leaves, so that it is inserted into the ordered linked list. */
- If (q = head)/* inserted before the first node */
- {
- Head = t;
- }
- Else/* p is the precursor of q */
- {
- P-> next = t;
- }
- T-> next = q;/* insert operation completed */
- /* First = first-> next ;*/
- }
- Return head;
- }