The merging of linear algorithm sorting

Source: Internet
Author: User

Sort by Category

A lot of people are not very clear about sorting classification, in fact I do not know, but I can publish their own views. The sorting algorithm is divided into two categories, internal sorting and external sorting. Internal sorting generally refers to the data in the memory of the sorting algorithm, the amount of data is generally relatively small. The maximum is not more than 2G, after all, this will take the user process address all occupied, in fact, it is impossible, card dead. External sorting generally refers to the data stored in the file, do not exclude the database stored inside. The internal and external sort links are here. The famous merge sort.

Merge

The external sort first divides the data into some data segments that can be loaded into memory, each data segment is sorted first, and finally, the merge sort is used to store the file again. Merge sort can have a lot of ways, as research articles are generally the most typical two-way sequencing, and then more words need to be related to the needs. The amount of data is unfortunately not generally known, so a single linked list node is used. Can look at the implementation of the two-way merger.

Realize

#include <stdio.h> #include <malloc.h> #include <memory.h>/************************************** @ Link List node @ Data--node data @ Next--Next node pointer ********************************************** /typedef struct Node{int data;struct node* next;} node;/************************************************************************@ linked list insertion node ************************* /int node_insert (struct node* the_node,int data) {//Find last node struct node * Cur_node=the_node;while (Cur_node->next! = NULL) {cur_node=cur_node->next;} Create a new node struct node* new_node= (struct node*) malloc (sizeof (struct node) *); new_node->data=data;new_node->next= null;//Add new node Cur_node->next=new_node;return 0;} /************************************************************************@ Release list ********************************* /int node_free (struct node* the_node) {struct node* cur_node=null;while (the_node = NULL) {cur_node=the_node;the_node=the_node->next;free (cur_node); cur_node=null;} return 0;} /************************************************************************@ Linked list Merge ********************************* /int list_merge (struct node* first,struct node* second,struct node** result) {/ /record Operation node struct node** cur_result=result;//Select Small while (first! = NULL && second! = null) {if (First->data < Secon D->data) {*cur_result=first;cur_result=& ((*cur_result)->next); first=first->next;} else{*cur_result=second;cur_result=& ((*cur_result)->next); second=second->next;}} Handle the remaining if (first! = NULL) {*cur_result=first;} Handles the remaining if (second! = NULL) {*cur_result=second;} return 0;} /************************************************************************@ Main Function--Program entry point ************************** /int Main () {//Create first linked list struct node* first= (struct node*) malloc (sizeof ( struct node); first->data=1;first->next=null;for (int i=2;i<10;i++) {Node_insert (first,i);} Create a second linked list struct node* second= (struct node*) malloc (sizeof (struct node) *);second->data=2;second->next=null; for (int i=14;i<100;i++) {Node_insert (second,i);} Merge sort struct node* result=null;list_merge (first,second,&result);//output result struct node* out_result=result;while (out _result = NULL) {printf ("%d ___", out_result->data); out_result=out_result->next;} Release list node_free (result); Result=null;first=null;second=null;return 0;}

Run results

There is a picture to testify

Summarize

1. The core of the two-way merge is the while loop inside the merge. Compare size.

2. Again is the processing of the remaining data, so it is a while loop, we do not know how many data left behind after all.

3. The other is the typical construction of the single-linked list data structure.

The merging of linear algorithm sorting

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.