blob: 277d6a79f3e41c3a4caa465a662481cb37ea7027 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
int a[1000];
/* 插入排序 */
void InsertSort(int a[],int n)
{
int p;
int i;
int tmp;
p =1;
while(p<n){
tmp=a[p];/*取出未排序序列中的第一个元素*/
i=p;
while(i>=1&&a[i-1]>tmp){
a[i]=a[i-1];/*依次与已排序序列中元素比较并右移*/
i=i-1;
}
a[i]=tmp;/*放进合适的位置 */
p=p+1;
}
}
int main()
{
int size;
int i;
size = getint();
i = 0;
while(i<size){
a[i] = getint();
i = i+1;
}
InsertSort(a,size);
putarray(size,a);
putch(10);
return 0;
}
|