বাইনারি সার্চ অ্যালগরিদম (Binary Search Algorithm)



#include <stdio.h>
int main()
{
int N, i, j, a, F, L, M, N1, arr[100], step=0;
printf("Number of integers: ");
scanf("%d", &N);
printf("Enter %d integers: \n", N);
for (i=0;i<N;i++)
{
scanf("%d", &arr[i]);
}
printf("Enter the value to find: ");
scanf("%d", &N1);
for (i=0;i<N;++i)
{
for (j=i+1;j<N;++j)
{
if(arr[i]>arr[j])
{

a=arr[i];
arr[i]=arr[j];
arr[j]=a;
}
}
}
printf("The Accending numbers are : ");
for(i=0;i<N;i++)
{
printf("%d ", arr[i]);
}
printf("\n");
F=0;
L=N-1;
M=(F+L)/2;
while (F<=L)
{
printf("For step %d:\n", step+1);
printf("First Number is %d\nLast Number is %d\nMiddle Number is %d\n", arr[F], arr[L], arr[M]);
if (arr[M]<N1)
{
printf("Here %d is smaller than %d\n", arr[M], N1);
F=M+1;
step++;
printf("So, the step will move to the RIGHT side\n");
}
else if (arr[M]==N1)
{
printf("Here %d is equal to %d\n", arr[M], N1);
printf("So, %d is FOUND at position %d.\n", N1, M+1);
step++;
break;
}
else
{
printf("Here %d is greater than %d\n", arr[M], N1);

L=M-1;
step++;
printf("So, the step will move to the LEFT side\n");
}
printf("Step %d completed \n", step);
M=(F+L)/2;
}
if (F>L)
{
printf("%d is NOT in the list\n", N1);
}
printf("Total steps needed = %d\n", step);
return 0;
}

  
Sample Input: Number of integers: 10 Enter 10 integers: 5 6 9 8 7 5 3 2 1 12 
Sample Output: Enter the value to find: 3 The Accending numbers are : 1 2 3 5 5 6 7 8 9 12 
For step 1: First Number is 1 Last Number is 12 Middle Number is 5 Here 5 is greater than 3 So, the step will move to the LEFT side Step 1 completed 
For step 2: First Number is 1 Last Number is 5 Middle Number is 2 Here 2 is smaller than 3 So, the step will move to the RIGHT side Step 2 completed 
For step 3: First Number is 3 Last Number is 5 Middle Number is 3 Here 3 is equal to 3 So, 3 is FOUND at position 3. Total steps needed = 3

Post a Comment

0 Comments