PDA

Tam Sürümünü Görmek İçin : binary search yardim??


CaCao
01/04/2007, 12:50
Merhabalar elimde binary search ile ilgili bir algorithm var.

1.0 Let the bottom be the initial array elements.

2.0 Let the top be the last array elements.

3.0 Let found false.

4.0 Repeat as long as bottom is not greater than top and target has not been found.

4.1 Let the middle be subscript of the element half way between bottom
and top.

4.2 If the element middle is target

4.2.1 Set found to true and index ot middle.

4.3 Let top be the middle-1


bunun programini su sekilde yaptim fakat olmadi acaba yardim edebilirmisiniz?

#include<stdio.h>
#include<stdlib.h>
#define SIZE 50
int
main(void)
{
int found, result, top, bottom,ary[SIZE];

found=0;
while(bottom>=top && found=0)
{
result= (bottom+top)/2;
if(ary[result]==found)
found=1;
else
if(ary[result]>found)
found=result-1;
else
found=result+1;
}
system("pause");
return(0);
}


cok acil lazim
tesekkur ederim simdiden


CaCao
01/04/2007, 12:51
pardon yanlislikla iki kere yollamisim :$

Akın Öcal
01/04/2007, 12:59
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define N 10
int dizi[N]={3,6,66,23,7667,2,5,12,43,62} ;
int goster (void);
int sirala (void);
int arama (void) ;

int main (void)
{

puts("SIRALANMAMIS HAL :");
goster ();
getch();
sirala();
puts("SIRALANMIS HAL :");
goster ();
arama();
getch();
return 0;
}

int goster (void)
{
register int i;
for(i=0;i<N;i++)
printf("\n%d\n",dizi[i]);
return 0;
}

int sirala (void)
{
register int i,j;
int gecici;
for(i=0;i<N;i++)
for(j=i+1;j<N;j++)
{
if(dizi[i]>dizi[j])
{
gecici=dizi[i];
dizi[i]=dizi[j];
dizi[j]=gecici;
}
}
return 0;
}

int arama (void)
{
int aranan,sol=0,sag=N-1,orta;
puts("\nAranacak sayiyi giriniz : ");
scanf("%d",&aranan);
while(sol<=sag)
{
orta=(sag+sol)/2;
if(dizi[orta]==aranan) {printf("\n%d dizide var...",aranan);return 0;}
if(aranan>dizi[orta])
sol=orta+1;
else
sag=orta-1;
}
printf("%d dizide yok",aranan);
return -1;
}


ilk ogrendigim zamanlarda dos da yaptigim gayet ilkel bubblesort+binary search programcigiydi :))

CaCao
01/04/2007, 13:20
tessekkur ederim
cok karisikmis seninin yolladigin :)
ya ben daha cok yeniyim programlamada ondan;)
col sagol tekrar

mr1yh1
01/04/2007, 13:24
if(ary[result]>found)
found=result-1;
else
found=result+1;


- result yerine middle adlı bir değişken kullanırsan daha açıklayıcı olur.

- aranan veri nerede ?
found değerini arıyorsun gibi görünüyor ( ary[result]>found yüzünden )
fakat bu değeri sonradan neden değiştiriyorsun.
ayrıca found döngüde kontrol için kullanıldığına göre kesinlikle o aranmıyor.
aranan bulunduğunda döngüyü durdurmak için var.

- bulunamama durumunda middle değil , top veya bottom değişmeli.
yani ary[middle] > arananDeger olması durumunda
arananDeger, middle+1 ile top arasındadır.
küçük olması durumunda bottom ile middle-1.