#define MAX_SIZE 10
typedef struct
{
int m[MAX_SIZE ];
int length;
} SqList;
//用排序,铁定跑不了这个swap函数,最好自己写一个
void swap(SqList *q, int i, int j);
void swap(SqList *q, int i, int j)
{
int temp = q->m[i];
q->m[i] = q->m[j];
q->m[j] = temp;
MSort(SR, TR2, s, m);
MSort(SR, TR2, m + 1, t);
Merge(TR2, TR1, s, m, t);
}
}
//最主要函数;一定要看懂
void Merge(int SR[], int TR[], int i, int m, int n)
{
int j, k, l;
for (j = m + 1, k = i; i <= m && j <= n; k++)
{
if (SR[i] < SR[j])
{
TR[k] = SR[i++];
}
else
{
TR[k] = SR[j++];
}
}
if (i <= m)
{
for (l = 0; l <= m - i; l++)
{
TR[k + l] = SR[i + l];
}
}
if (j <= n)
{
for (l = 0; l <= n - j; l++)
{
TR[k + l] = SR[j + l];
}
}
}
typedef struct
{
int m[MAX_SIZE ];
int length;
} SqList;
//用排序,铁定跑不了这个swap函数,最好自己写一个
void swap(SqList *q, int i, int j);
void swap(SqList *q, int i, int j)
{
int temp = q->m[i];
q->m[i] = q->m[j];
q->m[j] = temp;
}
//归并排序算法
void MergeSort(SqList *L)
{
MSort(L->m, L->m, 1, L->length);
}
void MSort(int SR[], int TR1[], int s, int t)
{
int m;
int TR2[MAX_SIZE];
if (s == t)
{
TR1[s] = SR[s];
}
else
{
//递归调用
m = (s + t) / 2;MSort(SR, TR2, s, m);
MSort(SR, TR2, m + 1, t);
Merge(TR2, TR1, s, m, t);
}
}
//最主要函数;一定要看懂
void Merge(int SR[], int TR[], int i, int m, int n)
{
int j, k, l;
for (j = m + 1, k = i; i <= m && j <= n; k++)
{
if (SR[i] < SR[j])
{
TR[k] = SR[i++];
}
else
{
TR[k] = SR[j++];
}
}
if (i <= m)
{
for (l = 0; l <= m - i; l++)
{
TR[k + l] = SR[i + l];
}
}
if (j <= n)
{
for (l = 0; l <= n - j; l++)
{
TR[k + l] = SR[j + l];
}
}
}