-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.c
More file actions
62 lines (53 loc) · 1.06 KB
/
Copy pathsort.c
File metadata and controls
62 lines (53 loc) · 1.06 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
#include <stdio.h>
#define NMAX 10
int input(int *a);
void sorting(int *a);
void output(int *a);
int main() {
int data[NMAX];
if (input(data) == 0) {
sorting(data);
output(data);
} else {
printf("n/a");
return 1;
}
return 0;
}
int input(int *a) {
char c;
for (int *p = a; p - a < NMAX; p++) {
if (scanf("%d", p) != 1) {
return 1;
}
}
if (scanf("%c", &c) != 1 || c != '\n') {
return 1;
}
return 0;
}
void sorting(int *a) {
int x = 0;
int c = 1;
while (c == 1) {
c = 0;
for (int *p = a; p - a < NMAX - 1; p++) {
for (int *i = a; i - a < NMAX - 1; i++) {
if (*i > *(i + 1)) {
x = *i;
*i = *(i + 1);
*(i + 1) = x;
c = 1;
}
}
}
}
}
void output(int *a) {
for (int *p = a; p - a < NMAX; p++) {
printf("%d", *p);
if (p - a + 1 != NMAX) {
printf(" ");
}
}
}