-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path17_linearserchalg_v0.c
99 lines (74 loc) · 1.99 KB
/
17_linearserchalg_v0.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
/* Project 17 -- Linear Search Algorithm
Sequential Search
Sorting algorithm
In computer science, a linear search or sequential search is a method
for finding an element within a list. It sequentially checks each
element of the list until a match is found or the whole list has been searched.
(Wikipedia)
Worst complexity: O(n)
Average complexity: O(n)
Space complexity: O(1)
Average performance: O(n/2)
Class: Search algorithm
Features:
1- find an element within a list
*****************************************************************
Output
Sequential Search Algorithm
Vector Generation:
792 330 111 19 473 641 505 862 70 60
Select a value to search: 19
Value found in the position 4.
Pressione qualquer tecla para continuar. . .
*****************************************************************
Based on: https://github.com/borinvini
UNINTER - Curso: Engenharia da Computacao
Escola Superior Politecnica
Author: Gilberto Jr RU 3326662
Edited: J3
Date: Nov, 2021
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int SequentialSearch(int vec[], int sought);
#define VECTORSIZE 10
int main()
{
int vec[VECTORSIZE] = { 0 };
int sought, found, i;
srand(time(NULL));
// Entering data
for (int i = 0; i < VECTORSIZE; i++)
vec[i] = rand() % 1000;
printf("Sequential Search Algorithm\n");
printf("Vector Generation:\n");
for (int i = 0; i < VECTORSIZE; i++)
printf("%d\t", vec[i]);
printf("\nSelect a value to search: ");
scanf_s("%d", &sought);
found = SequentialSearch(vec, sought);
if (found == -1)
printf("\nValue not found. \n");
else
printf("Value found in the position %d. \n", found);
system("pause");
return 0;
}
int SequentialSearch(int vec[], int sought)
{
int found, i;
found = 0;
i = 0;
while ((i <= VECTORSIZE) && (found == 0))
{
if (vec[i] == sought)
found = 1;
else
i++;
}
if (found == 0)
return -1;
else
return i + 1;
}