Data Structure --- direct insertion and sorting in C Language
// Insert sort directly
// Suppose the records to be sorted are stored in the array R [1..n].
// Initially, R [1] becomes an ordered region, and the unordered region is R [2..n].
// From i = 2 until i = n, insert R [i] into the current ordered region R [1..i-1] in turn,
// Generate an ordered area with n records.
// Yang Xin
#include
#include
#define N 10
void InsertSort (int a [], int size)
{
int i, j, k, temp = 0;
for (i = 1; i <size; i ++)
{
temp = a [i];
for (j = 0; j <i; j ++)
{
if (temp <a [j])
{
for (k = i-1; k> = j; k--)
{
a [k + 1] = a [k];
}
a [j] = temp;
break;
}
}
}
}
int main ()
{
int m = 0;
int b [N] = {9, 8, 7, 6, 5, 4, 3, 2, 1};
printf (===============================
);
printf (The data before sorting is:
9 8 7 6 5 4 3 2 1
);
InsertSort (b, N);
printf (sorted results are:
);
for (m = 0; m <N; m ++)
{
printf (% d, b [m]);
}
printf (
===============================
);
return 0;
}