You are on page 1of 6

// RICERCA LINEARE

int linear_search(double a[], int n, double x){


int i, trovato; for(i = 0; (i < n) && (a[i] != x); i++);
if(i<n) trovato = i; else trovato = −1; return trovato; }

// RICERCA BINARIA O DICOTOMICA


int binary_search(double a[], int n, double x){
int first=0, last=n−1, chosen=(first+last)/2, trovato;
bool flag = true;
while(first <= last && flag){
if(a[chosen] == x){ trovato = chosen; flag = false; }
else{
if(a[chosen] < x) first = chosen+1; else last = chosen−1;
chosen = (first+last)/2; }
} return trovato; }

// SELECTION SORT
double search_min(double a[], int n){
int i, imin=0;
for(i=1;i<n;i++){ if(a[i]<a[imin]) imin=i; }
return imin; }

void swap(double *a, double *b){ double temp = *a; *a = *b; *b = temp; }

void selection_sort(double a[], int n){


int i, imin;
for(i=0;i<n−1;i++){ imin=i+search_min(a+i, n−i); if(imin!=i) swap(&a[i], &a[imin]); } }

// INSERTION SORT
void insert_in_order(double a[], int n, double x){
int pos, i; for(pos=n; pos>0 && a[pos−1]>x; pos−−);
for(i=n−1; i>=pos; i−−){ a[i+1]=a[i]; }
a[pos]=x; }

void insertion_sort(double a[], int n){ int i; for(i=1; i<n; i++){ insert_in_order(a, i, a[i]); } }

// BUBBLE SORT
void swap(double *a, double *b){ double temp = *a; *a = *b; *b = temp; }

void bubble_sort(double a[], int n){


int i, k; bool modified = true;
for(k=0; k<n−1 && modified; k++){
modified = false;
for(i=0;i<n−k−1;i++)
if(a[i]>a[i+1]){ swap(&a[i], &a[i+1]); modified = true; }
} }

// MERGE SORT
void merge(double a1[], int n1, double a2[], int n2, double dest[]){
int pos1=0, pos2=0, k=0;
while(pos1<n1 && pos2<n2){
if(a2[pos2]<a1[pos1]) dest[k++] = a2[pos2++];
else dest[k++] = a1[pos1++]; }
while(pos1<n1){ dest[k++] = a1[pos1++]; }
while(pos2<n2){ dest[k++] = a2[pos2++]; } }

void merge_sort(double a[], int n, double temp[]){


int i, m = n/2;
if(n>1){
merge_sort(a, m, temp); merge_sort(a+m, n−m, temp); merge(a, m, a+m, n−m, temp);
for(i=0;i<n;i++){ a[i] = temp[i]; }
} }

// QUICK SORT
void swap(double *a, double *b){ double temp = *a; *a = *b; *b = temp; }

int partition(double a[], int n){


int i, k=1;
for(i=1;i<n;i++){ if(a[i]<a[0]) swap(&a[i], &a[k++]); }
swap(&a[0], &a[k−1]); return k−1; }

void quick_sort(double a[], int n){


int k;
if(n>1){ k=partition(a, n); quick_sort(a, k); quick_sort(a+k+1, n−k−1);} }
// LISTE −− funzioni iterative

typedef struct m_nodo{ int info; struct m_nodo *link; } t_nodo;


typedef t_nodo *t_lista;

t_nodo *crea_nodo(int valore){ t_nodo *n = malloc(sizeof(t_nodo));


if(n!=NULL){ n−>info = valore; n−>link = NULL; } return n; }

t_lista list_destroy(t_lista l){ t_nodo *curr = l, *succ;


while(curr != NULL){ succ = curr−>link; node_destroy(curr); curr = succ; }
return NULL; }

t_lista list_insert(t_lista l, int info){ t_nodo *prec = NULL, *succ = l, *nuovo;


while(succ != NULL && (info > succ−>info)){ prec = succ; succ = succ−>link; }
nuovo = crea_nodo(info);
if(nuovo != NULL){
if(prec == NULL){ nuovo−>link = l; l = nuovo; }
else{ nuovo−>link = succ; prec−>link = nuovo; }
} return l; }

t_lista liste_remove(t_lista l, int info){ t_nodo *prec = NULL, *curr = l, *alias;


while(curr != NULL && (info > curr−>info)){ prec = curr; curr = curr−>link; }
if(curr != NULL && (curr−>info == info)){
if(prec == NULL) l = curr−>link;
else{ alias = curr−>link; prec−>link = alias; }
node_destroy(curr);
} return l; }

// ARRAY DINAMICI −− resize con espansione geometrica

#define GROWING_FACTOR 2
#define SHRINKING_FACTOR 4
typedef struct SArray{ double *item; int lenght; int size; }TArray;

TArray array_create(int initial_lenght){


TArray a; a.item = malloc(initial_lenght*sizeof(double));
if(initial_lenght>=0 && a.item!=NULL){ a.lenght=a.size=initial_lenght; }
return a; }

void array_destroy(TArray *a){ free(a−>item); a−>item = NULL; a−>lenght = a−>size = 0; }

void array_resize(TArray *a, int new_lenght){


if(new_lenght>a−>size || new_lenght<a−>size/SHRINKING_FACTOR){
int new_size = new_lenght*GROWING_FACTOR;
a−>item = realloc(a−>item, new_size*sizeof(double));
if(new_size>0 && a−>item!=NULL){ a−>size=new_size; a−>lenght=new_lenght; } } }

// PILE −− funzioni iterative

#define CAPACITY 100


typedef struct SStack{ int n; double a[CAPACITY]; }TStack;
TStack stack_create(){ TStack s; s.n = 0; return s; }
void stack_destroy(TStack *stack){ stack−>n = 0; }
void stack_push(TStack *stack, double x){ stack−>a[stack−>n++] = x; }
double stack_pop(TStack *stack){ double x = stack−>a[stack−>n−1]; stack−>n−−; return x; }
double stack_top(TStack *stack){ return stack−>a[stack−>n−1]; }
bool stack_is_empty(TStack *stack){ return stack−>n == 0; }
bool stack_is_full(TStack *stack){ return stack−>n == CAPACITY; }

// CODE −− gestione non circolare dell’array −− funzioni iterative

typedef struct SQueue{ int elem_usati; int front; int back; int capacity; double *a; }TQueue;
TQueue queue_create(int capacity){ TQueue s; s.elem_usati=s.front=s.back=0; s.capacity=capacity;
s.a=malloc(sizeof(double)*capacity); return s; }
void queue_destroy(TQueue *queue){ queue−>elem_usati=queue−>capacity=0; free(queue−>a); }
void queue_add(TQueue *queue, double x){ queue−>a[queue−>back]=x; queue−>back++; queue−>elem_usati++; }
double queue_remove(TQueue *queue){ double x=queue−>a[queue−>front];
queue−>front=(queue−>front+1)%queue−>capacity; queue−>elem_usati−−; return x; }
double queue_front(TQueue *queue){ return queue−>a[queue−>front]; }
bool queue_is_empty(TQueue *queue){ return queue−>elem_usati==0; }
bool queue_is_full(TQueue *queue){ return queue−>elem_usati==queue−>capacity; }
// ALBERI RICORSIVE

typedef struct m_nodo{ int info; struct m_nodo *left; struct m_nodo *right; }t_nodo;
typedef t_nodo *t_albero;

t_albero bt_destroy(t_albero a){ // DISTRUGGI ALBERO


if(a−>left == NULL && a−>right == NULL){ node_destroy(a); }
else if(a−>right == NULL){ a−>left = bt_destroy(a−>left); node_destroy(a−>left); }
else if(a−>left == NULL){ a−>right = bt_destroy(a−>right); node_destroy(a−>right); }
else{ a−>left = bt_destroy(a−>left); a−>right = bt_destroy(a−>right); node_destroy(a); }
return NULL; }

t_albero bt_insert(t_albero a, int info){ // INSERISCI ELEMENTO


if(a == NULL){ t_nodo *n = crea_nodo(info); if(n != NULL) a = n; }
else if(info > a−>info){ a−>right = bt_insert(a−>right, info); }
else if(info < a−>info){ a−>left = bt_insert(a−>left, info); }
return a; }

t_albero search(t_albero a, int info){ // CERCA ELEMENTO


if(a == NULL) return NULL;
else if(a−>info > info) a = search(a−>left, info);
else if(a−>info < info) a = search(a−>right, info);
else return a; }

t_albero search_min(t_albero a){ // CERCA MINIMO


if(a == NULL) return NULL;
else if(a−>left == NULL) return a;
else{ t_albero temp = search_min(a−>left); return temp; } }

