-
Notifications
You must be signed in to change notification settings - Fork 0
/
heapsort.c
67 lines (57 loc) · 1.33 KB
/
heapsort.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
58
59
60
61
62
63
64
65
66
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char * ImprimePalavras( char **palavras, int m ){
for (int i = m; i >= 1; i--)
{
printf("%s\n", palavras[i]);
free(palavras[i]);
}
}
void troca( char *lp[], int x, int y ){
char *aux;
aux = lp[x];
lp[x] = lp[y];
lp[y] = aux;
}
void compara( char *lp[], int pai, int fd, int fs ){
if(strcmp( lp[fd], lp[pai] ) < 0) troca( lp, pai, fd );
if(strcmp( lp[fs], lp[pai] ) < 0) troca( lp, pai, fs );
}
void buildHeap( char *lp[] , int m ){
int p = 1;
while ( 2*p <= m )
{
int pai_d = (2*p)+1;
int pai_s = 2*p;
if (2*(pai_d)+1 <= m)
{
compara( lp, pai_d, 2*(pai_d)+1, 2*(pai_d) );
}
if (2*(pai_s)+1 <= m){
compara( lp, pai_s, 2*(pai_s)+1, 2*(pai_s) );
}
p += 1;
}
}
void heapsort( char **lp , int m ){
int pai = 1;
while(m > 0){
buildHeap( lp , m );
compara( lp , pai , 3 , 2 );
troca( lp , pai , m );
m -= 1;
}
troca( lp , pai , 3 );
}
int main(){
char *palavras[1000];
int m = 1, tam = sizeof( palavras );
while( !feof( stdin ) ){
palavras[m] = malloc( sizeof(char) * 10 );
fgets( palavras[m] , tam , stdin );
m += 1;
}
heapsort( palavras , m-2 );
ImprimePalavras( palavras , m-2 );
}