//insertion sort is an efficient algorithm for sorting a small number of elements.
1) void insertion_sort(int a[],int n)
{
int key,j;
for(int i = 1; i < n; ++i)
{
key = a[i];
j = i-1;
while(j >= 0 && key < a[j])
{
a[j] ^= a[j+1];
a[j+1] ^= a[j];
a[j] ^= a[j+1];
--j;
}
}
}
2) void insertion_sort(int a[],int n)
{
int i, j, value;
for(i = 1; i < n; ++i)
{
value = a[i];
for (j = i - 1; j >= 0 && a[j] > value; j--)
{
a[j + 1] = a[j];
}
a[j+1] = value;
}
}
insertion sort
最新推荐文章于 2024-05-08 17:58:22 发布