Author Topic: Can same no. be searched at two diff. positions using binary search?  (Read 4437 times)

0 Members and 1 Guest are viewing this topic.

Offline cool98765

  • Alumni
  • Super Member
  • *
  • Points: 2012
  • Posts: 962
  • Knowledge helps. No matter from where it comes.

    • MSIT
    • _
      ECE
    • View Profile
I made this code for binary search .. it works fine if the no. to be searched is entered only once.. but if it is entered twice.. it gives the position of only one of them.. How can I get the position of both of them?
 
p.s. I know ki break nahi lagana.. its just because loop infinite na ho jaye.. if I get a solution to my problem.. I can remove the break..

 
#include<stdio.h>
#include<conio.h>
void main()
{
 int first,last,n,i,a[20],m,x;
 clrscr();
 printf("Enter the no. of elements\n");
 scanf("%d",&n);
 printf("Enter the elements in ascending order\n");
 for(i=0;i<n;i++)
 {
  scanf("%d",&a);
 }
 printf("Enter the element to be searched\n");
 scanf("%d",&m);
 first=0;
 last=n-1;
 x=(first+last)/2;
 while(first<=last)
 {if(m>a)
   first=x+1;
  if(m<a)
     last=x-1;
  else
  {
   printf("No. %d found at position %d",m,x+1);
   break;
  }
  x=(first+last)/2;
}
if(first>last)
printf("Number not found");
getch();
}
« Last Edit: August 28, 2013, 04:41:17 pm by cool98765 »


Maharaja Surajmal Institute of Technology
ECE (2012-2016)
You do not have permission to give points
point 0 Points

This topic did not receive points.

Offline cool98765

  • Alumni
  • Super Member
  • *
  • Points: 2012
  • Posts: 962
  • Knowledge helps. No matter from where it comes.

    • MSIT
    • _
      ECE
    • View Profile
Re: Can same no. be searched at two diff. positions using binary search?
« Reply #1 on: August 28, 2013, 07:59:23 pm »
Matlab ab koi help nahi karega?  :| :((


Maharaja Surajmal Institute of Technology
ECE (2012-2016)

Offline XYLS

  • The Awesomeness Deployer™
  • Alumni
  • Super Member
  • *
  • Points: 3014
  • Posts: 1463

    • NIEC
    • _
      Not Specified
    • View Profile
    • Coding Bot


Offline Champ

  • KilLeR
  • Alumni
  • Super Member
  • *
  • Points: 1893
  • Posts: 655
  • KilLeR

    • MAIT
    • _
      CSE
    • View Profile
Re: Can same no. be searched at two diff. positions using binary search?
« Reply #3 on: August 28, 2013, 09:59:46 pm »
bhai it can..
add a condition for if m=a
jo part nahi hua execute m=a milte e wo part execute hona shuru ho jaye aur ye wali value print ho jaye...
I gave u the logic, now try to code yourself, that will help you :)
  JAAT BOY..
Be Unstoppable

Offline cool98765

  • Alumni
  • Super Member
  • *
  • Points: 2012
  • Posts: 962
  • Knowledge helps. No matter from where it comes.

    • MSIT
    • _
      ECE
    • View Profile
Re: Can same no. be searched at two diff. positions using binary search?
« Reply #4 on: August 28, 2013, 10:53:56 pm »
Here's the solution, check out this code here:

http://www.codingbot.net/2013/03/binary-search-in-array-algorithm-and-c.html

this one is just the same as mine,, doesn't work  :|


Maharaja Surajmal Institute of Technology
ECE (2012-2016)

Offline Champ

  • KilLeR
  • Alumni
  • Super Member
  • *
  • Points: 1893
  • Posts: 655
  • KilLeR

    • MAIT
    • _
      CSE
    • View Profile
Re: Can same no. be searched at two diff. positions using binary search?
« Reply #5 on: August 28, 2013, 10:55:43 pm »

this one is just the same as mine,, doesn't work  :|


bhai i told you how to do it... do as i said...
  JAAT BOY..
Be Unstoppable

Offline cool98765

  • Alumni
  • Super Member
  • *
  • Points: 2012
  • Posts: 962
  • Knowledge helps. No matter from where it comes.

    • MSIT
    • _
      ECE
    • View Profile
Re: Can same no. be searched at two diff. positions using binary search?
« Reply #6 on: August 28, 2013, 10:55:53 pm »
bhai it can..
add a condition for if m=a
jo part nahi hua execute m=a milte e wo part execute hona shuru ho jaye aur ye wali value print ho jaye...
I gave u the logic, now try to code yourself, that will help you :)

Bhai I made this.. but isme halka sa panga hai.. check this out:
 
Code:
Only registered users can see contents. Please click here to Register or Login.
« Last Edit: September 22, 2013, 12:43:25 am by cool98765 »


Maharaja Surajmal Institute of Technology
ECE (2012-2016)

Offline cool98765

  • Alumni
  • Super Member
  • *
  • Points: 2012
  • Posts: 962
  • Knowledge helps. No matter from where it comes.

    • MSIT
    • _
      ECE
    • View Profile
