直接插入排序:
#include <stdio.h>
#include <stdlib.h>
#define LEN 6
typedef int RecType;
void InserSort(RecType R[],int n) //直接插入法
{
int i, j;
RecType temp;
for(i = 1; i < n; i++)
{
temp = R[i];
j = i - 1;
while(j >= 0 && temp < R[j]) //升序
{
R[j+1] = R[j];
j--;
}
R[j+1] = temp;
}
}
void print(RecType R[],int n)
{
int i = 0;
for(i = 0; i < n; i++)
{
printf("%d ",R[i]);
}
printf("\n");
}
int main()
{
RecType R[LEN] = {31,141,59,26,41,58};
InserSort(R,LEN);
print(R,LEN);
return 0;
}