C language is based on recursive merge sort

I found myself forgetting a lot of recursion when I was writing my merge and sort homework.

Now review and summarize recursion as follows:

  • Merge sorting uses the idea of ​​divide and conquer. "Divide and conquer" means to divide complex problems into simple ones for solution.
  • In general: Divide and conquer is an idea for solving complex problems, and recursion provides a solution for it.

Let me talk about recursion first:

  1. Simply put, recursion is to call yourself. Recursive relationship is the establishment of a relationship between the entity itself and itself.
  2. Conditions for recursion:
    (1) The sub-problem must be consistent with the original problem and simpler.
    (2) Recursion must have an exit, otherwise it will never come out.

Next, we use merge sort to have a deeper understanding of recursion:

  • The general idea of ​​merge sort is very simple: that is, divide first, then merge.

Every time it is divided into two, when will this division end?

Answer: Think about it this way, what we will finally get is a sorted large array. The sub-problem is to get sorted small arrays. When will the small arrays be ordered? When there is only one element in the array, Then the small array is ordered. In this way, we get the recursive condition that is low<high.

The picture above:
Insert picture description here
Actually, I also understood what the teacher taught in class, but I still don't understand how he merged in order. This is also a criticism of my recursive forgetting.
The code I wrote MERGE merges and sorts two small arrays. (The code behind)
is actually very easy after understanding the recursive steps: continue with the above picture:
Insert picture description here

Code↓

#include <stdio.h>
#include <stdlib.h>
#define N 1000000


void Mergesort(int *A,int low,int high,int *temp)
{
    
    
    if(low < high)
    {
    
    
        int mid = (high+low)/2;
        Mergesort(A,low,mid,temp);
        Mergesort(A,mid+1,high,temp);
        MERGE(A,low,mid,high,temp);
    }
}

void MERGE(int *A,int low,int mid,int high,int *temp)
{
    
    
    int i=low;int j=mid;
    int m=mid+1;int n=high;
    int k=0;
    //开始合并两个数组;
    while(i <= j && m <= n)
    {
    
    
        if(A[i] <= A[m])
        {
    
    
            temp[k++] = A[i++];
        }
        else
        {
    
    
            temp[k++] = A[m++];
        }
    }
    while(i <= j)
    {
    
    
        temp[k++] = A[i++];
    }

    while(m <= n)
    {
    
    
        temp[k++] = A[m++];
    }

    //把temp数组中的结果装回A数组
    for(i = 0; i < k; i++)
    {
    
    
        A[i+low] = temp[i];
    }
}

int main(int argc, const char * argv[])
{
    
    
    int i,j;
    int num[100000];
    int temp[100000];
    FILE* fp = fopen("1.txt","r");//读文件
    FILE* stream = fopen("2.txt","w");//向文件中写入
    if(fp==NULL)
    {
    
    
        printf("没找到文件");
        return 0;
    }
    for(i=0;i<100000;i++)
    {
    
    
        fscanf(fp,"%d",&num[i]);
    }
    fclose(fp);
    Mergesort(num,0,99999,temp);
    for(j=0;j<100000;j++)
    {
    
    
        fprintf(stream,"%d\n",num[j]);
    }
    fclose(stream);
    return 0;
}

There are txt file read and write operations in my code, because the job needs, (✿◡‿◡).

Guess you like

Origin blog.csdn.net/qq_44165430/article/details/105462468