-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinsertionSort.c
52 lines (38 loc) · 971 Bytes
/
insertionSort.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
//Algoritmo de ordenação em memória primária - Insertion Sort
//************ FUNCIONANDO ***************
#include<stdio.h>
#define MAXN 1010 //Define MAXN com 1010
void insertionSort (int vet[], int tam){ //Implementação da função Insertio Sort
int aux, i, j;
for (i=1; i <tam; i++){
aux = vet[i];
j= i-1;
while (aux< vet[j] && j>= 0){
vet[j+1] = vet[j];
j--;
}
vet[j+1] = aux;
}
}
void printfVetor(int vet[], int tam){ //Função para printar
int i;
printf("Vetor Organizado: ");
for (i=0; i< tam; i++){
printf("%d ", vet[i]);
}
printf("\n");
}
int main(){
int n, vetor[MAXN]; // declaro as variáveis que usarei
int i; //Variaveis do for's
printf("Insira o tamanho do vetor: ");
scanf("%d", &n); // leio o valor de n
printf("Insira os valores do vetor: ");
for(i=0; i<n; i++){
scanf("%d", &vetor[i]); // leio os n números do vetor
}
//Chamada de funçoes
insertionSort (vetor, n);
printfVetor(vetor, n);
return 0;
}