Problem in combined code of bubble sort and binary search of an array in C

Viewed 1037

I was trying to sort a user input array using bubble sort and then do binary search to find a certain key.

However each code works fine on its own, but when I combine them binary search doesn't work: when I enter the search key it sorts the arrays and gives output but the key is always not found. I tried to add a break statement after the first if in while but it didn't work either

#include <stdio.h>

int main(void) {
    int arr[100];
    int temp, size ,first, last, middle ,search,found,result;

    printf("Enter the array size: ");
    scanf("%d",&size);
    printf("Enter the array elements: ");

    for(int i = 0 ; i <size ;++i )
    {
        scanf("%d",&arr[i]);
    }
    printf("Enter search value: ");
    scanf("%d", &search);

    for(int i = 0 ; i<size ; ++i)
        for(int j= 0; j<size-1; j++)
        {
            if(arr[j+1]>arr[j])
            {
                temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }

    printf("Elements of the array sorted in descending order: ");
    for(int i = 0;i <size ;++i)
    {
        printf("%d ", arr[i]);
    }

    printf("\n");
    printf("Number of passes is: %d", size -1);

    first = 0;
    last = size- 1;
    found = 0;

    while (!found &&first <= last) {
        middle = (first+last)/2;

        if ( search == arr[middle] )
        {
            found = 1 ;
            result = middle;
            break;
        }
        else if(search < arr[middle])
        {
            last = middle - 1;
        }

        else
            first = middle +1;
    }

    if (found)
    {
        printf("\n");
        printf("%d found at location %d.\n", search, result);
    }
    else 
    {
        printf("\n");
        printf("%d is not found", search);
    }

    return 0;
}
1 Answers

Replace the less than operator (<) to greater than operator (>) since the larger elements are on the left of middle.

else if(search < arr[middle]) //replace '<' to '>'
{
    last = middle - 1;
}
Related