-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
58 lines (49 loc) · 937 Bytes
/
list.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
#include "maff.h"
#include <stdio.h>
#include <stdlib.h>
int* setarray(int size, const int content) {
int* ret = malloc(size * 4);
for (int i = 0; i < size; i++) {
ret[i] = content;
}
return ret;
}
void printarr(int *arr, int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
void iterate(int* arr, int size) {
int max = size - 1;
for (int i = 0; i < size; i++) {
if (i == max) {
int x = arr[i - 1];
int y = arr[i];
if (x > y) {
arr[i - 1] = y;
arr[i] = x;
}
} else {
int x = arr[i];
int y = arr[i + 1];
if (x > y) {
arr[i] = y;
arr[i + 1] = x;
}
}
}
}
void sort(int* arr, int size) {
for (int i = 0; i < size; i++) {
iterate(arr, size);
}
}
int sum(int *list, int len) {
int x = 0;
for (int i = 0; i < len; i++) {
x = x + *list;
list++;
}
return x;
}