-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinearSearchWithVariations.c
97 lines (85 loc) · 1.75 KB
/
LinearSearchWithVariations.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
// Linear search using sentinel on 1D array
// And Linear search on 2D array
#include <stdio.h>
void read_arr(int size, int arr[])
{
for (int i = 0; i < size; ++i)
scanf("%d", arr + i);
}
void print_arr(int size, int arr[])
{
printf("[");
for (int i = 0; i < size; ++i)
printf("%d ", *(arr + i));
printf("]\n");
}
int linear_search(int size, int arr[], int search_key) // With sentinel
{
int last_elem = arr[size - 1];
arr[size - 1] = search_key;
int i;
for (i = 0; arr[i] != search_key; ++i);
if (i == size - 1 && last_elem != search_key) return -1;
else return i;
}
void main_linear_search()
{
int size;
scanf("%d", &size);
int arr[size];
read_arr(size, arr);
int search_key;
scanf("%d", &search_key);
int res = linear_search(size, arr, search_key);
if (res == -1)
{
printf("Not found.\n");
}
else
{
printf("First occurence at %d.\n", res);
}
}
void print_matrix(int rows, int cols, int mat[rows][cols])
{
printf("[\n");
for (int i = 0; i < rows; ++i, printf("\n"))
for (int j = 0; j < cols; ++j)
printf("%d ", mat[i][j]);
printf("]\n");
}
void matrix_search(int rows, int cols, int mat[rows][cols], int search_key, int* rpos, int* cpos)
{
for (int i = 0; i < rows; ++i)
for (int j = 0; j < cols; ++j)
if (mat[i][j] == search_key)
{
*rpos = i;
*cpos = j;
return;
}
}
void main_matrix_search()
{
int rows, cols;
scanf("%d %d", &rows, &cols);
int mat[rows][cols];
read_arr(rows*cols, &mat[0][0]);
int search_key;
scanf("%d", &search_key);
int rpos = -1, cpos;
matrix_search(rows, cols, mat, search_key, &rpos, &cpos);
if (rpos == -1)
{
printf("Not found.\n");
}
else
{
printf("First occurence at (%d, %d).\n", rpos, cpos);
}
print_matrix(rows, cols, mat);
}
void main()
{
main_matrix_search();
}