两个O(nlogn)的排序方法
QSort(快排):
int Partition(int *R,int low,int high)
{
int pivotkey=R[low];
while(low=pivotkey)
high--;
R[low]=R[high];
while(low
MSort(归并):
void Merge(int *R,int *RL,int first,int m,int last)//R为原数组 RL为中间数组
{
int i,k;
int begin1=first;
int end1=m;
int begin2=m+1;
int end2=last;
for(k=first;begin1<=end1&&begin2<=end2;k++)
{
if(R[begin1]<=R[begin2]) RL[k]=R[begin1++];
else RL[k]=R[begin2++];
}
while(begin1<=end1)
{
RL[k++]=R[begin1++];
}
while(begin2<=end2)
{
RL[k++]=R[begin2++];
}
for(i=first;i<=last;i++)
{
R[i]=RL[i];
}
}
void MSort(int *R,int *RL,int first,int last)
{
if(first


本文深入探讨了两种高效排序算法:快速排序(QSort)和归并排序(MSort)。快速排序通过分区实现递归排序,而归并排序则采用合并有序子序列的方法。了解这两种算法的核心思想、实现步骤及复杂度分析,有助于提升数据处理效率。

660

被折叠的 条评论
为什么被折叠?