t_albero delete(t_albero a, int info){ // ELIMINA ELEMENTO


if(a == NULL) return NULL;
else if(a−>info > info){ a−>left = delete(a−>left, info); return a; }
else if(a−>info < info){ a−>right = delete(a−>right, info); return a; }
else{
if(a−>right == NULL && a−>left == NULL){ node_destroy(a); return NULL; }
else if(a−>right == NULL){ t_albero temp = a−>left; node_destroy(a); return temp; }
else if(a−>left == NULL){ t_albero temp = a−>right; node_destroy(a); return temp; }
t_albero min_right = search_min(a−>right);
a−>info = min_right−>info;
a−>right = delete(a−>right, min_right−>info);
return a; } }

int conta_foglie(t_albero a){ // N.B.: simile a somma info nodi; sostituire con commenti
if(a == NULL) return 0;
else if(a−>left == NULL && a−>right == NULL) return 1; // return a−>info
else{
int f_left = conta_foglie(a−>left); int f_right = conta_foglie(a−>right);
return f_left + f_right; /* + a−>info */ } }

int conta_nodi(t_albero a){


if(a == NULL) return 0;
else if(a−>left == NULL && a−>right == NULL) return 1;
else{
int n_left = conta_nodi(a−>left); int n_right = conta_nodi(a−>right);
return 1 + n_left + n_right; } }

int altezza(t_albero a){


if(a == NULL) return 0;
else if(a−>left == NULL && a−>right == NULL) return 0;
else{
int h_left = altezza(a−>left); int h_right = altezza(a−>right);
if(h_left > h_right) return 1 + h_left; else return 1 + h_right; } }

void stampa_albero(t_albero a){


if(a != NULL){ stampa_albero(a−>left); printf("%d ", a−>info); stampa_albero(a−>right); }
}
// ALBERI ITERATIVE

t_albero *search(t_albero t, int info){


while((t != NULL) && (t−>info != info)){
if(t−>info > info) t = t−>left;
else t = t−>right; }
return t; }

t_albero bt_insert(t_albero t, int info){


t_albero new, prec = NULL, curr = t; int goleft = 0;
while(curr != NULL){
prec = curr; goleft = (info > curr−>info);
if(goleft) curr = curr−>left;
else curr = curr−>right;
}
new = crea_nodo(info);
if(new != NULL){
if(prec == NULL) return new;
else if(goleft) prec−>left = new;
else prec−>right = new;
} return t; }

t_albero bt_remove(t_albero t, int info){


t_albero curr = t, prec = NULL, min_right; int goleft = 0;
while ((curr != NULL) && (curr−>info != info)){
if (curr−>info > info){ prec = curr; curr = curr−>left; goleft = 1; }
else{ prec = curr; curr = curr−>right; goleft = 0; }
}
if (curr == NULL) return t;
else if (curr−>left == NULL && curr−>right == NULL){
node_destroy(curr);
if (goleft == 1) prec−>left = NULL;
else prec−>right = NULL;
return t;
}
else if (curr−>right == NULL){
t_albero alias = curr−>left;
node_destroy(curr);
if (goleft == 1) prec−>left = alias;
else prec−>right = alias;
return t;
}
else if (curr−>left == NULL){
t_albero alias = curr−>right;
node_destroy(curr);
if (goleft == 1) prec−>left = alias;
else prec−>right = alias;
return t;
}
min_right = search_min(curr−>right);
curr−>info = min_right−>info; prec = curr; curr = curr−>right;
if(curr−>left == NULL){ t_albero alias = curr−>right; node_destroy(curr); prec−>right = alias; }
else{
while (curr−>left != NULL){ prec = curr; curr = curr−>left; }
prec−>left = NULL; node_destroy(curr);
} return t; }
// T−HASH IND. CHIUSO

typedef struct m_nodo{ int key; int value; struct m_nodo *link; }t_nodo;
typedef t_nodo *t_lista;
typedef struct m_THash{ int bucket_number; t_lista *bucket; }t_THash;

t_nodo *crea_nodo(int key, int value){


t_nodo *n = malloc(sizeof(t_nodo));
if(n != NULL) { n−>key = key; n−>value = value; n−>link = NULL; }
return n; }

unsigned hash(int key){ return (unsigned)key; }

t_THash *crea_tabella(int bucket){


int i; t_THash *p = malloc(sizeof(t_THash));
if(p != NULL && bucket > 0){
p−>bucket_number = bucket; p−>bucket = malloc(sizeof(t_lista)*bucket);
if(p−>bucket != NULL)
for(i=0; i<bucket; i++) p−>bucket[i] = list_create();
} return p; }

void distruggi_tabella(t_THash *t){


int i;
for(i=0; i<t−>bucket_number; i++){ t−>bucket[i] = list_destroy(t−>bucket[i]); }
free(t−>bucket); t−>bucket_number = 0; free(t); }

