* main.c
* 插入法排序
* Created on: Nov 7, 2010
* Author: jenson
*/
#include<stdio.h>
typedef int item;
#define key(A)(A)
#define less(A,B)(key(A)<= key(B))/*比较大小*/
#define swap(A,B){item t = A;A = B;B = t;}/*交換*/
#define compexch(A,B) {if(less(A,B))swap(A,B)}/*比较大小并交换*/
void insertion(item a[], int l, int r) {
int i;
for (i = r; i >1; i--) {
compexch(a[i-1],a[i]);
}
for (i = l + 2; i <= r; i++) {
int j = i;
item v = a[i];
while (less(v,a[j-1])) {
a[j] = a[j - 1];
j--;
}
a[j] = v;
}
}
int main() {
item a[7] = { 0, -10, 8, -9, 4, 7, 2 };
insertion(a, 0, 6);
int i = 0;
for (i = 0; i < 7; i++) {
printf("%d\t", a[i]);
}
return 0;
}