


This article introduces in detail the JavaScript implementation of linked list insertion sort and linked list merge sort. Linked list merge sort is to merge and sort each part and then merge them together.
1. Linked list
1.1 Storage representation of linked list
//链表的存储表示 typedef int ElemType; typedef struct LNode { ElemType data; struct LNode *next; }LNode, *LinkList;
1.2 Basic operation
Create linked list:
/* * 创建链表。 * 形参num为链表的长度,函数返回链表的头指针。 */ LinkList CreatLink(int num) { int i, data; //p指向当前链表中最后一个结点,q指向准备插入的结点。 LinkList head = NULL, p = NULL, q; for (i = 0; i < num; i++) { scanf("%d", &data); q = (LinkList)malloc(sizeof(LNode)); q->data = data; q->next = NULL; if (i == 0) { head = q; } else { p->next = q; } p = q; } return head; }
Output linked list:
/* * 输出链表结点值。 */ int PrintLink(LinkList head) { LinkList p; for (p = head; p ;p = p->next) { printf("%-3d ", p->data); } return 0; }
2. Linked list insertion sort
Basic idea: Assume that the first n-1 nodes are in order, insert the nth node Go to the appropriate position of the previous node and order these n nodes.
Implementation method:
Remove the first node on the linked list to become a linked list containing one node (head1), and the remaining nodes will naturally become another A linked list (head2), at this time head1 is an ordered linked list containing
one node;
Remove the first node from the linked list head2 and insert it into the linked list head1 At the appropriate position, head1 is still in order. At this time, head1 becomes an ordered linked list containing two nodes;
removes one node from the linked list head2 in turn and inserts it into the linked list head1. Until the linked list head2 is an empty linked list. Finally, the linked list head1 contains all nodes, and the nodes are in order.
Insertion sort code:
/* * 链表插入排序(由小到大)。 * 输入:链表的头指针, * 输出:排序后链表的头指针。 * 实现方法:将原链表拆成两部分:链表1仍以head为头指针,链表结点有序。链表2以head2为头指针,链表结点无序。 * 将链表2中的结点依次插入到链表1中,并保持链表1有序。 * 最后链表1中包含所有结点,且有序。 */ LinkList LinkInsertSort(LinkList head) { //current指向当前待插入的结点。 LinkList head2, current, p, q; if (head == NULL) return head; //第一次拆分。 head2 = head->next; head->next = NULL; while (head2) { current = head2; head2 = head2->next; //寻找插入位置,插入位置为结点p和q中间。 for (p = NULL, q = head; q && q->data <= current->data; p = q, q = q->next); if (q == head) { //将current插入最前面。 head = current; } else { p->next = current; } current->next = q; } return head; }
Complete source code:
/* * 链表插入排序,由小到大 */ #define _CRT_SECURE_NO_WARNINGS #include#include #define TOTAL 10 //链表长度 //链表的存储表示 typedef int ElemType; typedef struct LNode { ElemType data; struct LNode *next; }LNode, *LinkList; LinkList CreatLink(int num); LinkList LinkInsertSort(LinkList head); int PrintLink(LinkList head); /* * 创建链表。 * 形参num为链表的长度,函数返回链表的头指针。 */ LinkList CreatLink(int num) { int i, data; //p指向当前链表中最后一个结点,q指向准备插入的结点。 LinkList head = NULL, p = NULL, q; for (i = 0; i < num; i++) { scanf("%d", &data); q = (LinkList)malloc(sizeof(LNode)); q->data = data; q->next = NULL; if (i == 0) { head = q; } else { p->next = q; } p = q; } return head; } /* * 链表插入排序(由小到大)。 * 输入:链表的头指针, * 输出:排序后链表的头指针。 * 实现方法:将原链表拆成两部分:链表1仍以head为头指针,链表结点有序。链表2以head2为头指针,链表结点无序。 * 将链表2中的结点依次插入到链表1中,并保持链表1有序。 * 最后链表1中包含所有结点,且有序。 */ LinkList LinkInsertSort(LinkList head) { //current指向当前待插入的结点。 LinkList head2, current, p, q; if (head == NULL) return head; //第一次拆分。 head2 = head->next; head->next = NULL; while (head2) { current = head2; head2 = head2->next; //寻找插入位置,插入位置为结点p和q中间。 for (p = NULL, q = head; q && q->data <= current->data; p = q, q = q->next); if (q == head) { //将current插入最前面。 head = current; } else { p->next = current; } current->next = q; } return head; } /* * 输出链表结点值。 */ int PrintLink(LinkList head) { LinkList p; for (p = head; p ;p = p->next) { printf("%-3d ", p->data); } return 0; } int main() { LinkList head; printf("输入Total个数以创建链表:\n"); head = CreatLink(TOTAL); head = LinkInsertSort(head); printf("排序后:\n"); PrintLink(head); putchar('\n'); return 0; }
3. Linked list merge sort
Basic idea: If the linked list If it is empty or contains a node, the linked list is naturally ordered. Otherwise, split the linked list into two parts, merge sort each part separately, and then merge the two sorted linked lists together.
Merge sort code:
/* * 链表归并排序(由小到大)。 * 输入:链表的头指针, * 输出:排序后链表的头指针。 * 递归实现方法:将链表head分为两部分,分别进行归并排序,再将排序后的两部分归并在一起。 * 递归结束条件:进行递归排序的链表为空或者只有一个结点。 */ LinkList LinkMergeSort(LinkList head) { LinkList head1, head2; if (head == NULL || head->next == NULL) return head; LinkSplit(head, &head1, &head2); head1 = LinkMergeSort(head1); head2 = LinkMergeSort(head2); head = LinkMerge(head1, head2); return head; }
The linked list split function is as follows. The basic idea is to use slow/fast pointers. See the comments for the specific implementation method.
/* * 链表分割函数。 * 将链表head均分为两部分head1和head2,若链表长度为奇数,多出的结点从属于第一部分。 * 实现方法:首先使指针slow/fast指向链首, * 然后使fast指针向前移动两个结点的同时,slow指针向前移动一个结点, * 循环移动,直至fast指针指向链尾。结束时,slow指向链表head1的链尾。 */ int LinkSplit(LinkList head, LinkList *head1, LinkList *head2) { LinkList slow, fast; if (head == NULL || head->next == NULL) { *head1 = head; *head2 = NULL; return 0; } slow = head; fast = head->next; while (fast) { fast = fast->next; if (fast) { fast = fast->next; slow = slow->next; } } *head1 = head; *head2 = slow->next; //注意:一定要将链表head1的链尾置空。 slow->next = NULL; return 0; }
The linked list merge function has two methods: recursive implementation and non-recursive implementation:
Non-recursive implementation:
/* * 链表归并。 * 将两个有序的链表归并在一起,使总链表有序。 * 输入:链表head1和链表head2 * 输出:归并后的链表 * 实现方法:将链表head2中的结点依次插入到链表head1中的适当位置,使head1仍为有序链表。 */ LinkList LinkMerge(LinkList head1, LinkList head2) { LinkList p, q, t; if (!head1) return head2; if (!head2) return head1; //循环变量的初始化,q指向链表head1中的当前结点,p为q的前驱。 p = NULL; q = head1; while (head2) { //t为待插入结点。 t = head2; head2 = head2->next; //寻找插入位置,插入位置为p和q之间。 for (;q && q->data <= t->data; p = q, q = q->next); if (p == NULL) head1 = t; else p->next = t; t->next = q; //将结点t插入到p和q之间后,使p重新指向q的前驱。 p = t; } return head1; }
Recursive implementation:
LinkList LinkMerge2(LinkList head1, LinkList head2) { LinkList result; if (!head1) return head2; if (!head2) return head1; if (head1->data <= head2->data) { result = head1; result->next = LinkMerge(head1->next, head2); } else { result = head2; result->next = LinkMerge(head1, head2->next); } return result; }
Complete source code:
/* * 链表归并排序,由小到大。 */ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #define TOTAL 10 //链表长度 //链表的存储表示 typedef int ElemType; typedef struct LNode { ElemType data; struct LNode *next; }LNode, *LinkList; LinkList CreatLink(int num); LinkList LinkMergeSort(LinkList head); LinkList LinkMerge(LinkList head1, LinkList head2); LinkList LinkMerge2(LinkList head1, LinkList head2); int LinkSplit(LinkList head, LinkList *head1, LinkList *head2); int PrintLink(LinkList head); /* * 创建链表。 * 形参num为链表的长度,函数返回链表的头指针。 */ LinkList CreatLink(int num) { int i, data; //p指向当前链表中最后一个结点,q指向准备插入的结点。 LinkList head = NULL, p = NULL, q; for (i = 0; i < num; i++) { scanf("%d", &data); q = (LinkList)malloc(sizeof(LNode)); q->data = data; q->next = NULL; if (i == 0) { head = q; } else { p->next = q; } p = q; } return head; } /* * 输出链表结点值。 */ int PrintLink(LinkList head) { LinkList p; for (p = head; p; p = p->next) { printf("%-3d ", p->data); } return 0; } int main() { LinkList head; printf("输入Total个数以创建链表:\n"); head = CreatLink(TOTAL); head = LinkMergeSort(head); printf("排序后:\n"); PrintLink(head); putchar('\n'); return 0; } /* * 链表归并排序(由小到大)。 * 输入:链表的头指针, * 输出:排序后链表的头指针。 * 递归实现方法:将链表head分为两部分,分别进行归并排序,再将排序后的两部分归并在一起。 * 递归结束条件:进行递归排序的链表为空或者只有一个结点。 */ LinkList LinkMergeSort(LinkList head) { LinkList head1, head2; if (head == NULL || head->next == NULL) return head; LinkSplit(head, &head1, &head2); head1 = LinkMergeSort(head1); head2 = LinkMergeSort(head2); head = LinkMerge(head1, head2); //非递归实现 //head = LinkMerge2(head1, head2); //递归实现 return head; } /* * 链表归并。 * 将两个有序的链表归并在一起,使总链表有序。 * 输入:链表head1和链表head2 * 输出:归并后的链表 * 实现方法:将链表head2中的结点依次插入到链表head1中的适当位置,使head1仍为有序链表。 */ LinkList LinkMerge(LinkList head1, LinkList head2) { LinkList p, q, t; if (!head1) return head2; if (!head2) return head1; //循环变量的初始化,q指向链表head1中的当前结点,p为q的前驱。 p = NULL; q = head1; while (head2) { //t为待插入结点。 t = head2; head2 = head2->next; //寻找插入位置,插入位置为p和q之间。 for (;q && q->data <= t->data; p = q, q = q->next); if (p == NULL) head1 = t; else p->next = t; t->next = q; //将结点t插入到p和q之间后,使p重新指向q的前驱。 p = t; } return head1; } LinkList LinkMerge2(LinkList head1, LinkList head2) { LinkList result; if (!head1) return head2; if (!head2) return head1; if (head1->data <= head2->data) { result = head1; result->next = LinkMerge(head1->next, head2); } else { result = head2; result->next = LinkMerge(head1, head2->next); } return result; } /* * 链表分割函数。 * 将链表head均分为两部分head1和head2,若链表长度为奇数,多出的结点从属于第一部分。 * 实现方法:首先使指针slow/fast指向链首, * 然后使fast指针向前移动两个结点的同时,slow指针向前移动一个结点, * 循环移动,直至fast指针指向链尾。结束时,slow指向链表head1的链尾。 */ int LinkSplit(LinkList head, LinkList *head1, LinkList *head2) { LinkList slow, fast; if (head == NULL || head->next == NULL) { *head1 = head; *head2 = NULL; return 0; } slow = head; fast = head->next; while (fast) { fast = fast->next; if (fast) { fast = fast->next; slow = slow->next; } } *head1 = head; *head2 = slow->next; //注意:一定要将链表head1的链尾置空。 slow->next = NULL; return 0; }
The above is the entire content of this article. I hope it will be helpful to everyone's learning. I also hope that everyone will support the PHP Chinese website.
For more JavaScript implementation of linked list insertion sort and linked list merge sort related articles, please pay attention to the PHP Chinese website!

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Notepad++7.3.1
Easy-to-use and free code editor