void table_insert(t_THash *t, int key, int value){


unsigned h = hash(key) % t−>bucket_number;
t_nodo *prec = NULL, *succ = t−>bucket[h], *nuovo;
while(succ != NULL && (succ−>key < key)){ prec = succ; succ = succ−>link; }
nuovo = crea_nodo(key, value);
if(nuovo != NULL){
if(prec == NULL){ nuovo−>link = t−>bucket[h]; t−>bucket[h] = nuovo; }
else{ nuovo−>link = succ; prec−>link = nuovo; } } }

void table_remove(t_THash *t, int key){


unsigned h = hash(key) % t−>bucket_number;
t_lista prec = NULL, temp = t−>bucket[h];
bool check = false;
while(temp != NULL && check == false){
if(temp−>key < key){ prec = temp; temp = temp−>link; }
else if(temp−>key == key){
prec = temp−>link;
free(temp);
check = true; }
else{
printf("Valore non presente");
check = true; } } }

void stampa_tabella(t_THash *t){


int max = t−>bucket_number, i; t_lista temp;
for(i=0; i<max; i++){
printf("\n\nBucket[%d]:",i); temp = t−>bucket[i];
while(temp != NULL){
printf("\n [key]:%d [value]:%d", temp−>key, temp−>value);
temp = temp−>link; } } }
// T−HASH IND. APERTO

#define MAX_LOAD 0.75


#define GROW_FACTOR 2
typedef struct m_bucket{ int key; int value; bool used; }t_bucket;
typedef struct m_THash{ int bucket_number; t_bucket *bucket; int used_buckets; }t_THash;

t_THash *crea_tabella(int buckets){ int i; t_THash *p = malloc(sizeof(t_THash));


if(p != NULL && buckets > 0){
p−>bucket_number = buckets;
p−>bucket = malloc(sizeof(t_THash)*buckets);
if(p−>bucket != NULL){
for(i=0; i<buckets; i++){ p−>bucket[i].used = false; }
p−>used_buckets = 0;
} } }

void distuggi_tabella(t_THash *t){ free(t−>bucket); t−>bucket_number = 0; t−>used_buckets = 0; free(t); }

int *search(t_THash *t, int key){ unsigned h = hash(key) % t−>bucket_number;


while(t−>bucket[h].used && key != t−>bucket[h].key){
h = (h+1) % t−>bucket_number; }
if(t−>bucket[h].used) return &t−>bucket[h].value;
else return NULL; }

void table_resize(t_THash *t, int new_buckets){


int i;
if(new_buckets > t−>bucket_number){
t_bucket *p = malloc(sizeof(t_bucket)*new_buckets);
t_bucket *old = t−>bucket;
int old_buckets = t−>bucket_number;
if(p != NULL){
t−>bucket = p; t−>bucket_number = new_buckets; t−>used_buckets = 0;
for(i=0; i<new_buckets; i++){ p[i].used = false; }
for(i=0; i<old_buckets; i++){
if(old[i].used) table_insert(t, old[i].key, old[i].value); }
} free(old); } }

void table_insert(t_THash *t, int key, int value){


int *pvalue = search(t, key);
if(pvalue != NULL){ *pvalue = value; return; }
if((t−>used_buckets + 1) >= (MAX_LOAD * t−>bucket_number)){
int new_buckets = GROW_FACTOR * t−>bucket_number;
if(new_buckets <= t−>bucket_number){ new_buckets = t−>bucket_number + 1; }
table_resize(t, new_buckets);
}
unsigned h = hash(key) % t−>bucket_number;
while(t−>bucket[h].used){ h = (h+1) % t−>bucket_number; }
t−>bucket[h].key = key; t−>bucket[h].value = value;t−>bucket[h].used = true; t−>used_buckets++; }

void table_remove(t_THash *t, int key){


unsigned h = hash(key) % t−>bucket_number;
while(t−>bucket[h].used && key != t−>bucket[h].key){ h = (h+1) % t−>bucket_number; }
if(t−>bucket[h].used){
unsigned hole = h; t−>bucket[hole].used = false; t−>used_buckets−−;
h = (h+1) % t−>bucket_number;
while(t−>bucket[h].used){
unsigned h1 = hash(t−>bucket[h].key) % t−>bucket_number; bool movable;
if(h > hole) movable = (h1 <= hole || h1 > h);
else movable = (h1 <= hole && h1 > h);
if(movable){
t−>bucket[hole] = t−>bucket[h];
hole = h;
t−>bucket[hole].used = false;
}
h = (h+1) % t−>bucket_number;
} } }

You might also like