-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathq10.c
159 lines (135 loc) · 4.02 KB
/
q10.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Binary search and interpolation search
#include <stdio.h>
// Binary Search function
int binarySearch(int arr[], int n, int key, int *iterations)
{
int low = 0, high = n - 1;
*iterations = 0; // Reset iteration count
while (low <= high)
{
(*iterations)++;
int mid = (low + high) / 2;
if (arr[mid] == key)
return mid;
else if (arr[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
return -1; // Element not found
}
// Interpolation Search function
int interpolationSearch(int arr[], int n, int key, int *iterations)
{
int low = 0, high = n - 1;
*iterations = 0; // Reset iteration count
while (low <= high && key >= arr[low] && key <= arr[high])
{
(*iterations)++;
if (low == high)
{
if (arr[low] == key)
return low;
return -1;
}
// Calculate position using interpolation formula
int pos = low + (((double)(high - low) / (arr[high] - arr[low])) * (key - arr[low]));
if (arr[pos] == key)
return pos;
else if (arr[pos] < key)
low = pos + 1;
else
high = pos - 1;
}
return -1; // Element not found
}
// Function to display the array
void displayArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
int main()
{
int arr[100], n, key, choice;
int binaryIterations, interpolationIterations;
// Input array size and elements
printf("Enter the number of elements in the sorted array: ");
scanf("%d", &n);
printf("Enter %d sorted elements: ", n);
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
// Menu-driven logic
while (1)
{
printf("\nMenu:\n");
printf("1. Binary Search\n");
printf("2. Interpolation Search\n");
printf("3. Compare Both Searches\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
if (choice == 4)
{
printf("Exiting...\n");
break;
}
printf("Enter the element to search: ");
scanf("%d", &key);
switch (choice)
{
case 1:
// Binary Search
{
int result = binarySearch(arr, n, key, &binaryIterations);
if (result != -1)
printf("Element found at index %d (Binary Search, Iterations: %d)\n", result, binaryIterations);
else
printf("Element not found (Binary Search, Iterations: %d)\n", binaryIterations);
}
break;
case 2:
// Interpolation Search
{
int result = interpolationSearch(arr, n, key, &interpolationIterations);
if (result != -1)
printf("Element found at index %d (Interpolation Search, Iterations: %d)\n", result, interpolationIterations);
else
printf("Element not found (Interpolation Search, Iterations: %d)\n", interpolationIterations);
}
break;
case 3:
// Compare both searches
{
int binaryResult = binarySearch(arr, n, key, &binaryIterations);
int interpolationResult = interpolationSearch(arr, n, key, &interpolationIterations);
printf("\nBinary Search:\n");
if (binaryResult != -1)
printf("Element found at index %d (Iterations: %d)\n", binaryResult, binaryIterations);
else
printf("Element not found (Iterations: %d)\n", binaryIterations);
printf("\nInterpolation Search:\n");
if (interpolationResult != -1)
printf("Element found at index %d (Iterations: %d)\n", interpolationResult, interpolationIterations);
else
printf("Element not found (Iterations: %d)\n", interpolationIterations);
// Compare number of iterations
if (binaryIterations < interpolationIterations)
printf("\nBinary Search is more efficient with fewer iterations.\n");
else if (binaryIterations > interpolationIterations)
printf("\nInterpolation Search is more efficient with fewer iterations.\n");
else
printf("\nBoth searches performed equally.\n");
}
break;
default:
printf("Invalid choice! Try again.\n");
}
}
return 0;
}