-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBigO_2.cs
85 lines (78 loc) · 2.3 KB
/
BigO_2.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace BigO;
public class BigO_2App
{
// Remember to open the System Monitor
public static void Main(string[] args)
{
var rnd = new Random();
Console.WriteLine("Checking O(N)");
for (int i = 0; i < 10; i++)
{
Check_O_N((int)Math.Pow(10, i), rnd);
}
Console.WriteLine("O(N) completed");
Console.WriteLine("Checking O(N²)");
for (int i = 0; i < 6; i++)
{
Check_O_N2((int)Math.Pow(10, i), rnd);
}
Console.WriteLine("O(N²) completed");
// What is the Big O Notation?
// Attention, it is a trick question
var stopwatch = new Stopwatch();
stopwatch.Start();
long sum = 0;
for (int i = 0; i < 1000; i++) // O(1) because all loops have a constant
{
for (int j = 0; j < 1000; j++)
{
for (int k = 0; k < 1000; k++)
{
sum += i + j + k;
}
}
}
Console.WriteLine($"Sum: {sum}");
stopwatch.Stop();
Console.WriteLine($"Elapsed: {stopwatch.Elapsed}");
}
private static void Check_O_N(int N, Random rnd)
{
var stopwatch = new Stopwatch();
Console.Write($"Elapsed time with {N:N0} operations:");
var list = new List<int>();
stopwatch.Start();
for (int i = 0; i < N; i++)
{
list.Add(rnd.Next(N));
}
stopwatch.Stop();
Console.WriteLine($"{stopwatch.Elapsed}");
}
private static void Check_O_N2(long N, Random rnd)
{
var stopwatch = new Stopwatch();
Console.Write($"Elapsed time with {N:N0} operations:");
var list = new List<long>();
stopwatch.Start();
for (int i = 0; i < N; i++)
{
list.Add(rnd.NextInt64(N));
}
// "Print" the sum from each index to the end of the list
for (int i = 0; i < N; i++)
{
long sum = 0;
for (int j = i; j < N; j++)
{
sum += list[j];
}
//Console.WriteLine($"{i} = {sum}");
}
stopwatch.Stop();
Console.WriteLine($"{stopwatch.Elapsed}");
}
}