2025-02-24 14:33:16 +01:00

57 lines
1.1 KiB
C

#include <iso646.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
// Variables
const int size=8;
int *alloc_tab(){
int *tab=NULL;
tab=malloc(sizeof(int)*size);
tab[0]=1;
tab[1]=2;
tab[2]=4;
tab[3]=5;
tab[4]=6;
tab[5]=7;
tab[6]=9;
tab[7]=10;
return tab;
}
bool dict_sort(int tab[], int cible) {
// Définition et Assignation des variables
int pointer = 1;
int intervale= size;
int pivot;
bool trouve=false;
while (pointer<=intervale && trouve==false) {
printf("Pointer: %d, Intervalle: %d, Pivot: %d \n ",pointer , intervale, pivot);
pivot = (pointer+intervale)/2;
if (tab[pivot]==cible) {
trouve=true;
}
else {
if (cible<tab[pivot]) {
intervale=pivot-1;
}
else {
pointer=pivot+1;
}
}
}
if (trouve==true) {
printf("Found !");
}
else {
printf("Not found !");
}
return trouve;
}
int main(void) {
int* tab=alloc_tab();
dict_sort(tab, 5);
free(tab);
return 0;
}