/* AUTORE: Manuel Vichi 3^AIN Algoritmi sort su file */ #include #include #include void fSelectionSort(const char *nomeFile) { FILE *fp = fopen(nomeFile, "r+b"); if (fp == NULL) { perror("Errore nell'apertura del file"); return; } fseek(fp, 0, SEEK_END); long dimensioneFile = ftell(fp); fseek(fp, 0, SEEK_SET); size_t n = dimensioneFile / sizeof(int); int valoreMin, valoreCorrente; size_t indiceMin; int temp; for (size_t i = 0; i < n - 1; i++) { indiceMin = i; fseek(fp, i * sizeof(int), SEEK_SET); fread(&valoreMin, sizeof(int), 1, fp); for (size_t j = i + 1; j < n; j++) { fseek(fp, j * sizeof(int), SEEK_SET); fread(&valoreCorrente, sizeof(int), 1, fp); if (valoreCorrente < valoreMin) { valoreMin = valoreCorrente; indiceMin = j; } } if (indiceMin != i) { fseek(fp, i * sizeof(int), SEEK_SET); fread(&temp, sizeof(int), 1, fp); int valoreTemp; fseek(fp, indiceMin * sizeof(int), SEEK_SET); fread(&valoreTemp, sizeof(int), 1, fp); fseek(fp, i * sizeof(int), SEEK_SET); fwrite(&valoreTemp, sizeof(int), 1, fp); fseek(fp, indiceMin * sizeof(int), SEEK_SET); fwrite(&temp, sizeof(int), 1, fp); } } fclose(fp); } void fBubbleSort(const char *nomeFile) { FILE *fp = fopen(nomeFile, "r+b"); if (fp == NULL) { perror("Errore nell'apertura del file"); return; } fseek(fp, 0, SEEK_END); long dimensioneFile = ftell(fp); fseek(fp, 0, SEEK_SET); size_t n = dimensioneFile / sizeof(int); int valore1, valore2; bool scambiato; for (size_t i = 0; i < n - 1; i++) { scambiato = false; for (size_t j = 0; j < n - i - 1; j++) { fseek(fp, j * sizeof(int), SEEK_SET); if (fread(&valore1, sizeof(int), 1, fp) != 1) { perror("Errore di lettura del file"); fclose(fp); return; } fseek(fp, (j + 1) * sizeof(int), SEEK_SET); if (fread(&valore2, sizeof(int), 1, fp) != 1) { perror("Errore di lettura del file"); fclose(fp); return; } if (valore1 > valore2) { fseek(fp, j * sizeof(int), SEEK_SET); if (fwrite(&valore2, sizeof(int), 1, fp) != 1) { perror("Errore di scrittura nel file"); fclose(fp); return; } fseek(fp, (j + 1) * sizeof(int), SEEK_SET); if (fwrite(&valore1, sizeof(int), 1, fp) != 1) { perror("Errore di scrittura nel file"); fclose(fp); return; } scambiato = true; } } if (!scambiato) { break; } } fclose(fp); } void swap(FILE *fp, int pos1, int pos2, size_t elementSize) { int temp1, temp2; fseek(fp, pos1 * elementSize, SEEK_SET); fread(&temp1, elementSize, 1, fp); fseek(fp, pos2 * elementSize, SEEK_SET); fread(&temp2, elementSize, 1, fp); fseek(fp, pos1 * elementSize, SEEK_SET); fwrite(&temp2, elementSize, 1, fp); fseek(fp, pos2 * elementSize, SEEK_SET); fwrite(&temp1, elementSize, 1, fp); } int partition(FILE *fp, int low, int high, size_t elementSize) { int pivot; fseek(fp, high * elementSize, SEEK_SET); fread(&pivot, elementSize, 1, fp); int i = low - 1; for (int j = low; j < high; j++) { int current; fseek(fp, j * elementSize, SEEK_SET); fread(¤t, elementSize, 1, fp); if (current < pivot) { i++; swap(fp, i, j, elementSize); } } swap(fp, i + 1, high, elementSize); return i + 1; } void fQuickSort(FILE *fp, int low, int high, size_t elementSize) { if (low < high) { int pi = partition(fp, low, high, elementSize); fQuickSort(fp, low, pi - 1, elementSize); fQuickSort(fp, pi + 1, high, elementSize); } } int main(void) { //fSelectionSort("file_prova.bin"); fBubbleSort("giovanni.bin"); return 0; }