Last active
May 1, 2018 12:55
-
-
Save unaipme/aaeb766a7e93ce8bffc660464cd87ede to your computer and use it in GitHub Desktop.
Ordenazio algoritmoak
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "algoritmoak.h" | |
void arrunta(int *zerrenda, int dim) { | |
int i, j; | |
for (i = 0; i < dim; i++) { | |
for (j = i; j < dim; j++) { | |
if (*(zerrenda + i) > *(zerrenda + j)) { | |
swap(zerrenda + 1, zerrenda + i); | |
} | |
} | |
} | |
} | |
void arrunta_hobeto(int *zerrenda, int dim) { | |
int i; | |
int *txikiena; | |
txikiena = zerrenda; | |
for (i = 0; i < dim; i++) { | |
if (*txikiena > *(zerrenda + i)) { | |
txikiena = zerrenda + i; | |
} | |
} | |
if (txikiena != zerrenda) { | |
swap(txikiena, zerrenda); | |
} | |
if (dim > 2) arrunta_hobeto(zerrenda + 1, dim - 1); | |
} | |
void burbuila(int *zerrenda, int dim) { | |
int i; | |
boolean aldaketak; | |
do { | |
aldaketak = false; | |
for (i = 0; i < dim - 1; i++) { | |
if (*(zerrenda + i) > *(zerrenda + i + 1)) { | |
swap(zerrenda + i, zerrenda + i + 1); | |
aldaketak = true; | |
} | |
} | |
} while (aldaketak); | |
} | |
void burbuila_hobetua(int *zerrenda, int dim) { | |
int i, j; | |
boolean aldaketak; | |
for (i = 0; i < dim; i++) { | |
} | |
} | |
void sartzez_ordenatzea(int *zerrenda, int dim) { | |
int k, i; | |
for (k = dim - 1; k >= 0; k--) { | |
int gorde = *(zerrenda + k); | |
for (i = k + 1; i < dim; i++) { | |
if (*(zerrenda + i) < gorde) { | |
*(zerrenda + i - 1) = *(zerrenda + i); | |
*(zerrenda + i) = gorde; | |
} | |
} | |
} | |
} | |
void shellsort(int *zerrenda, int dim) { | |
int k = dim / 2; | |
int i; | |
while (k >= 1) { | |
for (i = k; i < dim; i++) { | |
int gorde = zerrenda[i]; | |
int j; | |
for (j = i; j >= k; j -= k) { | |
if (zerrenda[j - k] > gorde) { | |
zerrenda[j] = zerrenda[j - k]; | |
} | |
else break; | |
} | |
zerrenda[j] = gorde; | |
} | |
k /= 2; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef SORTING_ALGORITMOAK_H | |
#define SORTING_ALGORITMOAK_H | |
#include "main.h" | |
void arrunta(int *, int); | |
void arrunta_hobeto(int *, int); | |
void burbuila(int *, int); | |
void burbuila_hobetua(int *, int); | |
void sartzez_ordenatzea(int *, int); | |
void shellsort(int *, int); | |
#endif //SORTING_ALGORITMOAK_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "main.h" | |
int main() { | |
srand(time(NULL)); | |
int aukera; | |
do { | |
aukera = menu(); | |
if (aukera == 1) { | |
sortuZerrenda(); | |
} else if (aukera == 2) { | |
ordenatu(arrunta); | |
} else if (aukera == 3) { | |
ordenatu(arrunta_hobeto); | |
} else if (aukera == 4) { | |
ordenatu(burbuila); | |
} else if (aukera == 5) { | |
ordenatu(burbuila_hobetua); | |
} else if (aukera == 6) { | |
ordenatu(sartzez_ordenatzea); | |
} else if (aukera == 7) { | |
ordenatu(shellsort); | |
} else if (aukera != 0) { | |
printf("Aukera hori ez da existitzen"); | |
} | |
} while (aukera != 0); | |
getchar(); | |
return 0; | |
} | |
void swap(int *v1, int *v2) { | |
int aux; | |
aux = *v1; | |
*v1 = *v2; | |
*v2 = aux; | |
} | |
int menu() { | |
int n; | |
printf("1.- Sortu zenbaki zerrenda\n"); | |
printf("2.- Metodo arrunta\n"); | |
printf("3.- Metodo arrunta hobetua\n"); | |
printf("4.- Burbuila\n"); | |
printf("5.- Burbuila hobetua\n"); | |
printf("6.- Sartzez bidezkoa\n"); | |
printf("7.- ShellSort\n"); | |
printf("0.- Irten\n"); | |
printf("Aukeratu bat: "); | |
scanf("%d", &n); | |
return n; | |
} | |
int zerrendaIrakurri(int *zerrenda) { | |
char izena [128]; | |
FILE *f; | |
printf("Sartu fitxategiaren izena: "); | |
scanf("%s", izena); | |
if ((f = fopen(izena, "rb")) == NULL) { | |
printf("Arazoa izan da fitxategia irekitzen."); | |
return -1; | |
} else { | |
int dim = fread(zerrenda, sizeof(int), 50000, f); | |
fclose(f); | |
return dim; | |
} | |
} | |
void sortuZerrenda() { | |
int n, i; | |
char izena[128]; | |
FILE *f; | |
printf("Sartu zenbaki kopurua: "); | |
scanf("%d", &n); | |
sprintf(izena, "zerrenda%d.dat", n); | |
int *zerrenda = calloc(n, sizeof(int)); | |
for (i = 0; i < n; i++) { | |
*(zerrenda + i) = rand(); | |
} | |
f = fopen(izena, "wb"); | |
fwrite(zerrenda, sizeof(int), (size_t) n, f); | |
fclose(f); | |
} | |
void bistaratuHamarLehenak(int *zerrenda) { | |
int i; | |
for (i = 0; i < 10; i++) { | |
printf("%d\n", *(zerrenda + i)); | |
} | |
} | |
void bistaratuHamarAzkenak(int *zerrenda, int dim) { | |
int i; | |
for (i = 0; i < 10; i++) { | |
printf("%d\n", *(zerrenda + dim - 10 + i)); | |
} | |
} | |
void emaitzakAurkeztu(int *zerrenda, int dim, double denbora) { | |
printf("Lehenengo hamar zenbakiak: \n"); | |
bistaratuHamarLehenak(zerrenda); | |
printf("\nAzkeneko hamar zenbakiak: \n"); | |
bistaratuHamarAzkenak(zerrenda, dim); | |
printf("\nDenbora guztira: %.3f\n", denbora); | |
} | |
void ordenatu(void (*f)(int *, int)) { | |
int *zerrenda = calloc(50000, sizeof(int)); | |
int dim = zerrendaIrakurri(zerrenda); | |
if (dim == -1) return; | |
printf("Ordenatu gabe: \n"); | |
clock_t hasiera = clock(); | |
(*f)(zerrenda, dim); | |
clock_t bukaera = clock(); | |
emaitzakAurkeztu(zerrenda, dim, ((double) (bukaera - hasiera)) / CLOCKS_PER_SEC); | |
printf("Ordenatuta: \n"); | |
hasiera = clock(); | |
(*f)(zerrenda, dim); | |
bukaera = clock(); | |
emaitzakAurkeztu(zerrenda, dim, ((double) (bukaera - hasiera)) / CLOCKS_PER_SEC); | |
free(zerrenda); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef SORTING_MAIN_H | |
#define SORTING_MAIN_H | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#include "algoritmoak.h" | |
typedef enum {false, true} boolean; | |
void swap(int *, int *); | |
int menu(); | |
void ordenatu(void (*f)(int *, int)); | |
int zerrendaIrakurri(int *); | |
void sortuZerrenda(); | |
void bistaratuHamarLehenak(int *); | |
void bistaratuHamarAzkenak(int *, int); | |
void emaitzakAurkeztu(int *, int, double); | |
#endif //SORTING_MAIN_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment