-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertSort.c
More file actions
56 lines (45 loc) · 1.28 KB
/
InsertSort.c
File metadata and controls
56 lines (45 loc) · 1.28 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
#include <stdio.h>
#include <string.h>
#define MAX_PESSOAS 100
#define MAX_NOME 50
typedef struct {
char nome[MAX_NOME];
int idade;
} Pessoa;
void insertSort(Pessoa pessoas[], int n) {
for (int i = 1; i < n; i++) {
Pessoa chave = pessoas[i];
int j = i - 1;
while (j >= 0 && pessoas[j].idade > chave.idade)
{
pessoas[j + 1] = pessoas[j];
j--;
}
pessoas[j + 1] = chave;
}
}
void imprimirPessoas(Pessoa pessoas[], int n) {
for (int i = 0; i < n; i++) {
printf("Nome: %s, Idade: %d\n", pessoas[i].nome, pessoas[i].idade);
}
}
int main() {
Pessoa pessoas[MAX_PESSOAS];
int n = 0;
int quantidade;
printf("Digite a quantidade de pessoas: ");
scanf("%d", &quantidade);
for (int i = 0; i < quantidade; i++) {
printf("Digite o nome da pessoa %d: ", i + 1);
scanf("%s", pessoas[i].nome);
printf("Digite a idade da pessoa %d: ", i + 1);
scanf("%d", &pessoas[i].idade);
n++;
}
printf("\nAntes da ordenação:\n");
imprimirPessoas(pessoas, n);
insertSort(pessoas, n);
printf("\nDepois da ordenação crescente:\n");
imprimirPessoas(pessoas, n);
return 0;
}