-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ins.c
79 lines (58 loc) · 1.32 KB
/
Ins.c
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
#include <stdio.h>
#include <stdlib.h>
// insertion sort, several errors
int X[10], // input array
Y[10], // workspace array
NumInputs, // length of input array
NumY = 0; // current number of
// elements in Y
void GetArgs(int AC, char **AV)
{ int I;
NumInputs = AC - 1;
for (I = 0; I < NumInputs; I++)
X[I] = atoi(AV[I+1]);
}
void ScootOver(int JJ)
{ int K;
for (K = NumY; K > JJ; K--)
Y[K] = Y[K-1];
}
void Insert(int NewY)
{ int J;
if (NumY == 0) { // Y empty so far,
// easy case
Y[0] = NewY;
return;
}
// need to insert just before the first Y
// element that NewY is less than
for (J = 0; J < NumY; J++) {
if (NewY < Y[J]) {
// shift Y[J], Y[J+1],... rightward
// before inserting NewY
ScootOver(J);
Y[J] = NewY;
return;
}
}
// one more case: NewY > all existing Y elements
Y[NumY] = NewY;
}
void ProcessData()
{
for (NumY = 0; NumY < NumInputs; NumY++)
// insert new Y in the proper place
// among Y[0],...,Y[NumY-1]
Insert(X[NumY]);
}
void PrintResults()
{ int I;
for (I = 0; I < NumInputs; I++)
printf("%d\n",Y[I]);
}
int main(int Argc, char ** Argv)
{
GetArgs(Argc,Argv);
ProcessData();
PrintResults();
}