Skip to content

Instantly share code, notes, and snippets.

@Tomcat-42
Created October 15, 2019 10:50
Show Gist options
  • Save Tomcat-42/fbf04a981da9b656acf5ebdcfdca2b63 to your computer and use it in GitHub Desktop.
Save Tomcat-42/fbf04a981da9b656acf5ebdcfdca2b63 to your computer and use it in GitHub Desktop.
bubblesort em disco
void bubblesort(FILE *F)
{
int i, j, n;
registro aux1, aux2;
int sizer = sizeof(registro);
//calcula o numero de registros
fseek(F, 0, SEEK_END);
n = ftell(F) / sizer; //ftell retorna numero de bytes tem arquvio (F); fteel/sizer retorna numero de registros armazenados
for(i = 1; i < n; i++)
{
for(j = n - 1;j >= i; j--)
{
//posiciona para fazer a leitura
fseek(F, (j - 1)*sizer, SEEK_SET);
//le os dados dos registros adjacentes para a memoria
fread(&aux1, sizer, 1, F);
fread(&aux2, sizer, 1, F);
//compara os registros por nome
if (strcmp(aux1.nome, aux2.nome)>0) //aux1>aux2
{
//posiciona para trocar
fseek(F, (j -1)*sizer, SEEK_SET);
//troca aux1 e aux2
fwrite(&aux2, sizeof(registro), 1, F);
fwrite(&aux1, sizeof(registro), 1, F);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment