Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dfb165f

Browse files
Merge pull request fnplus#107 from AkashM398/Update-2
Add Sub Directory
2 parents d102dfb + 9040a78 commit dfb165f

File tree

3 files changed

+79
-37
lines changed

3 files changed

+79
-37
lines changed
File renamed without changes.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//Binary Search Iterative Method
2+
3+
#include<stdio.h>
4+
#include<conio.h>
5+
6+
void main()
7+
{
8+
int n, arr[50], first, mid, last, key, loc, i;
9+
int flag=0;
10+
clrscr();
11+
12+
printf("\nEnter number of elements: ");
13+
scanf("%d", &n);
14+
printf("\nEnter the elements: ");
15+
16+
for(i=0; i<n; i++)
17+
scanf("%d", &arr[i]);
18+
first=0;
19+
last=n;
20+
printf("\nEnter the element to be searched: ");
21+
scanf("%d", &key);
22+
23+
while(first<=last)
24+
{
25+
mid=(first+last)/2;
26+
if(arr[mid]==key)
27+
{
28+
flag=1;
29+
printf("\nElement found at location %d", mid+1);
30+
break;
31+
}
32+
else if(key>arr[mid])
33+
first=mid+1;
34+
else
35+
last=mid-1;
36+
}
37+
38+
if(flag==0)
39+
printf("\nElement not found");
40+
getch();
41+
42+
}
Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
1-
//Binary search - Recursive method
2-
#include <stdio.h>
3-
#define MAX 1000
4-
int binarySearch(int ar[], int l, int r, int search)
5-
{
6-
if (l<=r)
7-
{
8-
int mid = (l+r)/2;
9-
if (ar[mid] == search) // we have found our search element
10-
return mid;
11-
else if (ar[mid] > search) // the element must be present in the left subarray
12-
binarySearch(ar, l, mid-1, search);
13-
else
14-
binarySearch(ar, mid+1, r, search); // the element must be present in the right subarray
15-
}
16-
return -1;
17-
}
18-
int main()
19-
{
20-
int N, i, key;
21-
int ar[MAX];
22-
printf ("Enter no of integers in the array:\n");
23-
scanf ("%d", &N);
24-
printf ("\nEnter the elements in a sorted array:\n");
25-
for (i=0; i<N; i++)
26-
{
27-
scanf ("%d", &ar[i]);
28-
}
29-
printf ("\nEnter the element to search:\n");
30-
scanf ("%d", &key);
31-
int pos = binarySearch(ar, 0, N-1, key);
32-
if (pos == -1)
33-
printf ("%d is not present in the array!", key);
34-
else
35-
printf ("%d is present at index %d", key, pos);
36-
return 0;
37-
}
1+
//Binary search - Recursive method
2+
#include <stdio.h>
3+
#define MAX 1000
4+
int binarySearch(int ar[], int l, int r, int search)
5+
{
6+
if (l<=r)
7+
{
8+
int mid = (l+r)/2;
9+
if (ar[mid] == search) // we have found our search element
10+
return mid;
11+
else if (ar[mid] > search) // the element must be present in the left subarray
12+
binarySearch(ar, l, mid-1, search);
13+
else
14+
binarySearch(ar, mid+1, r, search); // the element must be present in the right subarray
15+
}
16+
return -1;
17+
}
18+
int main()
19+
{
20+
int N, i, key;
21+
int ar[MAX];
22+
printf ("Enter no of integers in the array:\n");
23+
scanf ("%d", &N);
24+
printf ("\nEnter the elements in a sorted array:\n");
25+
for (i=0; i<N; i++)
26+
{
27+
scanf ("%d", &ar[i]);
28+
}
29+
printf ("\nEnter the element to search:\n");
30+
scanf ("%d", &key);
31+
int pos = binarySearch(ar, 0, N-1, key);
32+
if (pos == -1)
33+
printf ("%d is not present in the array!", key);
34+
else
35+
printf ("%d is present at index %d", key, pos);
36+
return 0;
37+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /