hi, below is the code for binary search in case of array is sorted in ascending order.
what changes i have to do if array is sorted in descending order.

#include<iostream>
using namespace std;

void getdata (int a[], int size);
int binarysearch (int a[], int size, int key);

int main()
{

    int size=10;
    int a[size];

    getdata (a, size);

    int key=11;
    int search=binarysearch (a, size, key);

    if (search==-1)
        cout<<"sorry, key is not found.";
    else
        cout<<"key is found at position " <<search <<endl;

return 0;
}

void getdata (int a[], int size)
{
    for (int i=0; i<size; i++)
        cin>>a[i];
}

int binarysearch (int a[], int size, int key)
{
    int first=0;
    int last=size-1;
    int mid;
    bool found=false;

        while (first<=last && !found)
            {
            mid=(first+last)/2;
            if (a[mid]==key)
                found=true;
            else if (a[mid]>key)
                last=mid-1;
            else
            first=mid+1;
            }
        if (found)
            return mid;
        else
            return -1;
}

Recommended Answers

All 6 Replies

You need to think about the problem and how binary searches work.

If you understand how binary searches work, then the answer is obvious. The fact you ask this question means you don't grasp the issue, so rather than answering the immediate question, it would be better if you understood the underlying issue and then applied your understanding to the code.

write a binary search progream thate can search items sorted in descnding order

//binary search using bubble sort....
#include<stdio.h>
#include<conio.h>
int main()
{
     int la[20],i,ub,temp,k,lb=0;
     printf("\n enter how many elements in array:");   //enter and print array                
     scanf("%d",&ub);
     for(i=lb;i<ub;i++)
     {
                        printf("\n enter %d element of array:",i);
                        scanf("\n %d",&la[i]);
     }
     printf("\n elements of array are:");
     for(i=lb;i<ub;i++)
     {
                        printf("\n la[%d]=%d",i,la[i]);
     }
    i=0;
    while(i<ub-1)
    {
                 k=0;
                 while(k<ub-i-1)
                 {
                      if(la[k]>la[k+1])
                      {
                         temp=la[k];
                         la[k]=la[k+1];
                         la[k+1]=temp;
                      }
                      k++;
                 }
                 i++;
    }
     printf("\n after sorting elements of array are:");
     for(i=lb;i<ub;i++)
     printf("\n [%d]=%d",i,la[i]);   
     //logic of bubble sort ends here
     //:::------------------------------------------------------------>

     //core logic of binary search starts here
     int beg=0,end=ub,key,mid=(beg+end)/2,loc=-1;
     printf("\n enter the element you wanna search:");
     scanf("%d",&key);
     while((beg<=end)&&(key!=la[mid]))   
     {
                       mid=(beg+end)/2;
                       if(key==la[mid])
                       {
                        printf("\n search is sucessful");
                        loc=mid;
                        printf("\n element possion is %d in array",loc+1);
                       }
                       else
                       {
                           if(key<la[mid])
                                      end=mid-1;
                           else
                                      beg=mid+1;
                       }
     }
     if(loc<0)
          printf("\n entered element doesn't exist into array!!!");
     getch();
     return 0;
}

Yes the code is working. Thank you

Both solutions are brain-dead... Use bsearch() and utilize a different compare() function for ascending vs descending orders. That way, you only need to tell the program how the list is sorted, and it can call bsearch() with the appropriate compare() function. Simple, effective, and fast.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.