-
Notifications
You must be signed in to change notification settings - Fork 2
/
insertionSort.c
53 lines (47 loc) · 1.05 KB
/
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
#include "ordenacaoInterna.h"
#include <time.h>
#include <stdlib.h>
void insertionSort(int *v, int tam)
{
int j, chave, i;
for(j=1; j < tam; j++){
chave = v[j];
i = j - 1;
while(i>=0 && v[i]>chave){
v[i+1] = v[i];
i--;
}
v[i+1] = chave;
}
}
void insertionSortBB(int *v, int tam)
{
int j, chave, i, inicio, fim, meio, flag, x, temp,index;
for(j=1; j < tam; j++){
chave = v[j];
i = j - 1;
inicio = 0;
fim = j;
flag = 0;
meio = (int)((inicio+fim)/2);
while(inicio < fim){
if(chave < v[meio]){
fim = meio - 1;
flag = 1;
}else{
inicio = meio + 1;
}
meio = (int)((inicio+fim)/2);
}
x=j;
if(flag == 1 && (v[meio] < chave))
index = meio + 1;
else
index = meio;
while(x != index){
v[x] = v[x-1];
x = x-1;
}
v[index] = chave;
}
}