-
Notifications
You must be signed in to change notification settings - Fork 0
/
array_binary_search.c
202 lines (164 loc) · 4.1 KB
/
array_binary_search.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* Program: Searching an int in a sorted integer array with Binary Search Algorithm
* Made by: Ransomware3301 (https://github.com/ransomware3301)
* Date: 01-11-2023
*/
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define DEBUG 1
typedef int BOOL;
typedef struct result {
int index;
BOOL found;
} result_t;
void array_input(int*, int);
void insertion_sort(int*, int);
void print_array(int*, int);
result_t binary_search(int*, int, int);
int main(int argc, char *argv[])
{
int dim;
int elem;
int *arr;
result_t res;
/* Array size input */
do{
printf("Insert array size: ");
scanf("%d", &dim);
}while(dim < 1);
if(( arr = (int*)malloc(sizeof(int) * dim) ))
{
array_input(arr, dim); /* Array input */
insertion_sort(arr, dim); /* Sorting the array */
if(DEBUG){
printf("---------------\n(DEBUG)");
print_array(arr, dim);
printf("---------------\n\n");
}
printf("Insert element to search in array: ");
scanf("%d", &elem);
res = binary_search(arr, dim, elem);
if(res.found == TRUE){
printf("\nValue %d was found at index %d\n\n", elem, res.index);
}
else{
printf("\n=> Value %d was not found\n\n", elem);
}
free(arr);
}
else{
printf("[in \"main()\"] - Memory allocation error\n");
}
return 0;
}
/*
* Takes a declared array reference as input and fills it
* with input values from user
*/
void array_input(int *arr, int dim)
{
int i;
for(i = 0; i < dim; i++)
{
printf("Insert value at index %d: ", i);
scanf("%d", (arr + i));
}
printf("\n");
}
/*
* Sorts the passed array with the Insertion Sort Algorithm
* in the ascending order
*/
void insertion_sort(int *arr, int dim)
{
int i, j;
int temp;
for(i = 0; i < dim - 1; i++)
{
for(j = i + 1; j < dim; j++)
{
if(arr[j] < arr[i])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
/*
* Prints the passed array to terminal
*/
void print_array(int *arr, int dim)
{
int i;
printf("\nArray Contents:\n");
for(i = 0; i < dim; i++){
printf("%d ", *(arr + i));
}
printf("\n");
}
/*
* Performs a binary search of elem in array and returns a struct containing
* the index and wether the element was found or not
*/
result_t binary_search(int *arr, int dim, int elem)
{
int l, r;
int center;
result_t res;
if(dim < 2){
/* Handling case where array dim == 1 */
if( *(arr) == elem )
{
res.index = 0;
res.found = TRUE;
}
else
{
res.index = -1;
res.found = FALSE;
}
}
else
{
/* Border flags, one at start and one at end of array */
l = 0;
r = dim - 1;
/* Default vaules in case element isn't found */
res.index = -1;
res.found = FALSE;
while( !res.found && l < r)
{
/* Calculating the center of the current array partition */
center = (l + r)/2;
if(DEBUG){
printf("\n---------------\n(DEBUG) ");
printf("\nCENTER: %d\n", center);
printf("L: %d | R: %d\n", l, r);
printf("---------------\n");
}
if( *(arr + center) == elem ){
res.index = center;
res.found = TRUE;
}
else if( *(arr + l) == elem){
res.index = l;
res.found = TRUE;
}
else if( *(arr + r) == elem){
res.index = r;
res.found = TRUE;
}
else if( elem > *(arr + center) ){
l = center + 1;
}
else{
r = center - 1;
}
}
}
return res;
}