-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathARRAY_SearchingTechniques_lab1.c
70 lines (69 loc) · 1.73 KB
/
ARRAY_SearchingTechniques_lab1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
int i, n, ch;
printf("IMPLEMENTATION OF SEARCHING TECHNIQUES\n");
printf("Enter the number of elements: ");
scanf("%d", &n);
int *arr=(int*)malloc(sizeof(int)*n);
printf("Enter the array elements:\n");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
int searchKey;
printf("Enter the search Element: ");
scanf("%d", &searchKey);
system("cls");
printf("IMPLEMENTATION OF SEARCHING TECHNIQUES\n");
printf("\n1.\tLinearSearch\n2.\tBinarySearch\n3.\tExit\n");
printf("Choose the searching technique: ");
scanf("%d", &ch);
switch(ch)
{
case 1: {
for(i=0;i<n;i++)
{
if(searchKey==arr[i])
{
printf("\nElement found at %d position",i+1);
break;
}
}
if(i>=n)
{
printf("Element not found in the given array!\n");
}
getch();
exit(1);
}
case 2: {
int first = 0, last = n-1, middle=(first+last)/2;
while(first<=last)
{
if(arr[middle]<searchKey)
{
first=middle+1;
}
else if(arr[middle]==searchKey)
{
printf("Element found at %d position",middle+1);
getch();
break;
}
else
last=middle-1;
middle=(first+last)/2;
if(first>last)
{
printf("Element not found!");
getch();
}
}
}
case 3: exit(1);
default: printf("\nInvalid Choice");break;
}
}