typedef int KeyType;
typedef int InfoType;
typedef struct
{
KeyType key;
InfoType data;
}RecType, SqList;
/*
* 直接插入排序
*/
void InsertSort(RecType R[], int n)
{
int i,j;
RecType tmp;
for(i=1; i<n; i++)
{
tmp = R[i];
j = i-1;
while(j>=0 && R[j].key>tmp.key)
{
R[j+1] = R[j];
j--;
}
R[j+1] = tmp;
}
}
/*
* 二分插入排序
*/
void InsertSort1(RecType R[], int n)
{
int i,j,low,high,mid;
RecType tmp;
for(i=1; i<n; i++)
{
tmp = R[i];
low = 0;
high = i-1;
while(low<=high)
{
mid = (low+high)/2;
if(tmp.key<R[mid].key)
high = mid-1;
else
low = mid+1;
}
for(j=i-1; j>=high+1; j--)
R[j+1] = R[j];
R[high+1] = tmp;
}
}
/*
* 希尔排序
*/
void ShellSort(SqList R[], int n)
{
int i,j,gap;
SqList tmp;
gap = n/2;
while(gap>0)
{
for(i=gap; i<n; i++)
{
tmp = R[i];
j = i-gap;
while(j>=0 && tmp.key<R[j].key)
{
R[j+gap] = R[j];
j = j-gap;
}
R[j+gap] = tmp;
}
gap = gap/2;
}
}
内排序算法
最新推荐文章于 2024-04-22 22:08:26 发布