0% found this document useful (0 votes)
11 views

Projekt 1

The document compares the performance of linear and binary search algorithms on arrays of different sizes by measuring the average time and number of comparisons. It generates random sorted arrays, performs searches on random targets within the arrays, times the searches, and calculates averages to analyze performance as array size increases.

Uploaded by

Stas Rybtsev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Projekt 1

The document compares the performance of linear and binary search algorithms on arrays of different sizes by measuring the average time and number of comparisons. It generates random sorted arrays, performs searches on random targets within the arrays, times the searches, and calculates averages to analyze performance as array size increases.

Uploaded by

Stas Rybtsev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

using System;

using System.Diagnostics;
using System.Linq;
using System.Collections.Generic;

int LinearSearch(int[] arr, int targ, out int compare)


{
compare = 0;
for (int i = 0; i < arr.Length; i++)
{
compare++;
if (arr[i] == targ)
{
return i;
}
}
return -1;
}

int BinarySearch(int[] arr, int targ, out int compare)


{
compare = 0;
int low = 0;
int high = arr.Length - 1;

while (low <= high)


{
compare++;
int mid = (low + high) / 2;
if (arr[mid] == targ)
{
return mid;
}
else if (arr[mid] < targ)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return -1;
}

int[] SortedArray(int size)


{
var random = new Random();
var array = new int[size];
for (int i = 0; i < size; i++)
{
array[i] = random.Next(1, size * 10);
}
Array.Sort(array);
return array;
}

double TimeInMicroseconds(Action action)


{
var time = new List<double>();
var stopwatch = new Stopwatch();

for (int i = 0; i < 20; i++)


{
stopwatch.Start();
action.Invoke();
stopwatch.Stop();

time.Add(stopwatch.Elapsed.TotalMilliseconds * 1000);
stopwatch.Reset();
}

time.Remove(time.Max());
time.Remove(time.Min());

return time.Average();
}

void Analyze()
{
int[] sizes = { 10_000_000, 20_000_000, 30_000_000, 40_000_000, 50_000_000,
60_000_000, 70_000_000, 80_000_000, 90_000_000, 100_000_000 };

foreach (var size in sizes)


{
var arr = SortedArray(size);
var random = new Random();

int linearSum = 0;
int binarySum = 0;
double linearTimeSum = 0;
double binaryTimeSum = 0;

var targets = Enumerable.Range(0, 10).Select(_ => random.Next(1,


size)).ToArray();

foreach (var targ in targets)


{
linearTimeSum += TimeInMicroseconds(() =>
{
int compare;
LinearSearch(arr, targ, out compare);
linearSum += compare;
});

binaryTimeSum += TimeInMicroseconds(() =>


{
int compare;
BinarySearch(arr, targ, out compare);
binarySum += compare;
});
}

double avgLinearTime = linearTimeSum / 1000000;


double avgBinaryTime = binaryTimeSum / 10;
int avgLinearCompare = linearSum / 10;
int avgBinaryCompare = binarySum / 10;
var maxElement = arr[^1];
LinearSearch(arr, maxElement, out int maxLinearCompare);
BinarySearch(arr, maxElement, out int maxBinaryCompare);

Console.WriteLine($"Rozmiar: {size}");
Console.WriteLine($"Średni czas (linowy): {avgLinearTime:F2} ms");
Console.WriteLine($"Średni czas (binarne): {avgBinaryTime:F2} ms");
Console.WriteLine($"Średnia liczba porównań (linowy): {avgLinearCompare}");
Console.WriteLine($"Średnia liczba porównań (binarne):
{avgBinaryCompare}");
Console.WriteLine($"Maksymalna liczba porównań (linowy):
{maxLinearCompare}");
Console.WriteLine($"Maksymalna liczba porównań (binarne):
{maxBinaryCompare}");
}
}
Analyze();

You might also like