-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSequentialSearch.cs
62 lines (55 loc) · 1.77 KB
/
SequentialSearch.cs
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
namespace Lab03_Search;
using System;
using System.Globalization;
class SequentialSearch
{
static void Main(string[] args)
{
// la hora de inicio de ejecución
DateTime startTime = DateTime.Now;
Console.WriteLine("Hora de inicio de ejecución: {0}", startTime);
// Crear 100 enteros aleatorios
Random random = new Random();
int[] numeros = new int[100];
for (int i = 0; i < numeros.Length; i++)
{
numeros[i] = random.Next(1, 201);//numeros random del 1 al 200
}
int exito = 0;
int fallas = 0;
for (int i = 0; i < 50; i++)
{
int busqnum = random.Next(1, 201);
bool encontr = false;
for (int j = 0; j < numeros.Length; j++)
{
if (numeros[j] == busqnum)
{
encontr = true;
break;
}
}
if (encontr)
{
exito++;
}
else
{
fallas++;
}
}
// estadísticas
Console.WriteLine("Número de búsquedas con éxito: {0}", exito);
Console.WriteLine("Número de búsquedas fallidas: {0}", fallas);
Console.WriteLine("Porcentaje de éxito de búsquedas: {0:P2}", (double)exito / 50);
Console.WriteLine("Porcentaje de fallo de búsquedas: {0:P2}", (double)fallas / 50);
Console.WriteLine("Números en orden creciente:");
foreach (int number in numeros.OrderBy(x => x))
{
Console.Write(number + " ");
}
// la hora de fin de ejecución
DateTime endTime = DateTime.Now;
Console.WriteLine("\nHora de fin de ejecución: {0}", endTime);
}
}