蔡子经数据结构1.1

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
#include <stdio.h>

int sgn(int x)
{
if(x > 0) return 1;
if(x < 0) return -1;
return 0;
}

int cmp(int *a, int *b , int n, int m)
{
int i = 0, j = 0;
while(i < n && j < m)
{
if(a[i] != b[j]) return sgn(a[i]-b[j]);
++i,++j;
}
if(i == n && j == m) return 0;
if(i == n) return -1;
return 1;
}

int main()
{
int a[10] = {0,1,4,7,8,5,2,3,6,9};
int b[5] = {0,1,4,7,8};
int res = cmp(a, b, 10, 5);
printf("%d\n",res);
return 0;
}