mid-1
) ham adres
maydoni TTJ bo„lsa,
search--,
ya‟ni bitta oldingi elementga o„tamiz va shu
qadamni boshidan bajaramiz. Aks holda keyingi qadamga o„tiladi.
8.
Joriy (
search
ko„rsatayotgan) elementdan boshlab adresi “TTJ” ga teng
bo„lgan talaba ma‟lumotlarini ekranga chiqaramiz. Agar adresi “TTJ” dan farq
qiladigan talaba chiqib qolsa, algoritm tugallanadi.
Dastur kodi
#include
using namespace std;
int main(){
int n;cout<<"n=";cin>>n;
105
struct Guruh{
string fio,adres;
}talaba[n];
for(int i=0;i
cout<>talaba[i].fio;
cout<<"adres=";cin>>talaba[i].adres;
}
//jadval binar qidiruv olib boriladigan maydoni bo‘yicha tartiblangan
//bo‘lishi kerak
for(int i=0;i
for(int j=i+1;j
if(talaba[i].adres>talaba[j].adres){
Guruh h=talaba[i];
talaba[i]=talaba[j];
talaba[j]=h;
}
for(int i=0;i
cout<
cout<
int low = 0,hi = n-1,search=-1,q=0;
string key="TTJ";
while(low<=hi){
int mid = (low + hi) / 2;
q++;
if (key == talaba[mid].adres){
search = mid;
break;
}
if (key < talaba[mid].adres)
hi = mid - 1;
106
else low = mid + 1;
}
if(search!=-1) cout<<"qidirilayotgan el "<
turibdi va "<
else {cout<
system("PAUSE");
return EXIT_SUCCESS;
}
while(talaba[search-1].adres==key) search--;
while(talaba[search].adres==key) {
cout<
"<
search++; }
system("pause");
}
Do'stlaringiz bilan baham: |