发布网友
共2个回答
热心网友
// 直接插入排序
void StaInsertSort(int a[], int n)
{
int i, j, tmp;
for(i = 1; i < n; ++i)
{
tmp = a[i];
j = i-1;
while(j>=0 && tmp<a[j])
{
a[j+1] = a[j];
--j;
}
a[j+1] = tmp;
}
}
/* 折半排序 */
void MidSort(int a[], int n)
{
int i, j;
int low, high, mid;
int tmp;
for(i = 1; i < n; i++)
{
tmp = a[i];
low = 0, high = i-1;
while(low <= high)
{
mid = (low+high)/2;
if(a[mid] > tmp)
high = mid-1;
else
low = mid+1;
}
low = high, high = i;
while(high > low+1)
{
a[high] = a[high-1];
--high;
}
a[low+1] = tmp;
}
}
/* 冒泡排序 */
void MaoPaoSort(int a[], int n)
{
int i, j;
int tmp;
for(i = 0; i < n-1; ++i)
{
for(j = i+1; j < n; ++j)
{
if(a[i] > a[j])
{
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
}
/* 快速排序 */
void QuickSort(int a[], int low, int high)
{
int i, j;
int tmp;
if(low < high)
{
i = low, j = high;
tmp = a[low];
while(i < j)
{
while(i<j && a[j]>=tmp)
--j;
a[i] = a[j];
while(i<j && a[i]<=tmp)
++i;
a[j] = a[i];
}
a[i] = tmp;
QuickSort(a, low, i-1);
QuickSort(a, i+1, high);
}
}
/* 选择排序 */
void SelectSort(int *a, int n)
{
int i, j, k;
int tmp;
for(i = 0; i < n-1; ++i)
{
k = i;
for(j = i+1; j < n; ++j)
if(a[k] > a[j])
k = j;
tmp = a[i];
a[i] = a[k];
a[k] = tmp;
}
}
/* 堆排序 */
void Sift(int a[], int low, int high) //堆调整函数
{
int i = low, j = 2*i+1;
int tmp = a[i];
while(j <= high)
{
if(j<high && a[j]<a[j+1])
++j;
if(tmp < a[j])
{
a[i] = a[j];
i = j;
j = 2*i+1;
}
else
break;
}
a[i] = tmp;
}
void HeapSort(int a[], int n) //堆排序函数
{
int i;
int tmp;
for(i = n/2-1; i >= 0; --i)
Sift(a, i, n-1);
for(i = n-1; i >= 1; --i)
{
tmp = a[0];
a[0] = a[i];
a[i] = tmp;
Sift(a, 0, i-1);
}
}
追问请问void那行是什么意思?
热心网友
五种。。要那么多干嘛 杂而不精