summaryrefslogtreecommitdiff
path: root/week2/Program.cs
blob: feef98b7fda0c701d013bd148ebd741f2a36d10a (plain)
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.Remoting.Channels;
using System.Text;
using System.Threading.Tasks;

namespace ALGA {
	class Program {
		static void Main(string[] args) {
			int items = 1000;
			int repetitions = 1_000;

			Stopwatch sw = new Stopwatch();
			int swaps = 0, comparisons = 0;

			for (int i = 0; i < repetitions; i++) {
				SortList list = new SortList(items);

				sw.Start();
				Bubblesort.bubblesort(list);
				sw.Stop();

				swaps += list.Swaps;
				comparisons += list.Comparisons;
			}
			Console.WriteLine("Unsorted:");
			Console.WriteLine("\tExec:        {0} ns", sw.ElapsedMilliseconds * 10e3 / repetitions);
			Console.WriteLine("\tSwaps:       {0} (avg)", swaps / repetitions);
			Console.WriteLine("\tComparisons: {0} (avg)", comparisons / repetitions);
			sw.Reset();
			swaps = 0;
			comparisons = 0;
			
			for (int i = 0; i < repetitions; i++) {
				SortList list = new SortList(items, true);

				sw.Start();
				Bubblesort.bubblesort(list);
				sw.Stop();

				swaps += list.Swaps;
				comparisons += list.Comparisons;
			}
			Console.WriteLine("Sorted:");
			Console.WriteLine("\tExec:        {0} ns", sw.ElapsedMilliseconds * 10e3 / repetitions);
			Console.WriteLine("\tSwaps:       {0} (avg)", swaps / repetitions);
			Console.WriteLine("\tComparisons: {0} (avg)", comparisons / repetitions);
			Console.WriteLine("");

			Console.WriteLine("({0} items, {0} repetitions)", items, repetitions);

			Console.ReadLine();
		}
	}
}