Re: Can same no. be searched at two diff. positions using binary search?
« Reply #7 on: August 28, 2013, 10:56:41 pm »
Dono dots ko ignore krke baki ka code Dekhnaa..


Maharaja Surajmal Institute of Technology
ECE (2012-2016)

Offline Champ

  • KilLeR
  • Alumni
  • Super Member
  • *
  • Points: 1893
  • Posts: 655
  • KilLeR

    • MAIT
    • _
      CSE
    • View Profile
Re: Can same no. be searched at two diff. positions using binary search?
« Reply #8 on: August 28, 2013, 11:02:18 pm »
bhai maine tera pura code padha b nahi :P
pata nhi kya kya bhar dia...
sun ek kaam kar.. search karne wale block ko ek function bana le... and call it recursively until first is equal to last and change the conditions accordingly... :)
that will work perfectly
  JAAT BOY..
Be Unstoppable

Offline Champ

  • KilLeR
  • Alumni
  • Super Member
  • *
  • Points: 1893
  • Posts: 655
  • KilLeR

    • MAIT
    • _
      CSE
    • View Profile
Re: Can same no. be searched at two diff. positions using binary search?
« Reply #9 on: August 28, 2013, 11:26:26 pm »
here is a code i made...
It may have little errors because i did not compiled it.
Rectify them and it will work great for your problem.
  JAAT BOY..
Be Unstoppable

Offline cool98765

  • Alumni
  • Super Member
  • *
  • Points: 2012
  • Posts: 962
  • Knowledge helps. No matter from where it comes.

    • MSIT
    • _
      ECE
    • View Profile
Re: Can same no. be searched at two diff. positions using binary search?
« Reply #10 on: August 28, 2013, 11:36:57 pm »
here is a code i made...
It may have little errors because i did not compiled it.
Rectify them and it will work great for your problem.

Nahi chal raha bhai tera..
 
this is what I entered:
 
array of 7: 1,2,3,5,5,5,5 .. find 5.. it showed only position 3(even that is wrong)
 
p.s. made the corrections .. attachment


Maharaja Surajmal Institute of Technology
ECE (2012-2016)

Offline cool98765

  • Alumni
  • Super Member
  • *
  • Points: 2012
  • Posts: 962
  • Knowledge helps. No matter from where it comes.

    • MSIT
    • _
      ECE
    • View Profile
Re: Can same no. be searched at two diff. positions using binary search?
« Reply #11 on: August 28, 2013, 11:38:34 pm »
bhai maine tera pura code padha b nahi :P
pata nhi kya kya bhar dia...
sun ek kaam kar.. search karne wale block ko ek function bana le... and call it recursively until first is equal to last and change the conditions accordingly... :)
that will work perfectly

And code padh le bhai :P
neeche ke recursive wale do functions check krde bas.. (aagehai aur peechehai ke naam se banaye hain maine wo :P)


Maharaja Surajmal Institute of Technology
ECE (2012-2016)

Offline Champ

  • KilLeR
  • Alumni
  • Super Member
  • *
  • Points: 1893
  • Posts: 655
  • KilLeR

    • MAIT
    • _
      CSE
    • View Profile
Re: Can same no. be searched at two diff. positions using binary search?
« Reply #12 on: August 29, 2013, 12:09:02 am »

Nahi chal raha bhai tera..
 
this is what I entered:
 
array of 7: 1,2,3,5,5,5,5 .. find 5.. it showed only position 3(even that is wrong)
 
p.s. made the corrections .. attachment


try this...
correction may be required. but this is the concept. iske hisab se kr lio ab code.
  JAAT BOY..
Be Unstoppable

Offline cool98765

  • Alumni
  • Super Member
  • *
  • Points: 2012
  • Posts: 962
  • Knowledge helps. No matter from where it comes.

    • MSIT
    • _
      ECE
    • View Profile
Re: Can same no. be searched at two diff. positions using binary search?
« Reply #13 on: August 29, 2013, 12:14:21 am »

try this...
correction may be required. but this is the concept. iske hisab se kr lio ab code.

Bhai infinite loop ban gaya ye to  :|


Maharaja Surajmal Institute of Technology
ECE (2012-2016)

Offline Champ

  • KilLeR
  • Alumni
  • Super Member
  • *
  • Points: 1893
  • Posts: 655
  • KilLeR

    • MAIT
    • _
      CSE
    • View Profile
Re: Can same no. be searched at two diff. positions using binary search?
« Reply #14 on: August 29, 2013, 12:18:23 am »

Bhai infinite loop ban gaya ye to  :|
bhai tune try kia?  o_O
  JAAT BOY..
Be Unstoppable

 

best way to search varchar string

Started by sagrma

Replies: 1
Views: 1561
Last post May 09, 2013, 11:35:45 pm
by sagrma
decimal to binary converter or viceversa

Started by rudeman

Replies: 0
Views: 1532
Last post December 27, 2008, 03:55:10 pm
by rudeman
Sitemap 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49