6-7 归并排序 分数 10

6-7 归并排序 分数 10_第1张图片

typedef int Datatype;
typedef struct 
{
	Datatype* elem; 
	int Length;
}SqList;
void Merge(SqList L, int low, int m, int high) 
{
    int array[1001] = { 0 };

    int k = low;

    int p = low;
    int q = m + 1;

    while (p <= m && q <= high)
    {
        if (L.elem[p] < L.elem[q])
            array[k++] = L.elem[p++];
        else
            array[k++] = L.elem[q++];
    }

    while (p <= m) 
        array[k++] = L.elem[p++];
    while (q <= high)
        array[k++] = L.elem[q++];

    for (int i = low; i <= high; i++)
    {
        L.elem[i] = array[i];
    }
}

void MergeSort(SqList L, int low, int high)
{
    int mid = 0;
    if (low < high)  
    {
        mid = (low + high) / 2;             
        MergeSort(L, low, mid);          
        MergeSort(L, mid + 1, high);      
        Merge(L, low, mid, high);        
    }
}

你可能感兴趣的:(C语言经典题目,c语言,排序算法,算法,数据结构)