33,007
社区成员
发帖
与我相关
我的任务
分享
void MergeSort(int *data, int low, int high)
{
// 参数data表示要排序的数组,low表示第一个下标,high表示最后一个下标
if (low>=high)
return;
int vector, left, right;
left=low, right=high;
vector=data[left++];
while (1)
{
while (data[left]<vector&&left<high) left++;
while (data[right]>vector&&right>low) right--;
if (left<right)
{
Swap(&data[left], &data[right]);
left++, right--;
}
else break;
}
if (low!=right)
{
Swap(&data[low], &data[right]);
}
MergeSort(data, low, right-1);
MergeSort(data+right+1, right+1, high);
}
void Swap(int* a, int* b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}