Merge sort C language

Merge sort

Merge sort (MERGE-SORT) is based on the operation of an efficient merge sorting algorithm that uses the classic divide and conquer (divide-and-conquer) strategy (divide and conquer the problem points (divide) into smaller then recursively solving problems, and phase rule (conquer) the stage will receive points for each answer to "repair" together, that is, divide and rule), which has been ordered sequences were combined to give complete and orderly sequence; that is, first the each sub-sequence ordered, then ordered that the segment between the promoter sequence, if the two merged into a sorted list ordered list, referred way merge

1, merge sort of the basic idea

Collating sequence to be R [0 ... n-1] is as an ordered sequence of length n 1, merge ordered list of pairs of adjacent, to give the n / 2 length ordered Table 2 ; these ordered sequence merging again to give n / 4 of length 4 ordered sequence; so repeatedly continues, to give a final ordered sequence of length n,

2, the merge sort algorithm described

The first step: the application space, so that the size of the sum of two sorted sequences, the sequence storage space for merged

Step 2: Set two pointers, initially sorted into two positions, respectively a starting position of the sequence

The third step: compare two pointer points to an element, the selected element into a relatively small space to merge, and move the pointer to the next position

Repeat step 3 until a tail pointer out of sequence, all the other elements of the sequence remaining end of the sequence is copied directly to Merge

In fact, merge sort to do two things:

(1) "exploded" - binary sequences each division (recursive)

(2) "Merge" - after twenty-two combined sequence segments of the divided sorted

How to merge?

Each merge process are ordered sequences of two segments are combined and then sorted.

The two segments are ordered sequence R [low, mid] and R [mid + 1, high].

First they merged into a local temporary storage array R2, with the completion of the merger and then copied back R, R2.

We call R [low, mid] first paragraph, R [mid + 1, high] for the second segment.

Two sections each taken from a record keyword comparison, the R2 into smaller, and finally the remaining portion of each segment directly copied to R2.

After such a process, already R2 is an ordered sequence, and then copy it back to the R, a merge sort is completed.

3, code implementation

/ * Sequence of a sequence length of up to half split *. 1 /
void MergeSort_UptoDown (NUM int *, int Start, End int)
{
    int = Start + MID (End - Start) / 2;

    if (start >= end)
    {
        return;
    }
   
    MergeSort_UptoDown(num, start, mid);
    MergeSort_UptoDown(num, mid + 1, end);

    Merge(num, start, mid, end);
}

the Merge void (NUM int *, int Start, MID int, int End)
{
    int * TEMP = (int *) the malloc ((Start-End +. 1) * the sizeof (int)); // applications have space to store two the program area after the temporary region merge
    int I = Start;
    int + J = MID. 1;
    int K = 0;

    while (i <= mid && j <= end)
    {
        if (num[i] <= num[j])
        {
            temp[k++] = num[i++];
        }
        else
        {
            temp[k++] = num[j++];
        }
    }

    while (i <= mid)
    {
        temp[k++] = num[i++];
    }
    while (j <= end)
    {
        temp[k++] = num[j++];
    }

    // The element of a sort staging area integrate into the original array
    for (I = 0; I <K; I ++)
    {
        NUM [Start + I] = TEMP [I];
    }

    free(temp);
}

4, resolution process

(Source: https: //www.cnblogs.com/chengxiao/p/6194356.html)

Complete code:

#include <stdio.h>
#include <stdlib.h>

void MergeSort_UptoDown(int *num, int start, int end);
void Merge(int *num, int start, int mid, int end);

int main()
{
    /* 归并排序(升序) */
    int num[10] = {5, 1, 8, 4, 7, 2, 3, 9, 0, 6};
    int length = sizeof(num) / sizeof(num[0]);
    int i;

    MergeSort_UptoDown(num, 0, length - 1);

    for (i = 0; i < length; i++)
    {
        printf("%d ", num[i]);
    }

    return 0;
}

/* 将序列对半拆分直到序列长度为1*/
void MergeSort_UptoDown(int *num, int start, int end)
{
    int mid = start + (end - start) / 2;

    if (start >= end)
    {
        return;
    }
   
    MergeSort_UptoDown(num, start, mid);
    MergeSort_UptoDown(num, mid + 1, end);

    Merge(num, start, mid, end);
}

void Merge(int *num, int start, int mid, int end)
{
    int *temp = (int *)malloc((end-start+1) * sizeof(int));    //申请空间来存放两个有序区归并后的临时区域
    int i = start;
    int j = mid + 1;
    int k = 0;

    while (i <= mid && j <= end)
    {
        if (num[i] <= num[j])
        {
            temp[k++] = num[i++];
        }
        else
        {
            temp[k++] = num[j++];
        }
    }

    while (i <= mid)
    {
        temp[k++] = num[i++];
    }
    while (j <= end)
    {
        temp[k++] = num[j++];
    }

    //将临时区域中排序后的元素,整合到原数组中
    for (i = 0; i < k; i++)
    {
        num[start + i] = temp[i];
    }

    free(temp);
}

Guess you like

Origin www.linuxidc.com/Linux/2019-08/159737.htm