Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <time.h>
- float aux;
- void starton(void){
- aux = clock();
- }
- float startoff(void){
- return(clock() - (float)aux) / CLOCKS_PER_SEC; // didn't work on my machine with clk_tck
- }
- typedef struct Lista{
- int ID,nr;
- char Name[100];
- } NOD;
- NOD vals[100];
- int LINEARSRCH(int x,NOD *vals, int n){
- int i=0;
- while ((i < n - 1) && (vals[i].ID != x))
- i++;
- if (vals[i].ID != x){
- printf("The array doesn't contain the searched element\n");
- return 0;
- }
- else{
- printf("The element was found at index %d \n", i);
- return i;
- }
- }
- int LINEARSRCHSAN(int x,NOD *vals, int n){
- int i = 0;
- vals[n].ID = x;
- while (vals[i].ID != x)
- i++;
- if (i == n ){
- printf("The array doesn't contain the searched element\n");
- return 0;
- }
- else{
- printf("The element was found at index %d \n", i+1);
- return i;
- }
- }
- int main(){
- int i = 0;
- FILE *fp = fopen("FisierCORalfabetic.txt","r");
- if (fp == NULL){
- printf("The file cannot be opened \n");
- return 0;
- }
- while (!feof(fp)){
- fscanf(fp,"%d", &vals[i].nr);
- fscanf(fp,"%d", &vals[i].ID);
- fgets(vals[i].Name, 90,fp);
- i++;
- }
- starton();
- LINEARSRCH(14,vals,i-1);
- aux = startoff();
- printf("Durata liniar search: %f\n",aux);
- starton();
- LINEARSRCHSAN(14,vals,i-1);
- aux = startoff();
- printf("Durata liniar search santinel: %f",aux);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement