עבור לתוכן
View in the app

A better way to browse. Learn more.

HWzone

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

חיפוש בינארי בתוך מערך ממוין/שפת C

Featured Replies

פורסם

היי חבר'ה

עשיתי את התוכנית..היא ממינת מערך רנמולי..

מה שאני עוד צצריך לעשות זה לעשות חיפוש בינארי

יש בעיה ואני לא מצליח[את החיפוש]..תסתכלו ותעזרו בבקשה.

void StraightSelect(int a[],int size)
{
int i,j,temp;
for(i=0;i<size-1;i++)
{
for(j=i+1;j<size;j++)
{
cmp++;
if(a[i]>a[j])
{
swch++;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}


int bin_search(int a[],int size,int num)
{
int low,high,mid;
low=0;
high=size-1;
while(low<=high)
{
mid=(low+high)/2;
if(a[mid]==num)
return mid;
else
if(a[mid]>num)
high=mid-1;
else
low=mid+1;
}
return -1;
}
void main()
{
randomize();
clrscr();
int a[n],ar[n],i,num,x;

//klita
for(i=0;i<n-1;i++)
{
a[i]=random(5000)+1;
ar[i] = a[i];
printf("%5d",a[i]);
}

//kriaa func
printf("\n");
printf("select");
printf("\n");
StraightSelect(a,n);
//hadpasa
for(i=0;i<n;i++)
printf("%5d",a[i]);
printf("\n");
for(i=0;i<n;i++)
a[i] = ar[i];
printf("\n");
printf ("number of compares=%d",cmp);
printf("\n");
printf ("number of switches=%d",swch);
cmp=0; swch=0;


printf("\n");
printf("enter 30 numbers to serch");

scanf("%d",num);
while ((num!=-1) &&(i<n))
{
scanf("%d",num);

//kriaa func
x=bin_search(a,n,num);
//hadpasa
printf("%d",x);
}














getch();



}






פורסם

בתור התחלה תעבור לרקורסיה, או לפחות תנסה.

פורסם

לפי מה שהבנתי מה-main שלך, אחרי שאתה מדפיס את המערך הממויין (a) אתה דורך עליו עם ar, שאינו ממוין... זה יגרום לכך שהחיפוש לא יעבוד.

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.