This repository has been archived by the owner on Jul 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp14_1910.c
118 lines (107 loc) · 1.77 KB
/
p14_1910.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Emmanuel Jojy
// S3 CSE A
// Roll no: 53
// Prg 14/1910
// Standard Searching Algorithms
// Linear and Binary Search
#include<stdio.h>
void lsearch();
void bsearch();
void sort(int arr[], int n);
int arr[20], n, key;
void main()
{
int i;
int ch;
printf("Enter length of array: ");
scanf("%d", &n);
printf("\nEnter elements\n");
for(i = 0; i < n; i++)
{
printf("arr[%d]: ", i);
scanf("%d", &arr[i]);
}
while(1)
{
printf("*** Search Algorithms ***");
printf("\n 1. Linear Search\n 2. Binary Search\n 3. Exit\nChoice: ");
scanf("%d", &ch);
printf("\n\tEnter element to search: ");
scanf("%d", &key);
switch(ch)
{
case 1: lsearch(); break;
case 2: bsearch(); break;
default: return;
}
}
}
void lsearch()
{
int flag = 0, i, index;
flag = 0;
for(i = 0; i < n && flag == 0; i++)
{
if(arr[i] == key)
{
flag = 1;
index = i;
}
}
if(flag == 1)
{
printf("\n\tLinear Search >> %d found at index %d.\n", key, index);
}
else
{
printf("\n\tLinear Search >> %d Not found.\n", key);
}
}
void bsearch()
{
sort(arr, n);
int low = 0, high = n - 1, mid = (low + high) / 2;
while(key != arr[mid] && low < high)
{
if(key > arr[mid])
{
low = mid + 1;
}
else
{
high = mid - 1;
}
mid = (low + high) / 2;
}
if(key == arr[mid])
{
printf("\n\tBinary Search >> %d found at index %d.\n", key, mid);
}
else
{
printf("\n\tBinary Search >> %d Not found.\n", key);
}
}
void sort(int arr[], int n)
{
int i, j, temp, flag = 1;
for(i = 0; i < n - 1 && flag == 1; i++)
{
flag = 0;
for(j = 0; j < n - i - 1; j++)
{
if(arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
flag = 1;
}
}
}
printf("\tSorted array: ");
for(i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
}