Files
aisd/Lab1/insertion.c
T
2025-04-27 18:04:47 +02:00

36 lines
688 B
C

#include <stdio.h>
#include <stdlib.h>
// Capture the pipe of array contents, first number is the size
int main() {
int s;
int res = scanf("%d", &s);
if (res != 1) {
fprintf(stderr, "Error reading size\n");
return 1;
}
int *array = malloc(sizeof(int) * s);
for (int i = 0; i < s; i++) {
res = scanf("%d", &array[i]);
if (res != 1) {
fprintf(stderr, "Error reading array element\n");
free(array);
return 1;
}
}
for (int j = 0; j < s; j++) {
int key = array[j];
int i = j - 1;
while (i >= 0 && array[i] > key) {
array[i + 1] = array[i];
i -= 1;
}
array[i + 1] = key;
}
free(array);
return 0;
}