Sorting Algorithm ---- insert sorting with no table header linked list, algorithm ----

Source: Internet
Author: User

Sorting Algorithm ---- insert sorting with no table header linked list, algorithm ----

  1. /*
  2. ======================================
  3. Function: sort by insert directly (from small to large)
  4. Return: pointer to the table header of the linked list.
  5. ======================================
  6. */
  7. /*
  8. The basic idea of directly inserting sorting is to assume that n-1-1 nodes in the linked list are already key values.
  9. (That is, the field sorted with it, we take the student number num as the key value) sort the order, for node n in
  10. Find the insert position in the sequence so that the new sequence is still ordered after n is inserted. According to this idea
  11. You can execute the chain table from start to end to change the unordered chain table to an ordered chain table.
  12. The following figure shows the direct insertion and sorting of a one-way linked list:
  13. ----> [1] ----> [3] ----> [2]... ----> [n] ----> [NULL] (original linked list)
  14. Head 1-> next 3-> next 2-> next n-> next
  15. ----> [1] ----> [NULL] (retrieve 1st nodes from the original linked list as an ordered linked list with only one node)
  16. Head
  17. Figure 11
  18. ----> [3] ----> [2]... ----> [n] ----> [NULL] (the original linked list contains the nodes used for direct insertion and sorting)
  19. First 3-> next 2-> next n-> next
  20. Figure 12
  21. ----> [1] ----> [2] ----> [3]... ----> [n] ----> [NULL] (sorted linked list)
  22. Head 1-> next 2-> next 3-> next n-> next
  23. Figure 13: Direct insertion and sorting of linked lists with N nodes
  24. 1. First, use the first node as an ordered linked list in the original linked list, and the remaining nodes as undetermined nodes.
  25. 2. Fetch nodes from the linked list in Figure 12 and locate the insertion in the linked list in Figure 11.
  26. 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.
  27. Please be sure to clarify this point, or you may think it is the same as the preceding sorting method.
  28. */
  29. Struct student * InsertSort (struct student * head)
  30. {
  31. Struct student * first;/* left the node header pointer for direct insertion of sorting in the original linked list */
  32. Struct student * t;/* Temporary pointer variable: insert node */
  33. Struct student * p;/* Temporary pointer variable */
  34. Struct student * q;/* Temporary pointer variable */
  35. 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. */
  36. Head-> next = NULL;/* ordered linked list containing only one node: see Figure 11. */
  37. While (first! = NULL)/* traverse the unordered linked list */
  38. {
  39. /* Note: here the for statement is the place where the sort idea is inserted directly */
  40. 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 */
  41. /* Exit the for loop, that is, locate the insert position */
  42. /* 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. */
  43. First = first-> next;/* the node in the unordered linked list leaves, so that it is inserted into the ordered linked list. */
  44. If (q = head)/* inserted before the first node */
  45. {
  46. Head = t;
  47. }
  48. Else/* p is the precursor of q */
  49. {
  50. P-> next = t;
  51. }
  52. T-> next = q;/* insert operation completed */
  53. /* First = first-> next ;*/
  54. }
  55. Return head;
  56. }

Related Article

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.