0

I am having trouble with a binary search on strings in c. I use the strcmp function to compare the strings, but I still get no output when I type in a name that I know is in the list.

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#define MAX_STRING_LEN 25

void insert_sata(char **strings, const char* filename, int size);
void allocate( char ***strings, int size);
int binary_search(char **strings, char *target, int start_idx, int end_idx);

int main(int argc, char* argv[]){

    if(argc != 4){
        printf("Wrong number of args");
    }

    char **pointer;
    int size = atoi(argv[1]);


    allocate(&pointer, size);
    insert_data(pointer, argv[2], size);

    int x;
    int z = 1;
    char search_name[MAX_STRING_LEN];

    while( z == 1){
        printf("\nEnter a name to search for: ");
        scanf("%s", search_name);
        x = binary_search(pointer, search_name, 0, size);
        printf("\nContinue searching names? ( 1 = yes, 0 = No):");
        scanf("%d", &z);
    }
}



void allocate(char ***strings, int size){

    int i;
    *strings =  malloc(sizeof(**strings) * size);
    for( i = 0; i < size; i++)
    {
        (*strings)[i] = malloc(sizeof(char) * MAX_STRING_LEN);
    }
}

void insert_data(char **strings, const char* filename, int size){

    FILE *input;
    input = fopen(filename, "r");

    int i;
    for (i = 0; i < size; i++){

        fscanf(input,"%24s", strings[i]);

    }

    fclose(input);
}

int binary_search(char **strings, char *target, int start_idx, int end_idx){

    int result;
    int mid_idx = 0;

    while( end_idx >= start_idx){
        mid_idx = (end_idx + start_idx) / 2;

        result = strcmp(strings[mid_idx], target);

        if(result > 0){
            end_idx = start_idx - 1;
        }
        else if (result < 0){
            start_idx = mid_idx + 1;
        }

        else if( result == 0){
            printf("%s was found in the set", target);

        }
    }
}

The binary search is the function that is giving me trouble. I do not recieve any seg faults or anything, just nothing is displayed when I search a name that is in the file. Here is the list of names that I scan into the program.

  • matt
  • susan
  • mark
  • david
  • aden
  • phil
  • erik
  • john
  • caden
  • mycah

3 Answers 3

3

Your input list isn't sorted and your program doesn't seem to try to sort it. Suppose you look for 'susan' - first comparision is 'susan' to 'aden', and the search area gets narrowed to last 5 items, while 'susan' is at the second position......

Sign up to request clarification or add additional context in comments.

1 Comment

gotcha, Thanks a lot I didnt know that it had to be sorted, Ill get right on that!
0

This:

if (result > 0) {
   end_idx = start_idx - 1;
}

is probably mean to be:

if (result > 0) {
  end_idx = mid_idx - 1;
}

1 Comment

Yes, you are right in that. dumb mistake on my part, but it still didnt fix my problem. I search for matt, yet nothing is printed out. Probably a dumb mistake that I cant figure out, but this is KILLING me.
0

The binary search algorithm requires that the list is sorted. Your example list isn't, so the algorithm will not work

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.