-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1-3.c
More file actions
85 lines (76 loc) · 1.41 KB
/
1-3.c
File metadata and controls
85 lines (76 loc) · 1.41 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#define MAXSIZE 100 //空间初始分配变量
#define LISTINCREMENT 20 //空间分配增量
#define ERROR -1
#define OK 1
typedef int Status;
typedef int ElemType;
#include <stdio.h>
#include <stdlib.h>
typedef struct{
ElemType *elem;
int length;
int listsize;
}SqList;
void PrintList(SqList L);
int CreatList(SqList *L)
{
int i;
do
{
printf("请输入元素个数: ");
scanf("%d",&((*L).length));
}while((*L).length<0);
(*L).elem=(ElemType *)malloc(MAXSIZE*sizeof(ElemType));
if(!(*L).elem)
return ERROR;
for(i=0;i<(*L).length;i++)
{
printf("请输入第%d个元素值=>",i+1);
scanf("%d",&((*L).elem[i]));
}
return OK;
}
Status MergeSort(SqList A,SqList B,SqList *C)
{
(*C).elem=(ElemType *)malloc((A.listsize+B.listsize)*sizeof(ElemType));
if(!(*C).elem)
return ERROR;
(*C).listsize=A.listsize+B.listsize;
int ia,ib,ic;
ia=ib=ic=0;
int j;
while(ia<A.length&&ib<B.length)
{
if(A.elem[ia]<B.elem[ib])
(*C).elem[ic++]=A.elem[ia++];
else
if(A.elem[ia]>B.elem[ib])
(*C).elem[ic++]=B.elem[ib++];
else
{
(*C).elem[ic++]=A.elem[ia++];
ib++;
}
}
for(j=ia;j<A.length;j++)
(*C).elem[ic++]=A.elem[j];
for(j=ib;j<B.length;j++)
(*C).elem[ic++]=B.elem[j];
(*C).length=ic;
return OK;
}
void main()
{
SqList A,B,C;
CreatList(&A);
CreatList(&B);
MergeSort(A,B,&C);
PrintList(C);
}
void PrintList(SqList L)
{
int i;
for(i=0;i<L.length;i++)
printf("%d-->",L.elem[i]);
printf("\n");
}