/*插入排序法 从小到大排序*/
#include <stdio.h>
int main (void)
{
int i,j;
int k;
int temp;
int arr[5] = {2,4,1,6,3};
for (i = 1; i < 5; i++)
{
j = 0;
if (arr[i] > arr[j] && (j < i))//arr[i]为待插入的值
{
j++;
}
if (i != j)
{
for (k = i; k > j; k--)
{
temp = arr[i];
arr[k] = arr[k - 1];
}
arr[j] = temp;
}
}
for (i = 0; i < 5; i++)
printf ("%d ",arr[i]);
return 0;
}
#include <stdio.h>
int main (void)
{
int i,j;
int k;
int temp;
int arr[5] = {2,4,1,6,3};
for (i = 1; i < 5; i++)
{
j = 0;
if (arr[i] > arr[j] && (j < i))//arr[i]为待插入的值
{
j++;
}
if (i != j)
{
for (k = i; k > j; k--)
{
temp = arr[i];
arr[k] = arr[k - 1];
}
arr[j] = temp;
}
}
for (i = 0; i < 5; i++)
printf ("%d ",arr[i]);
return 0;
}